自定义控件实现画圆从0到360度的动画效果
public class SelfView extends View {
private final String TAG = "SelfView";
private Paint mPaint;
private int startX = 0;
private int startY = 0;
/**
* 控制动画只执行一次,否则动画会无限制执行下去
*/
private boolean animationGo = false;
private RectF oval;
private ValueAnimator valueAnimator;
public SelfView(Context context) {
super(context);
initView(context,null);
}
public SelfView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context,attrs);
}
public SelfView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context,attrs);
}
private void initView(Context context,AttributeSet attrs){
int color = getResources().getColor(R.color.colorAccent);
if (attrs!=null) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SelfView);
color = array.getColor(R.styleable.SelfView_self_color, getResources().getColor(R.color.colorAccent));
array.recycle();
}
mPaint = new Paint();
mPaint.setColor(color);
mPaint.setTextSize(40);
int paddingTop = getPaddingTop();
oval = new RectF(0,paddingTop,150,150+paddingTop); //用于定义的圆弧的形状和大小的界限
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int targetWidth = widthSize;
int targetHeight = heightSize;
switch (widthMode){
case MeasureSpec.AT_MOST: //wrap_content
targetWidth = 200;
break;
case MeasureSpec.EXACTLY: //match_parent
targetWidth = widthSize;
break;
case MeasureSpec.UNSPECIFIED: //写死大小
targetWidth = widthSize;
break;
}
switch (heightMode){
case MeasureSpec.AT_MOST: //wrap_content
targetHeight = 100;
break;
case MeasureSpec.EXACTLY: //match_parent
targetHeight = heightSize;
break;
case MeasureSpec.UNSPECIFIED: //写死大小
targetHeight = heightSize;
break;
}
setMeasuredDimension(targetWidth,targetHeight);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(oval,0,startX,true,mPaint);
controlAnimation();
}
/**
* 控制展示动画
*/
private void controlAnimation() {
if (!animationGo) {
valueAnimator = ValueAnimator.ofInt(0, 360);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
startX = (int) animation.getAnimatedValue();
invalidate();
// postInvalidate(); //如果使用,就会看不到动画效果,只能看到结果
}
});
valueAnimator.setDuration(1000);
valueAnimator.start();
animationGo = true;
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (valueAnimator!=null && valueAnimator.isRunning()){ //移除控件时,停止动画
this.clearAnimation();
}
Log.e(TAG,"clear ::::");
}
}