Android动画之直播动效icon

目标效果:
在这里插入图片描述

代码实现:

public class PlayingView extends View {

    private ValueAnimator mValueAnimatorA;
    private ValueAnimator mValueAnimatorB;
    private ValueAnimator mValueAnimatorC;
    private float mWidth;
    private float mHeight;
    private float topA;
    private float topB;
    private float topC;
    private float rectWidth;
    private float perHeight;
    private float radius;
    private Paint mPaint;
    private RectF mRectFA;
    private RectF mRectFB;
    private RectF mRectFC;


    public PlayingView(Context context) {
        super(context);
    }

    public PlayingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(getResources().getColor(R.color.main_color));
    }

    public PlayingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PlayingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        rectWidth = mWidth / 6;
        perHeight = mHeight / 4;
        radius = rectWidth / 2;
        topA = mHeight - perHeight;
        topB = topA;
        topC = topA;
        float margin = rectWidth / 2;
        mRectFA = new RectF(margin, topA, rectWidth + margin, mHeight);
        mRectFB = new RectF(2 * rectWidth + margin, topB, 3 * rectWidth + margin, mHeight);
        mRectFC = new RectF(4 * rectWidth + margin, topC, 5 * rectWidth + margin, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mValueAnimatorA == null) {
            mValueAnimatorA = getValueAnimator();
            mValueAnimatorA.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    topA = (float) animation.getAnimatedValue();
                }
            });
            mValueAnimatorA.setStartDelay(200);
            mValueAnimatorA.start();

            mValueAnimatorB = getValueAnimator();
            mValueAnimatorB.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    topB = (float) animation.getAnimatedValue();
                    PlayingView.this.postInvalidate();
                }
            });
            mValueAnimatorB.start();

            mValueAnimatorC = getValueAnimator();
            mValueAnimatorC.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    topC = (float) animation.getAnimatedValue();
                }
            });
            mValueAnimatorC.setStartDelay(400);
            mValueAnimatorC.start();
        }
        mRectFA.top = topA;
        mRectFB.top = topB;
        mRectFC.top = topC;
        canvas.drawRoundRect(mRectFA, radius, radius, mPaint);
        canvas.drawRoundRect(mRectFB, radius, radius, mPaint);
        canvas.drawRoundRect(mRectFC, radius, radius, mPaint);
    }

    private ValueAnimator getValueAnimator() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(mHeight - perHeight, perHeight);
        valueAnimator.setDuration(600);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        return valueAnimator;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值