代码:

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);
}
} }

 

最新文章

  1. 介绍Unreal Engine 4中的接口(Interface)使用C++和蓝图
  2. Python paramiko 模块
  3. URL、表单数据、IP等处理类
  4. BEvent_标准控件Event的用法(案例)(待整理)
  5. 解决Mac下SublimeLinter的Unsafe Characters警告
  6. html的3要素
  7. LoadRunner 参数化详解
  8. (转) 关于成为linux运维工程师需要掌握的技能
  9. 阿里云WinServer2008下配置IIS7支持php
  10. Codeforces 839B Game of the Rows【贪心】
  11. IDEA+&#39;mvn&#39; 不是内部或外部命令
  12. asp.net导出excle
  13. MVC和Web API的区别
  14. Flex 界面初始化 自定义 预加载 类!
  15. 前端代码控制gif图暂停与播放的坑
  16. [Unity3D]Unity3D游戏开发之角色控制漫谈
  17. socketio server推送
  18. 实践:由0到1-无线大数据UX团队的成长
  19. cin读取不知行列数的矩阵以及带逗号的整型数据
  20. 【转】Win32 创建控件风格不是Win XP解决方案

热门文章

  1. XStream环境设置
  2. Java高级进阶:自定义ClassLoader
  3. 用Cython加速Python代码
  4. 2019ICPC南京网络赛A The beautiful values of the palace
  5. Ubuntu下搭建NFS,并在开发板挂载
  6. Java 导出excel进行换行
  7. HIVE常用SQL语句及语法
  8. thinkphp 模板驱动
  9. 2019 hdu多校1
  10. css Sticky footers