Android 仿酷点圆形菜单

看见一个人写了一个圆形的可以转的菜单,当时看的还是挺模糊的,最后自己模仿的写了一遍,这是源代码

基本思想是这样的

1,把每个图标显示的什么图标确定下来

2,计算每一个点的坐标,

3,在activity就可以正确显示出了

4,当我们移动图标的时候,计算出移动后的图标的角度,知道角度和半径就可以设置它的坐标啊,以它为起始图标,画出剩下的图标

5,实在很模糊,不理解的再议论

启动的Activity

import android.app.Activity; import android.os.Bundle; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new CircleMenuActivity(getApplicationContext(), 150, 150, 100)); } }


菜单生成的类

package rw.circle; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class CircleMenuActivity extends View { private Paint paint=new Paint(); private SeeView[] seeViews; private static final int MenuCount=6;//桌面图标的个数 private int pointX=0,pointY=0;//圆心坐标 private static final int radius=150;//半径 private int DegreeOfPoint;//两个点之间的角度 public CircleMenuActivity(Context context, int x, int y,int radiu) { super(context); paint.setColor(Color.GREEN); paint.setStrokeWidth(2); setBackgroundResource(R.drawable.tt1); pointX=x; pointY=y; radiu=radius; SetPoint(); CalculateCoordinate(); // TODO Auto-generated constructor stub } /* * 这个函数是设置每一个点 */ private void SetPoint() { // TODO Auto-generated method stub seeViews=new SeeView[MenuCount]; //SeeView seeView; int angle=0; DegreeOfPoint=360/MenuCount; for (int i = 0; i <MenuCount; i++) { seeViews[i]=new SeeView(); seeViews[i].angle=angle; seeViews[i].bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.t1+i); angle+=DegreeOfPoint; } } private void resetPointAngle(float x,float y){ int angle=CalculateAngle(x, y); for (int i = 0; i < MenuCount; i++) { seeViews[i].angle=angle; angle+=DegreeOfPoint; } } //计算坐标 private void CalculateCoordinate(){ SeeView seeView; for (int i = 0; i < MenuCount; i++) { seeView=seeViews[i]; seeView.x=pointX+(float)(radius*Math.cos(seeView.angle*Math.PI/180)); seeView.y=pointY+(float)(radius*Math.sin(seeView.angle*Math.PI/180)); } } private int CalculateAngle(float x,float y) { float distance=(float)Math.sqrt((x-pointX)*(x-pointX)+(y-pointY)*(y-pointY)); int degree=(int)(Math.acos((x-pointX)/distance)*180/Math.PI); if (y<pointY) { degree=-degree; } return degree; } public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub //Animation animation; //animation=AnimationUtils.loadAnimation(getContext(), R.anim.scale); resetPointAngle(event.getX(), event.getY()); CalculateCoordinate(); invalidate(); return true; } protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawPoint(pointX, pointY, paint); for (int i = 0; i < MenuCount; i++) { if (!seeViews[i].isVisible) continue; drawIncenter(canvas, seeViews[i].bitmap, seeViews[i].x, seeViews[i].y); } super.onDraw(canvas); } private void drawIncenter(Canvas canvas,Bitmap bitmap,float left,float top) { canvas.drawPoint(left, top, paint); Log.i("-------->", left+""); Log.i("----------------->", bitmap.getWidth()/2+""); Log.i("----------------------->", left-bitmap.getWidth()/2+""); canvas.drawBitmap(bitmap, left+bitmap.getWidth()/2, top+bitmap.getHeight()/2,null); } private class SeeView{ Bitmap bitmap;//每个图标的图 float x,y;//每个图标的坐标 int angle;//角度 boolean isVisible=true;//是否可见,当然可见,要不怎么看见 } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值