Android實(shí)現(xiàn)氣泡動(dòng)畫
本文實(shí)例為大家分享了Android實(shí)現(xiàn)氣泡動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
一、前言本來想做一個(gè)類似window氣泡屏保的動(dòng)畫效果。
奈何小球間的非對(duì)心碰撞公式?jīng)]研究出來,對(duì)于我來說真的太復(fù)雜了,等公式給我研究差不多的時(shí)候,發(fā)現(xiàn)計(jì)算角度的問題也很復(fù)雜。博主表示高中的時(shí)候物理從未及格,而且這是大學(xué)物理的課程……然而我大學(xué)并沒有學(xué)物理。目前做出來的效果也很簡(jiǎn)單,只是檢測(cè)了邊界碰撞,原理就是動(dòng)量守恒,速度交換。實(shí)際效果如絲般順滑,gif錄制掉幀了。
這次就不封裝了,反正也只是半成品,寫著玩玩。
用到了一個(gè)很不錯(cuò)的庫(kù):點(diǎn)擊查看,隨機(jī)生成好看的顏色,國(guó)人寫的,厲害。
/** * Created by AItsuki on 2016/1/12. */public class BallView extends View { private final Random mRandom; class Ball {int radius; // 半徑float cx; // 圓心float cy; // 圓心float vx; // X軸速度float vy; // Y軸速度Paint paint;// 移動(dòng)void move() { //向角度的方向移動(dòng),偏移圓心 cx += vx; cy += vy;}int left() { return (int) (cx - radius);}int right() { return (int) (cx +radius);}int bottom() { return (int) (cy + radius);}int top() { return (int) (cy - radius);} } private int mCount = 40; // 小球個(gè)數(shù) private int maxRadius; // 小球最大半徑 private int minRadius; // 小球最小半徑 private int minSpeed = 5; // 小球最小移動(dòng)速度 private int maxSpeed = 20; // 小球最大移動(dòng)速度 private int mWidth = 200; private int mHeight = 200; public Ball[] mBalls; // 用來保存所有小球的數(shù)組 public BallView(Context context, AttributeSet attrs) {super(context, attrs);// 初始化所有球(設(shè)置顏色和畫筆, 初始化移動(dòng)的角度)mRandom = new Random();RandomColor randomColor = new RandomColor(); // 隨機(jī)生成好看的顏色,github開源庫(kù)。mBalls = new Ball[mCount];for(int i=0; i< mCount; i++) { mBalls[i] = new Ball(); // 設(shè)置畫筆 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(randomColor.randomColor()); paint.setStyle(Paint.Style.FILL); paint.setAlpha(180); paint.setStrokeWidth(0); // 設(shè)置速度 float speedX = (mRandom.nextInt(maxSpeed -minSpeed +1)+5)/10f; float speedY = (mRandom.nextInt(maxSpeed -minSpeed +1)+5)/10f; mBalls[i].paint = paint; mBalls[i].vx = mRandom.nextBoolean() ? speedX : -speedX; mBalls[i].vy = mRandom.nextBoolean() ? speedY : -speedY;}// 圓心和半徑測(cè)量的時(shí)候才設(shè)置 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mWidth = resolveSize(mWidth, widthMeasureSpec);mHeight = resolveSize(mHeight, heightMeasureSpec);setMeasuredDimension(mWidth, mHeight);maxRadius = mWidth/12;minRadius = maxRadius/2;// 初始化圓的半徑和圓心for (int i=0; i<mBalls.length; i++) { mBalls[i].radius = mRandom.nextInt(maxRadius+1 - minRadius) +minRadius;// mBalls[i].mass = (int) (Math.PI * mBalls[i].radius * mBalls[i].radius); // 初始化圓心的位置, x最小為 radius, 最大為mwidth- radius mBalls[i].cx = mRandom.nextInt(mWidth - mBalls[i].radius) + mBalls[i].radius; mBalls[i].cy = mRandom.nextInt(mHeight - mBalls[i].radius) + mBalls[i].radius;} } @Override protected void onDraw(Canvas canvas) {long startTime = System.currentTimeMillis();// 先畫出所有圓for (int i = 0; i < mCount; i++) { Ball ball = mBalls[i]; canvas.drawCircle(ball.cx, ball.cy, ball.radius, ball.paint);}// 球碰撞邊界for (int i = 0; i < mCount; i++) { Ball ball = mBalls[i]; collisionDetectingAndChangeSpeed(ball); // 碰撞邊界的計(jì)算 ball.move(); // 移動(dòng)}long stopTime = System.currentTimeMillis();long runTime = stopTime - startTime;// 16毫秒執(zhí)行一次postInvalidateDelayed(Math.abs(runTime -16)); } // 判斷球是否碰撞碰撞邊界 public void collisionDetectingAndChangeSpeed(Ball ball) {int left = getLeft();int top = getTop();int right = getRight();int bottom = getBottom();float speedX = ball.vx;float speedY = ball.vy;// 碰撞左右,X的速度取反。 speed的判斷是防止重復(fù)檢測(cè)碰撞,然后黏在墻上了=。=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;} }}
代碼直接復(fù)制就可以用,所以就不提供Demo下載了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明2. CSS hack用法案例詳解3. ASP 處理JSON數(shù)據(jù)的實(shí)現(xiàn)代碼4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問題7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲
