效果图
单个图片的类
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 stub
imageView = 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);
}
@Override
public void onClick(View v) {
Log.i("image", "1111111111111111111111111111" + v.getId());
// TODO Auto-generated method stub
switch (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<DJItemImageView> djArrayList;
public DJRotateRelativeLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
//初始化一个数字
djArrayList = new ArrayList<DJItemImageView>();
this.setBackgroundColor(Color.TRANSPARENT);
RelativeLayout.LayoutParams bglp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
bglp.addRule(RelativeLayout.CENTER_HORIZONTAL);
bglp.addRule(RelativeLayout.CENTER_VERTICAL);
this.setLayoutParams(bglp);
//this.setOnTouchListener(this);
ImageView bgImageViewimageView = new ImageView(context);
bgImageViewimageView.setLayoutParams(bglp);
Bitmap bgbitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ui_navi_bg)).getBitmap();
bgImageViewimageView.setImageBitmap(bgbitmap);
this.addView(bgImageViewimageView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// lp.addRule(CENTER_VERTICAL);
this.setLayoutParams(lp);
for (int i = 1; i < 7; i++) {
DJItemImageView djItemImageView = new DJItemImageView(context);
djItemImageView.setRotation(240- (60 * i));
djArrayList.add(djItemImageView);
int tagItem = i + 1000;
djItemImageView.setImageTag(tagItem);
djItemImageView.djsetImageResources(imageIdsDefault[i - 1]);
System.out.println(tagItem);
this.addView(djItemImageView);
}
}
public void changeImageDefaultResources(int imageId) {
DJItemImageView inDjItemImageView = djArrayList.get(imageId);
inDjItemImageView.djsetImageResources(imageIdsDefault[imageId]);
}
public void changeImageResoures(int imageId) {
System.out.println(imageId);
DJItemImageView inDjItemImageView = djArrayList.get(imageId);
inDjItemImageView.djsetImageResources(imageIdsIn[imageId]);
}
}
处理旋转功能的类
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 = false;
}
public void djTouchBegin(PointF touchPointF) {
double distance = djDistance(touchPointF, this.centerPointF);
//获取当前图片ID
System.out.println("distance " + distance);
}
public void djTouchMove(PointF touchPointF) {
PointF previousLocationF = previousPointF;
//角度差
PointF previousCGP = djCgpSub(previousLocationF, centerPointF);
double previousVector = djToAngle(previousCGP);
previousDegress = dj_Radians_To_Degrees(previousVector);
PointF nowLocation = touchPointF;
PointF nowCGP = djCgpSub(nowLocation, centerPointF);
double nowVector = djToAngle(nowCGP);
nowDegress = dj_Radians_To_Degrees(nowVector);
//在这里判断角度
angle = -(nowDegress - previousDegress);
currentDegree = djRotateRelativeLayout.getRotation();
//degressNum += angle;
angle += currentDegree;
djSetRotate(angle);
//djChangeImageView();
}
public void djChangeImageView() {
float tempCurrentAngle = djRotateRelativeLayout.getRotation();
System.out.println("tempCurrentAngle" + tempCurrentAngle);
double currentAngle = djRotateRelativeLayout.getRotation();
double currentSub;
currentSub = currentAngle / djDegressBtn;
int temp = (int)currentSub;
currentSub = currentAngle -djDegressBtn * temp;
if (currentSub > 0)
{
if (temp != previousImageNum) {
if (currentSub > djDegressBtn / 2) {
currentImageId += 1;
}
}
}else if (currentSub < 0) {
if (temp != previousImageNum) {
currentImageId -= 1;
}
}
previousImageNum = temp;
if (currentImageId < 0) {
currentImageId += 6;
}
// if (tempCurrentAngle > 0) {
//
// tempCurrentAngle += djDegressBtn / 2;
// }
// else if(tempCurrentAngle < 0){
// tempCurrentAngle -= djDegressBtn / 2;
// }
// int btnTypeNum = (int)((tempCurrentAngle + djDegressBtn / 2) / (djDegressBtn));
// btnTypeNum = (int)tempCurrentAngle / (int)djDegressBtn;
//
// float currentSub = tempCurrentAngle - (float)djDegressBtn * btnTypeNum;
//
// System.out.println("btnTypeNum" + btnTypeNum);
// //btnTypeNum = 6-btnTypeNum;
// System.out.println("btnTypeNum" + btnTypeNum);
}
public void djTouchEnd(float touchX,float touchY) {
calculateDegress();
//djChangeImageView();
}
public void calculateDegress() {
double currentAngle = djRotateRelativeLayout.getRotation();
double currentSub;
currentSub = currentAngle / djDegressBtn;
int temp = (int)currentSub;
currentSub = currentAngle -djDegressBtn * temp;
System.out.println("currentSub = " + currentSub);
if (currentSub > 0) {
if (currentSub > djDegressBtn / 2) {
currentSub = djDegressBtn - currentSub;
}
else if (currentSub < djDegressBtn / 2) {
currentSub = -currentSub;
}
}
else if (currentSub < 0) {
if (currentSub > -djDegressBtn / 2) {
currentSub = - currentSub;
}
else if (currentSub < - djDegressBtn / 2) {
currentSub = -djDegressBtn - currentSub;
}
}
djSetRotateAnimation(currentSub);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float tempTouchX = event.getX();
float tempTouchY = event.getY();
PointF pointF = new PointF(tempTouchX, tempTouchY);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
djTouchBegin(pointF);
previousPointF = pointF;
break;
case MotionEvent.ACTION_MOVE:
djTouchMove(pointF);
previousPointF = pointF;
break;
case MotionEvent.ACTION_UP:
djTouchEnd(tempTouchX, tempTouchY);
break;
default:
break;
}
return true;
}
public void djSetRotateAnimation(double angle) {
float tempCurrentAngle = djRotateRelativeLayout.getRotation();
destinationAngle = tempCurrentAngle + (float)angle;
//destinationAngle = tempCurrentAngle + (float)30.0000;
System.out.println("tempCurrentAngle" + tempCurrentAngle + "destinationAngle" + destinationAngle );
RotateAnimation rotateAnimation = new RotateAnimation((float)0.0,(float)angle,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setAnimationListener(this);
rotateAnimation.setDuration(600);
rotateAnimation.setFillAfter(true);
rotateAnimation.setFillBefore(true);
rotateAnimation.setFillEnabled(true);
rotateAnimation.setDetachWallpaper(true);
djRotateRelativeLayout.startAnimation(rotateAnimation);
}
// public void getMyComputerCalculate() {
//
// Timer timer = new Timer();
// timer.schedule(new TimerTask() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// calBool = true;
// }
// }, 1);
//
//
// while (true) {
// myComputerCalculate++;
// if (calBool) {
// break;
// }
// }
//
//
//
// }
public void djSetRotate(double degress) {
float tempDegress = (float)degress;
djRotateRelativeLayout.setRotation(tempDegress);
}
// public void djRotateTo(double angle,float delay) {
// double rotateTo = angle;
// double currentAngle = djRotateRelativeLayout.getRotation();
// double subAngle = rotateTo - currentAngle ;
// double perAngle = subAngle / (double)myComputerCalculate * delay;
// System.out.println(perAngle);
// for(int i = 0 ; i< myComputerCalculate;i++)
// {
//
// djRotateRelativeLayout.setRotation( ((float)currentAngle+ ((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 = v1.x - v2.x;
double tempY = v1.y - v2.y;
return Math.sqrt(tempX * tempX + tempY * tempY);
}
PointF djCgpSub(PointF v1,PointF v2)
{
PointF temPointF = 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()" + djRotateRelativeLayout.getRotation());
// djRotateRelativeLayout.startAnimation(animation);
djRotateRelativeLayout.setRotation(destinationAngle);
djRotateRelativeLayout.clearAnimation();
//currentImageId = (int)djRotateRelativeLayout.getRotation() / (int)djDegressBtn;
if (destinationAngle / 360 < 1) {
int desNum = (int)destinationAngle / 360;
destinationAngle -= 360 * desNum;
}
currentImageId = (int)destinationAngle / (int)djDegressBtn;
if (currentImageId < 0) {
currentImageId += 6;
if (currentImageId < 0) {
currentImageId += 6;
}
}
System.out.println("currentImageId" + currentImageId);
System.out.println("currentImageId" + currentImageId);
//设置图片
djRotateRelativeLayout.changeImageResoures(currentImageId);
if (currentImageId != previousimageId) {
djRotateRelativeLayout.changeImageDefaultResources(previousimageId);
}
previousimageId = currentImageId;
//变换图片
//djChangeImageView();
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (600 == animation.getDuration()) {
adjustAngleByAnimation();
}
if (650 == animation.getDuration()) {
djRotateRelativeLayout.clearAnimation();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// switch (v.getId()) {
// case 987654:
// if (calBool) {
// TranslateAnimation translateAnimation = 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 = false;
//
// }
// else {
// TranslateAnimation translateAnimation = 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 = true;
//
//
// }
//
//
//
// break;
//
// default:
// break;
// }
}
}
DJRotateAbsoluteView.java
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 stub
AbsoluteLayout.LayoutParams djlp = new AbsoluteLayout.LayoutParams(width, height, width/ 2, 0);
DJRotateMenuView djRotateMenuView = 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 = true;
private DJRotateAbsoluteView djRotateAbsoluteView;
public DJRotateMenu(Context context,int width,int height) {
super(context);
// TODO Auto-generated constructor stub
djRotateAbsoluteView = new DJRotateAbsoluteView(context, width, height);
this.addView(djRotateAbsoluteView);
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);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case 987654:
if (calBool) {
TranslateAnimation translateAnimation = 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 = false;
}
else {
TranslateAnimation translateAnimation = 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 = true;
}
break;
default:
break;
}
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
djRotateAbsoluteView.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
MainActivity.java
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dMetrics);
int width = dMetrics.widthPixels;
int height = dMetrics.heightPixels - 125;
System.out.println("==========" + height);
//调用时只用这个方法就OK
DJRotateMenu djRotateMenu = new DJRotateMenu(this, width, height);
setContentView(djRotateMenu);
}
@Override
public 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;
}
}
本文完!!