最近在學習android的開發,打算把sdk的示例代碼研究一下,一直以來都沒有作筆記的習慣,以致於看過的東西記住的少遺忘的多,所以試試記下筆記,以備之後查詢。java
-----------------------------以上是首博呢喃------------------------------------------------------android
BouncingBalls主要使用的是ValueAnimator及其子類ObjectAnimatoride
首先是採用了ValueAnimator及其子類ObjectAnimator來設置背景顏色漸變的動畫效果學習
//建立一個值漸變更畫,其中backgroundColor是View中的屬性,即View類中有setBackgroundColor方法 ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE); colorAnim.setDuration(3000); colorAnim.setEvaluator(new ArgbEvaluator());//設置顏色插值器 colorAnim.setRepeatCount(ValueAnimator.INFINITE); colorAnim.setRepeatMode(ValueAnimator.REVERSE); colorAnim.start();
自定義了ShapHolder類保存用於繪製ball的屬性
動畫
public class ShapeHolder { private float x = 0, y = 0;//ball的中心點座標 private ShapeDrawable shape;//ball的圖形 private int color; private RadialGradient gradient;//ball的環形渲染器,沒有用到,渲染器已經設置到paint裏邊了 private float alpha = 1f; private Paint paint; ..... }
ball的動畫效果經過一組ValueAnimator疊加來完成this
//ball加速下落,尚未碰到下邊框的動畫 ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY); bounceAnim.setDuration(duration); bounceAnim.setInterpolator(new AccelerateInterpolator());//設置加速 //ball碰到下邊框後壓扁的效果,寬度拉長一倍,因此中心點要相應調整 ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x", newBall.getX(), newBall.getX() - 25f); squashAnim1.setDuration(duration/4); squashAnim1.setRepeatCount(1);//壓扁和回彈是逆向過程,因此這裏須要重複一次 squashAnim1.setRepeatMode(ValueAnimator.REVERSE);//設置重複的那次動畫是逆向過程 squashAnim1.setInterpolator(new DecelerateInterpolator());//設置減速 //ball碰到下邊框後壓扁的效果,寬度拉長一倍 ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall, "width", newBall.getWidth(), newBall.getWidth() + 50); squashAnim2.setDuration(duration/4); squashAnim2.setRepeatCount(1); squashAnim2.setRepeatMode(ValueAnimator.REVERSE); squashAnim2.setInterpolator(new DecelerateInterpolator()); //ball碰到下邊框後壓扁的效果,高度變爲原來的一半,中心點相應調整 ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "y", endY, endY + 25f); stretchAnim1.setDuration(duration/4); stretchAnim1.setRepeatCount(1); stretchAnim1.setInterpolator(new DecelerateInterpolator()); stretchAnim1.setRepeatMode(ValueAnimator.REVERSE); //ball碰到下邊框後壓扁的效果,高度變爲原來的一半 ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall, "height", newBall.getHeight(), newBall.getHeight() - 25); stretchAnim2.setDuration(duration/4); stretchAnim2.setRepeatCount(1); stretchAnim2.setInterpolator(new DecelerateInterpolator()); stretchAnim2.setRepeatMode(ValueAnimator.REVERSE); //ball回彈的效果 ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall, "y", endY, startY); bounceBackAnim.setDuration(duration); bounceBackAnim.setInterpolator(new DecelerateInterpolator());
ball回彈完成以後,有一個逐漸消失的過程動畫lua
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); fadeAnim.setDuration(250); fadeAnim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { //消失動畫完成後刪除ball balls.remove(((ObjectAnimator)animation).getTarget()); } });