一:自定义属性
<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>
二:与生命周期进行绑定时,不能强制退出线程,故提供一个一个状态,改变状态值或者更新状态值,让线程停止
public abstract class FrameThread extends Thread {
protected boolean running = false;
public FrameThread() {
}
public void run() {
this.onPreExecute();
while (this.running) {
this.doWork();
}
this.onPostExecute();
}
public void onPostExecute() {
}
public void onPreExecute() {
}
protected abstract void doWork();
public synchronized void start() {
this.running = true;
super.start();
}
public void shutDown() {
this.running = false;
}
public boolean isRunning() {
return this.running;
}
}
public class CircleView extends View {
private int mFirstColor;
private int mSecondColor;
private int mCircleWidth;
private Paint mPaint;
private int mProgress;
private int mSpeed;
FrameThread myThred;
private boolean isNext = false;
public CircleView(Context context) {
this(context, null);
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//属性获取有两种方式
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.CustomProgressBar_firstColor:
mFirstColor = a.getColor(attr, Color.GREEN);
break;
case R.styleable.CustomProgressBar_secondColor:
mSecondColor = a.getColor(attr, Color.RED);
break;
case R.styleable.CustomProgressBar_circleWidth:
mCircleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
break;
case R.styleable.CustomProgressBar_speed:
mSpeed = a.getInt(attr, 20);
break;
}
}
a.recycle();
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
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);
canvas.drawCircle(centre, centre, radius, mPaint);
mPaint.setColor(mSecondColor);
canvas.drawArc(oval, -90, mProgress, false, mPaint);
} else {
mPaint.setColor(mSecondColor);
canvas.drawCircle(centre, centre, radius, mPaint);
mPaint.setColor(mFirstColor);
canvas.drawArc(oval, -90, mProgress, false, mPaint);
}
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (View.VISIBLE== visibility) {
if (myThred == null) {
myThred = new CircleThred();
myThred.start();
}
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
if (myThred.isAlive()) {
myThred.shutDown();
}
super.onDetachedFromWindow();
}
class CircleThred extends FrameThread {
@Override
protected void doWork() {
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();
}
}
}
}
}
三:线程与VIEW生命周期进行绑定
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
onAttachedToWindow运行在onResume之后<可以理解为进入到view时执行>
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
}
在执行过程中:会执行两次,第一次状态为INVISABLE,第二次状态更新为VISABLE int visibility>–4–>0
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
退出时执行