观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

代码:

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import net.wt.gate.dev.libs.log.L;

import java.util.concurrent.CopyOnWriteArrayList;

public class DoorBellAnimal extends View {
    private int centerX;
    private int centerY;
    private int startRadius = 10;
    private int endRadius = 250;
    private int startAlpha = 250;

    private CopyOnWriteArrayList<RippleCircle> rippleCircles = new CopyOnWriteArrayList<>();

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

    public DoorBellAnimal(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

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


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        centerX = width / 2;
        centerY = height / 2;
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

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

        for (RippleCircle circle : rippleCircles) {
            circle.draw(canvas);
        }

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startRipple();
        startAnimal();

    }

    private void startRipple() {
        postOnAnimationDelayed(() -> {

            RippleCircle rippleCircle = new RippleCircle();
            rippleCircles.add(rippleCircle);
            startRipple();
        }, 500);


    }

    private ValueAnimator valueAnimator;

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        valueAnimator.cancel();
        valueAnimator.end();
    }

    private void startAnimal() {
        valueAnimator = ValueAnimator.ofInt(0, 60);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setDuration(3000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);

        valueAnimator.addUpdateListener(animation -> postInvalidateOnAnimation());
        valueAnimator.start();

    }


    /**
     * 需要画的 圆圈,需要不断改变半径何透明度
     */
    private class RippleCircle {

        private Paint paint;            //画笔
        private int progress;       //当前进度

        private int perRadius = (endRadius - startRadius) / 60;
        private int perAlpha = startAlpha / 60;

        RippleCircle() {
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.WHITE);
            progress = 0;
        }

        void draw(Canvas canvas) {

            if (paint == null) {
                return;
            }
            if (progress >= 60) {
                rippleCircles.remove(this);
                return;
            }
            progress++;
            float currentAlpha = startAlpha - perAlpha * progress;
            paint.setAlpha((int) (currentAlpha));       //更改透明度
            float current = startRadius + perRadius * progress;
            canvas.drawCircle(centerX, centerY, current, paint);
        }
    }

}

 

 
posted on 2019-10-18 20:51  观心静  阅读(633)  评论(0编辑  收藏  举报