android利用画布实现环形菜单

来源:互联网 发布:java web 多线程 编辑:程序博客网 时间:2024/05/15 06:17

android中圆环布局会有很多用处,菜单、点选、连接等下面是利用画布实现的

首先定义环形上的对象

package com.bm;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.view.KeyEvent;/** * @author shaohx * */public class Creater {private int width;private int height;public String getName() {return name;}public void setName(String name) {this.name = name;}//坐标以及位图public int x, y;private Bitmap bmpCreater;private String name;//构造函数public Creater(Bitmap bmpCreater,int width,int height) {this.width=width;this.height=height;this.bmpCreater = bmpCreater;x = width / 2 - bmpCreater.getWidth() / 2;y = height/2 - bmpCreater.getHeight()/2;}//绘图函数public void draw(Canvas canvas, Paint paint) {canvas.drawBitmap(bmpCreater, x, y, paint);//canvas.drawCircle(x+bmpCreater.getWidth()/2, y+bmpCreater.getHeight()/2, 120, paint);canvas.drawText("李娜", x+bmpCreater.getWidth()/2-7, y+bmpCreater.getHeight()+10, paint);}}
自定义SurfanceView 当然这里可以用View

@SuppressLint("NewApi")public class MySurfaceView extends SurfaceView implements Callback, Runnable {private SurfaceHolder sfh;private Paint paint;private Thread th;private boolean flag;private Canvas canvas;public static int screenW, screenH;private Context context;private Reciver reciver;private List<Reciver> list;//声明一个Resources实例便于加载图片private Resources res = this.getResources();/** * SurfaceView初始化函数 */public MySurfaceView(Context context,List recivers) {super(context);this.context=context;this.list=recivers;sfh = this.getHolder();sfh.addCallback(this);setZOrderOnTop(true);  sfh.setFormat(PixelFormat.TRANSPARENT);//设置背景透明  paint = new Paint();paint.setColor(Color.BLACK);paint.setAntiAlias(true);setFocusable(true);//this.setZOrderOnTop(true);//自定义视图位于xml布局上层////;}/** * SurfaceView视图创建,响应此函数 */@Overridepublic void surfaceCreated(SurfaceHolder holder) {screenW = this.getWidth();screenH = this.getHeight();flag = true;//实例线程th = new Thread(this);//启动线程th.start();}/** * 绘图 */public void myDraw() {try {canvas = sfh.lockCanvas();if (canvas != null) {canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);//清除屏幕  //接收者for(int i=0;i<list.size();i++){list.get(i).draw(canvas, paint);}reciver.draw(canvas, paint);}} catch (Exception e) {// TODO: handle exception} finally {if (canvas != null)sfh.unlockCanvasAndPost(canvas);}}/** * 触屏事件监听 */@Overridepublic boolean onTouchEvent(MotionEvent event) {int x=(int) event.getX();int y=(int) event.getY();if(event.getAction()==MotionEvent.ACTION_DOWN){for(int i=0;i<list.size();i++){if(f){//if(flag2){//list.get(i).setFlag(false);//}else{//list.get(i).setFlag(true);//}list.get(i).flag=list.get(i).flag==false?true:false;flag2=list.get(i).getFlag();System.out.println("flage2点击后"+i+flag2);if(flag2){list.get(i).setBmpEnemy(BitmapFactory.decodeResource(res, R.drawable.favicon80));list.get(i).setIsClick(true);}else{list.get(i).setBmpEnemy(BitmapFactory.decodeResource(res, R.drawable.favicon50));list.get(i).setIsClick(true);}}else{if(list.get(i).getFlag()){list.get(i).setBmpEnemy(BitmapFactory.decodeResource(res, R.drawable.favicon80));list.get(i).setIsClick(false);}else{list.get(i).setBmpEnemy(BitmapFactory.decodeResource(res, R.drawable.favicon50));list.get(i).setIsClick(false);}} }}return false;}@Overridepublic void run() {while (flag) {long start = System.currentTimeMillis();myDraw();long end = System.currentTimeMillis();try {if (end - start < 20) {Thread.sleep(20 - (end - start));}} catch (InterruptedException e) {e.printStackTrace();}}}/** * SurfaceView视图状态发生改变,响应此函数 */@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}/** * SurfaceView视图消亡时,响应此函数 */@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {flag = false;}}

下边是在Activity里边的调用

得到环形上每个对象之间的角度

private int getAngle(int size){int angle=(int) Math.round(360/size);return angle;}

调用
//得到每个对象之间的角度int angle=getAngle(8);
//计算坐标
for(int i = -150; i < 210;){<span style="white-space:pre"></span>// 避免第一个和最后一个重合<span style="white-space:pre"></span>if (210 - i < angle) {<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>int x = (int) (mPointX + (r * Math.cos(i * Math.PI<span style="white-space:pre"></span>/ 180)));<span style="white-space:pre"></span>int y = (int) (mPointY + (float) (r * Math.sin(i<span style="white-space:pre"></span>* Math.PI / 180)));<span style="white-space:pre"></span>//Reciver对象<span style="white-space:pre"></span>Bitmap btpReciver=BitmapFactory.decodeResource(getResources(), R.drawable.favicon50_black);<span style="white-space:pre"></span>Reciver recivera=new Reciver(btpReciver,list.get(j).getUserName(), x, y);<span style="white-space:pre"></span><span style="white-space:pre"></span>mList.add(recivera);<span style="white-space:pre"></span>i = i + angle;<span style="white-space:pre"></span>j++;<span style="white-space:pre"></span>if (j == c.size()) {<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}
mList是要显示成环形的对象集合





0 0
原创粉丝点击