android利用画布实现环形菜单

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 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视图创建,响应此函数
	 */
	@Override
	public 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

下边是在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;){
	// 避免第一个和最后一个重合
	if (210 - i < angle) {
		break;
	}
	int x = (int) (mPointX + (r * Math.cos(i * Math.PI
			/ 180)));
	int y = (int) (mPointY + (float) (r * Math.sin(i
		* Math.PI / 180)));
	//Reciver对象
	Bitmap btpReciver=BitmapFactory.decodeResource(getResources(), R.drawable.favicon50_black);
	Reciver recivera=new Reciver(btpReciver,list.get(j).getUserName(), x, y);
					
	mList.add(recivera);
	i = i + angle;
	j++;
	if (j == c.size()) {
		break;
	}
	}
mList是要显示成环形的对象集合





你可能感兴趣的:(android,android开发记录)