android 高仿圆形菜单
看见一个人写了一个圆形的可以转的菜单,当时看的还是挺模糊的,最后自己模仿的写了一遍,这是源代码
基本思想是这样的
1,把每个图标显示的什么图标确定下来
2,计算每一个点的坐标,
3,在activity就可以正确显示出了
4,当我们移动图标的时候,计算出移动后的图标的角度,知道角度和半径就可以设置它的坐标啊,以它为起始图标,画出剩下的图标
5,实在很模糊,不理解的再议论
启动的Activity
1.import Android.app.Activity;
2.import android.os.Bundle; 3.
4.public class Main extends Activity {
5. /** Called when the activity is first created. */
6. @Override
7. public void onCreate(Bundle savedInstanceState) {
8. super.onCreate(savedInstanceState);
9. setContentView(new CircleMenuActivity(getApplicationContext(), 150, 150, 100)); 10. }
11.}
菜单生成的类
1.package rw.circle;
2.
3.
4.import android.content.Context;
5.import android.graphics.Bitmap;
6.import android.graphics.BitmapFactory;
7.import android.graphics.Canvas;
8.import android.graphics.Color;
9.import android.graphics.Paint;
10.import android.util.Log;
11.import android.view.MotionEvent;
12.import android.view.View;
14.public class CircleMenuActivity extends View {
16.private Paint paint=new Paint();
17.private SeeView[] seeViews;
18.private static final int MenuCount=6;//桌面图标的个数
19.private int pointX=0,pointY=0;//圆心坐标
20.private static final int radius=150;//半径
21.private int DegreeOfPoint;//两个点之间的角度
22. public CircleMenuActivity(Context context, int x, int y,int radiu) {
23. super(context);
24. paint.setColor(Color.GREEN);
25. paint.setStrokeWidth(2);
26. setBackgroundResource(R.drawable.tt1);
27. pointX=x;
28. pointY=y;
29. radiu=radius;
30. SetPoint();
31. CalculateCoordinate();
32. // TODO Auto-generated constructor stub
33. }
34. /* 35. * 这个函数是设置每一个点
36. */
37. private void SetPoint() {
38. // TODO Auto-generated method stub
39. seeViews=new SeeView[MenuCount];
40. //SeeView seeView;
41. int angle=0;
42. DegreeOfPoint=360/MenuCount;
43. for (int i = 0; i <MenuCount; i++) {
44. seeViews[i]=new SeeView();
45. seeViews[i].angle=angle;
46. seeViews[i].bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.t1+i);
47. angle+=DegreeOfPoint;
48. }
49. }
50. private void resetPointAngle(float x,float y){
51. int angle=CalculateAngle(x, y);
52. for (int i = 0; i < MenuCount; i++) {
53. seeViews[i].angle=angle;
54. angle+=DegreeOfPoint;
55. }
56. }
57. //计算坐标 58. private void CalculateCoordinate(){
59. SeeView seeView;
60. for (int i = 0; i < MenuCount; i++) {
61. seeView=seeViews[i];
62. seeView.x=pointX+(float)(radius*Math.cos(seeView.angle*Math.PI/180));
63. seeView.y=pointY+(float)(radius*Math.sin(seeView.angle*Math.PI/180));
64. }
65. }
66. private int CalculateAngle(float x,float y) {
67. float distance=(float)Math.sqrt((x-pointX)*(x-pointX)+(y-pointY)*(y-pointY));
68. int degree=(int)(Math.acos((x-pointX)/distance)*180/Math.PI);
69. if (y<pointY) {
70. degree=-degree;
71. }
72. return degree;
73.}
74.
75. public boolean dispatchTouchEvent(MotionEvent event) {
76. // TODO Auto-generated method stub
77. //Animation animation;
78. //animation=AnimationUtils.loadAnimation(getContext(), R.anim.scale);
79. resetPointAngle(event.getX(), event.getY());
80. CalculateCoordinate();
81. invalidate();
82. return true;
83. }
84.
85.
86. protected void onDraw(Canvas canvas) {
87. // TODO Auto-generated method stub
88. canvas.drawPoint(pointX, pointY, paint);
89. for (int i = 0; i < MenuCount; i++) {
90. if (!seeViews[i].isVisible)
91. continue;
92. drawIncenter(canvas, seeViews[i].bitmap, seeViews[i].x, seeViews[i].y);
93. }
94. super.onDraw(canvas);
95. }
96.
97.private void drawIncenter(Canvas canvas,Bitmap bitmap,float left,float top)
98.{
99. canvas.drawPoint(left, top, paint);
100. Log.i("-------->", left+"");
101. Log.i("----------------->", bitmap.getWidth()/2+"");
102. Log.i("----------------------->", left-bitmap.getWidth()/2+"");
103. canvas.drawBitmap(bitmap, left+bitmap.getWidth()/2, top+bitmap.getHeight()/2,null);
104.}
105.private class SeeView{
106. Bitmap bitmap;//每个图标的图
107. float x,y;//每个图标的坐标
108. int angle;//角度
109. boolean isVisible=true;//是否可见,当然可见,要不怎么看见
110. }
111.}
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。