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


    代码:

    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);
            }
        }
    
    }
    
    
    
     
  • 相关阅读:
    SpringBoot exception异常处理机制源码解析
    集群时间同步
    VMware 克隆的相关设置
    MySQL安装
    MogliFS与spring mvc结合简单示例
    MogileFS与spring结合
    MogileFS表说明
    使用Maven编译项目遇到——“maven编码gbk的不可映射字符”解决办法
    IAT HOOK 简单实现
    进程加载_模块隐藏
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/11700715.html
Copyright © 2020-2023  润新知