Android开发 自定义View_白色圆型涟漪动画View

代码:

package net.wt.gate.dev.function.doorbell;

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;

/**
 * package : net.wt.gate.dev.widget
 * author  : xiaoyunfei
 * date    : 19-7-9  下午5:21
 */
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);
        }
    }

}

 

上一篇:获取Android手机日志


下一篇:iOS13 深色模式 适配