写下这个是为了以后方便自己查看。
大神链接http://my.youkuaiyun.com/lmj623565791
自定义一个圆环转动器,
前面步奏大同小异,直接粘代码。
这个是控件的属性。
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="firstColor" format="color"/> <attr name="secondColor" format="color"/> <attr name="circleWidth" format="dimension"/> <attr name="speed" format="integer"/> <declare-styleable name="CustomProgressBar"> <attr name="firstColor"/> <attr name="secondColor"/> <attr name="circleWidth"/> <attr name="speed"/> </declare-styleable> </resources>建立一个控件类
在构造函数里面实现获取属性,判断画圆环的线程
获取属性的代码
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0); int n = typedArray.getIndexCount(); for (int i=0;i<n;i++) { //获得ID int aar = typedArray.getIndex(i); switch (aar) { case R.styleable.CustomProgressBar_firstColor: mFirstColor = typedArray.getColor(aar, 0); break; case R.styleable.CustomProgressBar_secondColor: mSecondColor = typedArray.getColor(aar, 0); break; case R.styleable.CustomProgressBar_circleWidth: mCircleWidth =typedArray.getDimensionPixelSize(aar, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics())); break; case R.styleable.CustomProgressBar_speed: mSpeed = typedArray.getInt(aar,20); break; } }
画圆弧变化的线程
new Thread(new Runnable() { @Override public void run() { while (true) { mProgress++; if (mProgress == 360) { mProgress = 0; if (!isNext) isNext = true; else isNext = false; } //刷新界面 postInvalidate(); try { Thread.sleep(mSpeed); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start();
最后在onDraw方法里面实现画圆弧
@Override protected void onDraw(Canvas canvas) { //获取圆心的X坐标 int centre = getWidth()/2; //圆的半径 int radius = centre - mCircleWidth/2; //圆环的宽度 mPaint.setStrokeWidth(mCircleWidth); //抗锯齿 mPaint.setAntiAlias(true); //设置空心 mPaint.setStyle(Paint.Style.STROKE); // 用于定义的圆弧的形状和大小的界限 RectF oval = new RectF(centre-radius,centre-radius,centre+radius,centre+radius); if(isNext){ mPaint.setColor(mFirstColor); // 设置圆环的颜色 }else { mPaint.setColor(mSecondColor); // 设置圆环的颜色 } canvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环 if(isNext){ mPaint.setColor(mSecondColor); // 设置圆环的颜色 }else { mPaint.setColor(mFirstColor); // 设置圆环的颜色 } //mPaint.setColor(mSecondColor); // 设置圆环的颜色 canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根据进度画圆弧 }
完整的一个自定义控件的代码
/** * Created by Administrator on 2017/9/19. */ public class CustomProgressBar extends View { /** * 第一圈的颜色 */ private int mFirstColor; /** * 第二圈的颜色 */ private int mSecondColor; /** * 圈的宽度 */ private int mCircleWidth; /** * 画笔 */ private Paint mPaint; /** * 当前进度 */ private int mProgress; /** * 速度 */ private int mSpeed; private boolean isNext; public CustomProgressBar(Context context) { this(context,null); } public CustomProgressBar(Context context, AttributeSet attrs) { this(context,attrs,0); } public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0); int n = typedArray.getIndexCount(); for (int i=0;i<n;i++) { //获得ID int aar = typedArray.getIndex(i); switch (aar) { case R.styleable.CustomProgressBar_firstColor: mFirstColor = typedArray.getColor(aar, 0); break; case R.styleable.CustomProgressBar_secondColor: mSecondColor = typedArray.getColor(aar, 0); break; case R.styleable.CustomProgressBar_circleWidth: mCircleWidth =typedArray.getDimensionPixelSize(aar, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics())); break; case R.styleable.CustomProgressBar_speed: mSpeed = typedArray.getInt(aar,20); break; } } typedArray.recycle(); mPaint = new Paint(); new Thread(new Runnable() { @Override public void run() { while (true) { mProgress++; if (mProgress == 360) { mProgress = 0; if (!isNext) isNext = true; else isNext = false; } //刷新界面 postInvalidate(); try { Thread.sleep(mSpeed); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override protected void onDraw(Canvas canvas) { //获取圆心的X坐标 int centre = getWidth()/2; //圆的半径 int radius = centre - mCircleWidth/2; //圆环的宽度 mPaint.setStrokeWidth(mCircleWidth); //抗锯齿 mPaint.setAntiAlias(true); //设置空心 mPaint.setStyle(Paint.Style.STROKE); // 用于定义的圆弧的形状和大小的界限 RectF oval = new RectF(centre-radius,centre-radius,centre+radius,centre+radius); if(isNext){ mPaint.setColor(mFirstColor); // 设置圆环的颜色 }else { mPaint.setColor(mSecondColor); // 设置圆环的颜色 } canvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环 if(isNext){ mPaint.setColor(mSecondColor); // 设置圆环的颜色 }else { mPaint.setColor(mFirstColor); // 设置圆环的颜色 } //mPaint.setColor(mSecondColor); // 设置圆环的颜色 canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根据进度画圆弧 } }