Android View分析(四)—— 自定义View

这篇博客探讨了如何在Android中自定义View来创建一个动态的圆形进度动画,详细讲解了从0到360度画圆的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义控件实现画圆从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 ::::");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值