Android自定義View實(shí)現(xiàn)氣泡動(dòng)畫
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)氣泡動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
一、前言
最近有需求制作一個(gè)水壺的氣泡動(dòng)畫,首先在網(wǎng)上查找了一番,找到了一個(gè)文章:Android實(shí)現(xiàn)氣泡動(dòng)畫
測試了一下發(fā)現(xiàn),如果把它作為子視圖的話,會(huì)出現(xiàn)小球溢出邊界的情況。所以簡單的修改了一下。
二、代碼
1. 隨機(jī)移動(dòng)的氣泡
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
// 移動(dòng)
fun move() {
//向角度的方向移動(dòng),偏移圓心
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; // 小球個(gè)數(shù)
private final int minSpeed = 5; // 小球最小移動(dòng)速度
private final int maxSpeed = 20; // 小球最大移動(dòng)速度
public Ball[] mBalls; // 用來保存所有小球的數(shù)組
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);
// 初始化所有球(設(shè)置顏色和畫筆, 初始化移動(dòng)的角度)
this.mRandom = new Random();
final RandomColor randomColor = new RandomColor(); // 隨機(jī)生成好看的顏色,github開源庫。
this.mBalls = new Ball[this.mCount];
for (int i = 0; i < this.mCount; i++) {
this.mBalls[i] = new Ball();
// 設(shè)置畫筆
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(randomColor.randomColor());
paint.setStyle(Paint.Style.FILL);
paint.setAlpha(180);
paint.setStrokeWidth(0);
// 設(shè)置速度
final float speedX = (this.mRandom.nextInt(this.maxSpeed - this.minSpeed + 1) + 5) / 10f;
final float speedY = (this.mRandom.nextInt(this.maxSpeed - this.minSpeed + 1) + 5) / 10f;
this.mBalls[i].paint = paint;
this.mBalls[i].vx = this.mRandom.nextBoolean() ? speedX : -speedX;
this.mBalls[i].vy = this.mRandom.nextBoolean() ? speedY : -speedY;
}
}
@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 (Ball mBall : this.mBalls) {
mBall.radius = this.mRandom.nextInt(this.maxRadius + 1 - this.minRadius) + this.minRadius;
// 初始化圓心的位置, x最小為 radius, 最大為mwidth- radius
mBall.cx = this.mRandom.nextInt(this.mWidth - mBall.radius) + mBall.radius;
mBall.cy = this.mRandom.nextInt(this.mHeight - mBall.radius) + mBall.radius;
}
}
@Override
protected void onDraw(final Canvas canvas) {
final long startTime = System.currentTimeMillis();
// 先畫出所有圓
for (int i = 0; i < this.mCount; i++) {
final Ball ball = this.mBalls[i];
canvas.drawCircle(ball.cx, ball.cy, ball.radius, ball.paint);
}
// 球碰撞邊界
for (int i = 0; i < this.mCount; i++) {
final Ball ball = this.mBalls[i];
this.collisionDetectingAndChangeSpeed(ball); // 碰撞邊界的計(jì)算
ball.move(); // 移動(dòng)
}
final long stopTime = System.currentTimeMillis();
final long runTime = stopTime - startTime;
// 16毫秒執(zhí)行一次
this.postInvalidateDelayed(Math.abs(runTime - 16));
}
// 判斷球是否碰撞碰撞邊界
public void collisionDetectingAndChangeSpeed(final Ball ball) {
final int left = 0;
final int top = 0;
final int right = this.mWidth;
final int bottom = this.mHeight;
final float speedX = ball.vx;
final float speedY = ball.vy;
// 碰撞左右,X的速度取反。 speed的判斷是防止重復(fù)檢測碰撞,然后黏在墻上了=。=
if (ball.left() <= left && speedX < 0) {
ball.vx = -ball.vx;
} else if (ball.top() <= top && speedY < 0) {
ball.vy = -ball.vy;
} else if (ball.right() >= 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
// 移動(dòng)
fun move() {
//向角度的方向移動(dòng),偏移圓心
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(); // 隨機(jī)生成好看的顏色,github開源庫。
private final Random mRandom = new Random();
private final int mCount = 5; // 小球個(gè)數(shù)
private final int minSpeed = 5; // 小球最小移動(dòng)速度
private final int maxSpeed = 15; // 小球最大移動(dòng)速度
public Ball[] mBalls = new Ball[this.mCount]; // 用來保存所有小球的數(shù)組
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;
// 初始化所有球(設(shè)置顏色和畫筆, 初始化移動(dòng)的角度)
for (int i = 0; i < mBalls.length; i++) {
this.mBalls[i] = getRandomBall();
}
}
private Ball getRandomBall() {
Ball mBall = new Ball();
// 設(shè)置畫筆
setRandomBall(mBall);
return mBall;
}
private void setRandomBall(Ball ball) {
// 設(shè)置畫筆
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(randomColor.randomColor());
paint.setStyle(Paint.Style.FILL);
paint.setAlpha(180);
paint.setStrokeWidth(0);
ball.paint = paint;
// 設(shè)置速度
final float speedX = (this.mRandom.nextInt(this.maxSpeed - this.minSpeed + 1) + 5) / 10f;
final float speedY = (this.mRandom.nextInt(this.maxSpeed - this.minSpeed + 1) + 5) / 10f;
ball.vx = this.mRandom.nextBoolean() ? speedX : -speedX;
ball.vy = -speedY;
ball.radius = mRandom.nextInt(maxRadius + 1 - minRadius) + minRadius;
ball.cx = mRandom.nextInt(mWidth - ball.radius) + ball.radius;
ball.cy = mHeight - ball.radius;
}
@Override
protected void onDraw(final Canvas canvas) {
final long startTime = System.currentTimeMillis();
// 先畫出所有圓
for (int i = 0; i < this.mCount; i++) {
final Ball ball = this.mBalls[i];
canvas.drawCircle(ball.cx, ball.cy, ball.radius, ball.paint);
}
// 球碰撞邊界
for (int i = 0; i < this.mCount; i++) {
collisionDetectingAndChangeSpeed(mBalls[i]); // 碰撞邊界的計(jì)算
mBalls[i].move(); // 移動(dòng)
}
final long stopTime = System.currentTimeMillis();
final long runTime = stopTime - startTime;
// 16毫秒執(zhí)行一次
this.postInvalidateDelayed(Math.abs(runTime - 16));
}
// 判斷球是否碰撞碰撞邊界
public void collisionDetectingAndChangeSpeed(Ball ball) {
final int left = 0;
final int top = 0;
final int right = this.mWidth;
final int bottom = this.mHeight;
final float speedX = ball.vx;
final float speedY = ball.vy;
// 碰撞左右,X的速度取反。 speed的判斷是防止重復(fù)檢測碰撞,然后黏在墻上了=。=
if (ball.left() <= left && speedX < 0) {
ball.vx = -ball.vx;
} else if (ball.top() <= top && speedY < 0) {
setRandomBall(ball);
} else if (ball.right() >= right && speedX > 0) {
ball.vx = -ball.vx;
}
}
}


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android 仿微信聊天氣泡效果實(shí)現(xiàn)思路
- Android氣泡效果實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)氣泡布局/彈窗效果 氣泡尖角方向及偏移量可控
- Android貝塞爾曲線初步學(xué)習(xí)第二課 仿QQ未讀消息氣泡拖拽黏連效果
- Android帶氣泡的第三方Tab選項(xiàng)卡
- Android使用貝塞爾曲線仿QQ聊天消息氣泡拖拽效果
- Android使用ViewDragHelper實(shí)現(xiàn)QQ聊天氣泡拖動(dòng)效果
- Android Q之氣泡彈窗的實(shí)現(xiàn)示例
- Android不顯示開機(jī)向?qū)Ш烷_機(jī)氣泡問題
- Android實(shí)現(xiàn)氣泡動(dòng)畫
相關(guān)文章
Android App后臺(tái)服務(wù)報(bào)告工作狀態(tài)實(shí)例
這篇文章主要介紹了Android App后臺(tái)服務(wù)報(bào)告工作狀態(tài)實(shí)例,使用LocalBroadcastManager發(fā)送和接收狀態(tài),需要的朋友可以參考下2014-06-06
Android自定義view實(shí)現(xiàn)有header和footer作為layout使用的滾動(dòng)控件
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)有header和footer的滾動(dòng)控件,可以在XML中當(dāng)Layout使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android 關(guān)閉多個(gè)Activity的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 關(guān)閉多個(gè)Activity的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android通過代碼控制ListView上下滾動(dòng)的方法
今天小編就為大家分享一篇關(guān)于Android通過代碼控制ListView上下滾動(dòng)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

