标签:
android 自定义View(3)圆形View 加速条
分析加速条 一个圆环两种颜色, 加速的速度 圆环的宽度
firstColor
secondColor
speed
circleWidth;
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="firstColor" format="color"></attr> <attr name="secondColor" format="color"></attr> <attr name="circleWidth" format="dimension"></attr> <attr name="speed" format="integer"></attr> <declare-styleable name="CustomProgress"> <attr name="firstColor"></attr> <attr name="secondColor"></attr> <attr name="circleWidth"></attr> <attr name="speed"></attr> </declare-styleable> </resources>
package com.zhy.customview02; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; public class CustomProgress extends View { /** * 第一圈颜色 */ private int firstColor; /** * 第二圈颜色 */ private int secondColor; /** * 圆的宽度 */ private int circleWidth; /** * 速度 */ private int speed; /** * 当前的进度 */ private int curProgress; /** * 画笔 */ private Paint mPaint; boolean isNext = false; public CustomProgress(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgress, defStyle, 0); initView(typedArray); } public CustomProgress(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomProgress(Context context) { super(context, null); } public void initView(TypedArray typedArray){ int n = typedArray.getIndexCount(); for (int i = 0; i < n; i++) { int attr = typedArray.getIndex(i); switch (attr) { case R.styleable.CustomProgress_firstColor: firstColor = typedArray.getColor(attr, Color.GREEN); break; case R.styleable.CustomProgress_secondColor: secondColor = typedArray.getColor(attr, Color.RED); break; case R.styleable.CustomProgress_speed: speed = typedArray.getInt(attr, 20); break; case R.styleable.CustomProgress_circleWidth: circleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics())); break; default: break; } } typedArray.recycle(); mPaint = new Paint(); new Thread(new Runnable(){ @Override public void run() { while(true){ curProgress += speed; if(curProgress == 360){ curProgress = 0; if(!isNext){ isNext = true; }else { isNext = false; } } postInvalidate(); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } @Override protected void onDraw(Canvas canvas) { int center =getWidth()/2; int radius = center - circleWidth/2; mPaint.setStrokeWidth(circleWidth); mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); RectF oval = new RectF(center-radius, center-radius, center+radius, center+radius); //canvas.drawArc(oval, 0, 360, false, mPaint); if(isNext){ mPaint.setColor(firstColor); canvas.drawCircle(center, center, radius, mPaint);//画圆 mPaint.setColor(secondColor); canvas.drawArc(oval, -90, curProgress, false, mPaint); }else{ mPaint.setColor(secondColor); canvas.drawCircle(center, center, radius, mPaint);//画圆 mPaint.setColor(firstColor); canvas.drawArc(oval, -90, curProgress, false, mPaint); } } }
标签:
原文地址:http://www.cnblogs.com/chengbao/p/5656883.html