属性动画-波纹扩散WaveView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010844304/article/details/86006497

再次复习了属性动画,以前总是直接拿别人的轮子来用,现在学以致用,想自己搞一下。

先上图

实现思路:3个圆不断的扩大半径,并且增加颜色的透明度。

代码:

public class WaveView extends RelativeLayout {

    private Circle circle1;
    private Circle circle2;
    private Circle circle3;
    private AnimatorSet animatorSet;

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_wave_view, this, true);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        circle1 = findViewById(R.id.circle1);
        circle2 = findViewById(R.id.circle2);
        circle3 = findViewById(R.id.circle3);
    }



    public void startAnimation() {
        if (animatorSet == null) {
            PropertyValuesHolder radiusHolder = PropertyValuesHolder.ofInt("radius", 100,200);
            PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofInt("color",255, 0);
            ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(circle1,
                    alphaHolder,radiusHolder);
            objectAnimator.setDuration(1500);
            objectAnimator.setRepeatMode(ValueAnimator.RESTART);
            objectAnimator.setRepeatCount(ValueAnimator.INFINITE);

            ObjectAnimator objectAnimator1 = objectAnimator.clone();
            objectAnimator1.setTarget(circle2);
            objectAnimator1.setStartDelay(500);

            ObjectAnimator objectAnimator2 = objectAnimator.clone();
            objectAnimator2.setTarget(circle3);
            objectAnimator2.setStartDelay(1000);

            animatorSet = new AnimatorSet();
            animatorSet.playTogether(objectAnimator, objectAnimator1, objectAnimator2);
        }
        if (!animatorSet.isRunning()) {
            animatorSet.start();
        }
    }

    public void cancelAnimation() {
        if (animatorSet != null) {
            animatorSet.cancel();
        }
    }


}

关于Circle的代码,这里就不贴出来了。源码可以直接到github中Clone代码。欢迎star

猜你喜欢

转载自blog.csdn.net/u010844304/article/details/86006497