Android圆环形自定义进度条控件的绘制

<span style="font-size:24px;">效果图如下:</span>


以下是代码详细:

package org.example.fanqietime;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class OvalTimeView extends SurfaceView implements SurfaceHolder.Callback {

	public OvalTimeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		holder = this.getHolder();
		holder.addCallback(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		ispause = false;
	}

	@SuppressLint("WrongCall")
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		init();
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				onDraw();
			}
		}).start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		ispause = true;
	}

	private int width;
	private int height;
	private int radius,radius2;
	private int zxX = 0;
	private int zxY = 0;
	public SurfaceHolder holder;
	private boolean ispause = false;
	private float textSixe1,textSixe2;

	public void init(){
		width = getWidth();
		height = getHeight();

		if(width >= height)
			radius = height / 2 - 10;
		else
			radius = width / 2 - 10;

		zxX = width / 2;
		zxY = height / 2;
		radius2 = radius - radius / 20;
		textSixe1 = (float) (radius * 1.0 / 2.5);
		textSixe2 = (float) (radius * 1.0 / 6.3);
	}

	public float angle = 0;
	public boolean istop = true;
	public int sleep = 1000;
	public int longtime = 0;
	public int finaltime = 0;
	public int bgcolor;
	private Handler thandler;

	public void setTHandler(Handler thandler){
		this.thandler = thandler;
	}
	
	public void setLongtime(int finaltime,int currentlong, int color){
		this.longtime = finaltime - currentlong;
		this.finaltime = finaltime;
		bgcolor = color;
		angle = (float) (angle + (360 * 1.0 / finaltime) * currentlong);
		istop = false;
	}

	public void startDraw(Canvas canvas, SurfaceHolder holder){
		if(!ispause){
			canvas = holder.lockCanvas(null);//锁定画布,一般在锁定后就可以通过其返回的画布对象Canvas,在其上面画图等操作了。
			canvas.drawColor(bgcolor);
			drawWOval(canvas);
			drawSOval(canvas, angle);
			drawNOval(canvas);
			drawNText(canvas);
			holder.unlockCanvasAndPost(canvas);//结束锁定画图,并提交改变。
		}
	}

	//画外圆
	public void drawWOval(Canvas canvas){
		Paint paint = new Paint();
		paint.setColor(Color.parseColor("#FFFFFF"));
		paint.setStrokeWidth(2);// 设置画笔粗细
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);  
		canvas.drawOval(new RectF(zxX - radius, zxY - radius, zxX + radius, zxY + radius), paint);
	}
	//画内圆
	public void drawNOval(Canvas canvas){
		Paint paint = new Paint();
		paint.setColor(bgcolor);
		paint.setStrokeWidth(2);// 设置画笔粗细
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);  
		canvas.drawOval(new RectF(zxX - radius2, zxY - radius2, zxX + radius2, zxY + radius2), paint);
	}
	//画扇形圆
	public void drawSOval(Canvas canvas, float angle){
		Paint paint = new Paint();
		paint.setColor(bgcolor);
		paint.setStrokeWidth(2);// 设置画笔粗细
		paint.setStyle(Paint.Style.FILL);
		paint.setAlpha(50);
		paint.setAntiAlias(true);
		canvas.drawArc(new RectF(zxX - radius, zxY - radius, zxX + radius, zxY + radius), 270, angle, true, paint);
	}
	//画外圆
	public void drawNText(Canvas canvas){
		Paint paint = new Paint();
		paint.setColor(Color.parseColor("#FFFFFF"));
		paint.setStrokeWidth(2);// 设置画笔粗细
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);
		paint.setTextSize(textSixe1);
		//获取文字宽度高度
		String str = CommonUtil.SecondsToTimeString(longtime);
		Rect rect = new Rect();
		paint.getTextBounds(str, 0, str.length(), rect);
		int w = rect.width();
		int h = rect.height();
		canvas.drawText(str, zxX - (w / 2), zxY + (h / 2), paint);
		//画工作名称
		str = AppApplication.getInstances().getWork().getWorkname();
		paint.setTextSize(textSixe2);
		paint.getTextBounds(str, 0, str.length(), rect);
		int ww = rect.width();
		int hw = rect.height();
		canvas.drawText(str, zxX - (ww / 2), zxY - hw * 2, paint);
	}

	public void onDraw(){
		while(true){
			if(!istop){
				Canvas canvas = null;
				synchronized (holder){
					try {
						startDraw(canvas , holder);
						angle = (float) (angle + (360 * 1.0 / finaltime));
						if(angle >= 360 || longtime < 0){
							thandler.sendEmptyMessage(1001);
							break;
						}
						Thread.sleep(sleep);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					longtime --;						//工作时间递减
				}
			}else{
				Canvas canvas = null;
				startDraw(canvas , holder);
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值