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