效果图
单个图片的类
DJItemImageView.java
package com.ldci.djrotatemenuforandroid;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;public class DJItemImageView extends RelativeLayout implements OnClickListener{public ImageView imageView;public DJItemImageView(Context context) {super(context);//this.setBackgroundColor(Color.GREEN);//设置该布局的属性RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);lp1.addRule(RelativeLayout.CENTER_HORIZONTAL);lp1.addRule(RelativeLayout.CENTER_VERTICAL);// TODO Auto-generated constructor stubimageView = new ImageView(context);imageView.setRotation(180);Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ui_navi_course_in)).getBitmap();//设置图片的属性RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());//RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);lp.addRule(RelativeLayout.CENTER_VERTICAL);imageView.setLayoutParams(lp);//imageView.setImageBitmap(bitmap);imageView.setOnClickListener(this);this.addView(imageView);// lp1.width = bitmap.getWidth() * 3;// lp1.height = bitmap.getHeight();this.setLayoutParams(lp1);}public void setImageTag(int tagId) {imageView.setId(tagId);Log.i("image", "imageItem tag" + tagId);}public void djsetImageResources(int imageId) {Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(imageId)).getBitmap();imageView.setImageBitmap(bitmap);}@Overridepublic void onClick(View v) {Log.i("image", "1111111111111111111111111111" + v.getId());// TODO Auto-generated method stubswitch (v.getId()) {case 1001:Log.i("image", "1111111111111111111111111111");break;case 1002:Log.i("image", "222222222222222222222222222222");break;case 1003:Log.i("image", "33333333333333333333333333333");break;case 1004:Log.i("image", "444444444444444444444444444");break;case 1005:Log.i("image", "5555555555555555555555555555");break;case 1006:Log.i("image", "66666666666666666666666666");break;case 10000:Log.i("image", "777777777777777777777");break;default:break;}}}
6个图片的第一次集合类
DJRotateRelativeLayout.java
package com.ldci.djrotatemenuforandroid;import java.util.ArrayList;import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;public class DJRotateRelativeLayout extends RelativeLayout{private int imageIdsDefault[] = {R.drawable.ui_navi_home,R.drawable.ui_navi_feature,R.drawable.ui_navi_course,R.drawable.ui_navi_listen,R.drawable.ui_navi_play,R.drawable.ui_navi_more};private int imageIdsIn[] = {R.drawable.ui_navi_home_in,R.drawable.ui_navi_feature_in,R.drawable.ui_navi_course_in,R.drawable.ui_navi_listen_in,R.drawable.ui_navi_play_in,R.drawable.ui_navi_more_in};private ArrayList
处理旋转功能的类
DJRotateMenuView.java
package com.ldci.djrotatemenuforandroid;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;public class DJRotateMenuView extends RelativeLayout implements AnimationListener,OnClickListener{//角度private double angle;//旋转角度private double rotate;//现在的角度private double nowDegress;private double previousDegress;//计算角度变化的数据private double degressNum;//按钮间的夹角private double djDegressBtn;//当前的角度private double currentDegree;//屏幕的宽private float winSizeWidth;//高private float winSizeHeight;private boolean selectBoolean;//上一次触摸点的坐标private PointF previousPointF;//目标位置private float destinationAngle;private int previousimageId;private int currentImageId = 0;private int previousImageNum = 90;//private ImageView btnImageView;//private TranslateAnimation translateAnimation;//private RotateAnimation rotateAnimation;// private boolean calBool = true;//中心的点坐标 。。。。。临时 设置的屏幕中心PointF centerPointF;//当前计算机1秒钟的计算能力int myComputerCalculate = 0;private DJRotateRelativeLayout djRotateRelativeLayout;public DJRotateMenuView(Context context,float tempWidth,float tempHeight) {super(context);// this.setBackgroundColor(Color.BLUE);this.winSizeWidth = tempWidth;this.winSizeHeight = tempHeight;this.centerPointF = new PointF(this.winSizeWidth / 2, this.winSizeHeight / 2);previousimageId = 0;djDegressBtn = 360 / 6;// AbsoluteLayout.LayoutParams new_params = new AbsoluteLayout.LayoutParams((int)this.winSizeWidth, (int)this.winSizeHeight, (int)this.winSizeWidth, (int)this.winSizeHeight / 2);djRotateRelativeLayout = new DJRotateRelativeLayout(context);
// djRotateRelativeLayout.setLayoutParams(new_params);this.addView(djRotateRelativeLayout);// djRotateRelativeLayout.setRotation(120);//设置图片djRotateRelativeLayout.changeImageResoures((int)(djRotateRelativeLayout.getRotation() / djDegressBtn));degressNum = 0;// btnImageView = new ImageView(context);
// btnImageView.setId(987654);
// btnImageView.setOnClickListener(this);
//
// Bitmap btnBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ui_navi_triangle)).getBitmap();
// RelativeLayout.LayoutParams btnlp = new RelativeLayout.LayoutParams(btnBitmap.getWidth(),btnBitmap.getHeight());
// btnImageView.setImageBitmap(btnBitmap);
// btnlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
// btnlp.addRule(RelativeLayout.CENTER_VERTICAL);
// //btnlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//
// btnImageView.setLayoutParams(btnlp);
// this.addView(btnImageView);//btnlp.alignWithParent &#61; false;}public void djTouchBegin(PointF touchPointF) {double distance &#61; djDistance(touchPointF, this.centerPointF);//获取当前图片IDSystem.out.println("distance " &#43; distance);}public void djTouchMove(PointF touchPointF) {PointF previousLocationF &#61; previousPointF;//角度差PointF previousCGP &#61; djCgpSub(previousLocationF, centerPointF);double previousVector &#61; djToAngle(previousCGP);previousDegress &#61; dj_Radians_To_Degrees(previousVector);PointF nowLocation &#61; touchPointF;PointF nowCGP &#61; djCgpSub(nowLocation, centerPointF);double nowVector &#61; djToAngle(nowCGP);nowDegress &#61; dj_Radians_To_Degrees(nowVector);//在这里判断角度angle &#61; -(nowDegress - previousDegress);currentDegree &#61; djRotateRelativeLayout.getRotation();//degressNum &#43;&#61; angle;angle &#43;&#61; currentDegree;djSetRotate(angle);//djChangeImageView();}public void djChangeImageView() {float tempCurrentAngle &#61; djRotateRelativeLayout.getRotation();System.out.println("tempCurrentAngle" &#43; tempCurrentAngle);double currentAngle &#61; djRotateRelativeLayout.getRotation();double currentSub;currentSub &#61; currentAngle / djDegressBtn;int temp &#61; (int)currentSub;currentSub &#61; currentAngle -djDegressBtn * temp;if (currentSub > 0) {if (temp !&#61; previousImageNum) {if (currentSub > djDegressBtn / 2) {currentImageId &#43;&#61; 1;}} }else if (currentSub <0) {if (temp !&#61; previousImageNum) {currentImageId -&#61; 1;}}previousImageNum &#61; temp;if (currentImageId <0) {currentImageId &#43;&#61; 6;}// if (tempCurrentAngle > 0) {
//
// tempCurrentAngle &#43;&#61; djDegressBtn / 2;
// }
// else if(tempCurrentAngle <0){
// tempCurrentAngle -&#61; djDegressBtn / 2;
// }
// int btnTypeNum &#61; (int)((tempCurrentAngle &#43; djDegressBtn / 2) / (djDegressBtn));
// btnTypeNum &#61; (int)tempCurrentAngle / (int)djDegressBtn;
//
// float currentSub &#61; tempCurrentAngle - (float)djDegressBtn * btnTypeNum;
// // System.out.println("btnTypeNum" &#43; btnTypeNum);// //btnTypeNum &#61; 6-btnTypeNum;
// System.out.println("btnTypeNum" &#43; btnTypeNum);}public void djTouchEnd(float touchX,float touchY) {calculateDegress();//djChangeImageView();}public void calculateDegress() {double currentAngle &#61; djRotateRelativeLayout.getRotation();double currentSub;currentSub &#61; currentAngle / djDegressBtn;int temp &#61; (int)currentSub;currentSub &#61; currentAngle -djDegressBtn * temp;System.out.println("currentSub &#61; " &#43; currentSub);if (currentSub > 0) {if (currentSub > djDegressBtn / 2) {currentSub &#61; djDegressBtn - currentSub;}else if (currentSub
//
// Timer timer &#61; new Timer();
// timer.schedule(new TimerTask() {
//
// &#64;Override
// public void run() {
// // TODO Auto-generated method stub
// calBool &#61; true;
// }
// }, 1);
//
//
// while (true) {
// myComputerCalculate&#43;&#43;;
// if (calBool) {
// break;
// }
// }
//
//
//
// }public void djSetRotate(double degress) {float tempDegress &#61; (float)degress;djRotateRelativeLayout.setRotation(tempDegress);}// public void djRotateTo(double angle,float delay) {
// double rotateTo &#61; angle;
// double currentAngle &#61; djRotateRelativeLayout.getRotation();
// double subAngle &#61; rotateTo - currentAngle ;
// double perAngle &#61; subAngle / (double)myComputerCalculate * delay;
// System.out.println(perAngle);
// for(int i &#61; 0 ; i
//
// djRotateRelativeLayout.setRotation( ((float)currentAngle&#43; ((float)perAngle * i)));
// }
//
// djRotateRelativeLayout.setRotation((float)rotateTo);
// }double dj_Degrees_To_Radians(double angle) {return angle * 0.01745329252f; }double dj_Radians_To_Degrees(double angle){return angle * 57.29577951f;}double djDistance(PointF v1,PointF v2){double tempX &#61; v1.x - v2.x;double tempY &#61; v1.y - v2.y; return Math.sqrt(tempX * tempX &#43; tempY * tempY);}PointF djCgpSub(PointF v1,PointF v2){PointF temPointF &#61; new PointF(v1.x - v2.x, v1.y - v2.y);return temPointF;}double djToAngle(PointF vF){return Math.atan2(vF.x, vF.y);}public void adjustAngleByAnimation() {System.out.println("djRotateRelativeLayout.getRotation()" &#43; djRotateRelativeLayout.getRotation());// djRotateRelativeLayout.startAnimation(animation);djRotateRelativeLayout.setRotation(destinationAngle);djRotateRelativeLayout.clearAnimation();//currentImageId &#61; (int)djRotateRelativeLayout.getRotation() / (int)djDegressBtn;if (destinationAngle / 360 <1) {int desNum &#61; (int)destinationAngle / 360;destinationAngle -&#61; 360 * desNum;}currentImageId &#61; (int)destinationAngle / (int)djDegressBtn;if (currentImageId <0) {currentImageId &#43;&#61; 6;if (currentImageId <0) {currentImageId &#43;&#61; 6;}}System.out.println("currentImageId" &#43; currentImageId);System.out.println("currentImageId" &#43; currentImageId);//设置图片djRotateRelativeLayout.changeImageResoures(currentImageId);if (currentImageId !&#61; previousimageId) {djRotateRelativeLayout.changeImageDefaultResources(previousimageId);}previousimageId &#61; currentImageId;//变换图片
//djChangeImageView();}&#64;Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubif (600 &#61;&#61; animation.getDuration()) {adjustAngleByAnimation();}if (650 &#61;&#61; animation.getDuration()) {djRotateRelativeLayout.clearAnimation();}}&#64;Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}&#64;Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}&#64;Overridepublic void onClick(View v) {// TODO Auto-generated method stub// switch (v.getId()) {
// case 987654:
// if (calBool) {
// TranslateAnimation translateAnimation &#61; new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
// translateAnimation.setDuration(650);
//
// translateAnimation.setFillAfter(true);
// djRotateRelativeLayout.startAnimation(translateAnimation);
// calBool &#61; false;
//
// }
// else {
// TranslateAnimation translateAnimation &#61; new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0.5f, Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
// translateAnimation.setDuration(650);
//
// translateAnimation.setFillAfter(true);
// djRotateRelativeLayout.startAnimation(translateAnimation);
// calBool &#61; true;
//
//
// }
//
//
//
// break;
//
// default:
// break;
// }}}
package com.ldci.djrotatemenuforandroid;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.AbsoluteLayout.LayoutParams;public class DJRotateAbsoluteView extends AbsoluteLayout{public DJRotateAbsoluteView(Context context,int width,int height) {super(context);// TODO Auto-generated constructor stubAbsoluteLayout.LayoutParams djlp &#61; new AbsoluteLayout.LayoutParams(width, height, width/ 2, 0);DJRotateMenuView djRotateMenuView &#61; new DJRotateMenuView(context, width, height);djRotateMenuView.setLayoutParams(djlp);this.addView(djRotateMenuView);}}
DJRotateMenu.java
package com.ldci.djrotatemenuforandroid;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;public class DJRotateMenu extends RelativeLayout implements OnClickListener,AnimationListener{private ImageView btnImageView;private boolean calBool &#61; true;private DJRotateAbsoluteView djRotateAbsoluteView;public DJRotateMenu(Context context,int width,int height) {super(context);// TODO Auto-generated constructor stubdjRotateAbsoluteView &#61; new DJRotateAbsoluteView(context, width, height);this.addView(djRotateAbsoluteView);btnImageView &#61; new ImageView(context);btnImageView.setId(987654);btnImageView.setOnClickListener(this);Bitmap btnBitmap &#61; ((BitmapDrawable)getResources().getDrawable(R.drawable.ui_navi_triangle)).getBitmap();RelativeLayout.LayoutParams btnlp &#61; new RelativeLayout.LayoutParams(btnBitmap.getWidth(),btnBitmap.getHeight());btnImageView.setImageBitmap(btnBitmap);btnlp.addRule(RelativeLayout.CENTER_HORIZONTAL);btnlp.addRule(RelativeLayout.CENTER_VERTICAL);btnlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);btnImageView.setLayoutParams(btnlp);this.addView(btnImageView);}&#64;Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case 987654:if (calBool) {TranslateAnimation translateAnimation &#61; new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0f, Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);translateAnimation.setDuration(650);// translateAnimation.setAnimationListener(this);translateAnimation.setFillAfter(true);djRotateAbsoluteView.startAnimation(translateAnimation);calBool &#61; false;}else {TranslateAnimation translateAnimation &#61; new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0.5f, Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);translateAnimation.setDuration(650);//translateAnimation.setAnimationListener(this);translateAnimation.setFillAfter(true);djRotateAbsoluteView.startAnimation(translateAnimation);calBool &#61; true;}break;default:break;}}&#64;Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubdjRotateAbsoluteView.clearAnimation();}&#64;Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}&#64;Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}}
package com.ldci.djrotatemenuforandroid;import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;public class MainActivity extends Activity {&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);DisplayMetrics dMetrics &#61; new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dMetrics);int width &#61; dMetrics.widthPixels;int height &#61; dMetrics.heightPixels - 125;System.out.println("&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;" &#43; height);//调用时只用这个方法就OKDJRotateMenu djRotateMenu &#61; new DJRotateMenu(this, width, height);setContentView(djRotateMenu);}&#64;Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
本文完&#xff01;&#xff01;