Android自定义View实现气泡动画

这篇文章主要为大家详细介绍了Android自定义View实现气泡动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义View实现气泡动画的具体代码,供大家参考,具体内容如下

一、前言

最近有需求制作一个水壶的气泡动画,首先在网上查找了一番,找到了一个文章:Android实现气泡动画

测试了一下发现,如果把它作为子视图的话,会出现小球溢出边界的情况。所以简单的修改了一下。

二、代码

1. 随机移动的气泡

Ball类

 /** * @author jiang yuhang * @date 2021-04-18 19:57 */ class Ball { // 半径 @kotlin.jvm.JvmField var radius = 0 // 圆心 @kotlin.jvm.JvmField var cx = 0f // 圆心 @kotlin.jvm.JvmField var cy = 0f // X轴速度 @kotlin.jvm.JvmField var vx = 0f // Y轴速度 @kotlin.jvm.JvmField var vy = 0f @kotlin.jvm.JvmField var paint: Paint? = null // 移动 fun move() { //向角度的方向移动,偏移圆心 cx += vx cy += vy } fun left(): Int { return (cx - radius).toInt() } fun right(): Int { return (cx + radius).toInt() } fun bottom(): Int { return (cy + radius).toInt() } fun top(): Int { return (cy - radius).toInt() } }

BallView类

 /** * @author jiang yuhang * @date 2021-04-18 19:53 */ public class BallView extends View { private final Random mRandom; private final int mCount = 5;   // 小球个数 private final int minSpeed = 5; // 小球最小移动速度 private final int maxSpeed = 20; // 小球最大移动速度 public Ball[] mBalls;   // 用来保存所有小球的数组 private int maxRadius;  // 小球最大半径 private int minRadius; // 小球最小半径 private int mWidth = 200; private int mHeight = 200; public BallView(final Context context, final AttributeSet attrs) { super(context, attrs); // 初始化所有球(设置颜色和画笔, 初始化移动的角度) this.mRandom = new Random(); final RandomColor randomColor = new RandomColor(); // 随机生成好看的颜色,github开源库。 this.mBalls = new Ball[this.mCount]; for (int i = 0; i = right && speedX > 0) { ball.vx = -ball.vx; } else if (ball.bottom() >= bottom && speedY > 0) { ball.vy = -ball.vy; } } }

2.热水气泡

 /** * @author jiang yuhang * @date 2021-04-18 19:57 */ class Ball { // 半径 @kotlin.jvm.JvmField var radius = 0 // 圆心 @kotlin.jvm.JvmField var cx = 0f // 圆心 @kotlin.jvm.JvmField var cy = 0f // X轴速度 @kotlin.jvm.JvmField var vx = 0f // Y轴速度 @kotlin.jvm.JvmField var vy = 0f @kotlin.jvm.JvmField var paint: Paint? = null // 移动 fun move() { //向角度的方向移动,偏移圆心 cx += vx cy += vy } fun left(): Int { return (cx - radius).toInt() } fun right(): Int { return (cx + radius).toInt() } fun bottom(): Int { return (cy + radius).toInt() } fun top(): Int { return (cy - radius).toInt() } }
 /** * @author jiang yuhang * @date 2021-04-18 19:53 */ public class BallView extends View { final RandomColor randomColor = new RandomColor(); // 随机生成好看的颜色,github开源库。 private final Random mRandom = new Random(); private final int mCount = 5;   // 小球个数 private final int minSpeed = 5; // 小球最小移动速度 private final int maxSpeed = 15; // 小球最大移动速度 public Ball[] mBalls = new Ball[this.mCount];   // 用来保存所有小球的数组 private int maxRadius;  // 小球最大半径 private int minRadius; // 小球最小半径 private int mWidth = 200; private int mHeight = 200; public BallView(final Context context, final AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); this.mWidth = View.resolveSize(this.mWidth, widthMeasureSpec); this.mHeight = View.resolveSize(this.mHeight, heightMeasureSpec); this.setMeasuredDimension(this.mWidth, this.mHeight); this.maxRadius = this.mWidth / 12; this.minRadius = this.maxRadius / 2; // 初始化所有球(设置颜色和画笔, 初始化移动的角度) for (int i = 0; i = right && speedX > 0) { ball.vx = -ball.vx; } } }

以上就是Android自定义View实现气泡动画的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动