當前位置:編程學習大全網 - 腳本源碼 - animation和animator的區別

animation和animator的區別

壹、 前言

Animator框架是Android 4.0中新添加的壹個動畫框架,和之前的Animation框架相比,Animator可以進行更多和更精細化的動畫控制,而且比之前更簡單和更高效。在4.0源碼中隨處都可以看到Animator的使用。

二、 Animation和Animator比較

如下圖,是Animation和Animator兩個類繼承圖的對比。

C:Object C:Object

C:Animation C:Animator

C:AlphaAnimation C:AnimatorSet

C:AnimationSet C:ValueAnimator

C:DummyAnimation C:ObjectAnimator

C:Rotate3dAnimation C:TimeAnbimator

C:RotateAniamtion

C:ScaleAnimation

C:TranslateAnimation

Animation框架定義了透明度,旋轉,縮放和位移幾種常見的動畫,而且控制的是壹個整個View動畫,實現原理是每次繪制視圖時View所在的ViewGroup中的drawChild函數獲取該View的Animation的Transformation值,然後調用canvas.concat(transformToApply.getMatrix()),通過矩陣運算完成動畫幀,如果動畫沒有完成,繼續調用invalidate()函數,啟動下次繪制來驅動動畫,動畫過程中的幀之間間隙時間是繪制函數所消耗的時間,可能會導致動畫消耗比較多的CPU資源。

在Animator框架中使用最多的是AnimatorSet和ObjectAnimator配合,使用ObjectAnimator進行更精細化控制,只控制壹個對象的壹個屬性值,多個ObjectAnimator組合到AnimatorSet形成壹個動畫。而且ObjectAnimator能夠自動驅動,可以調用setFrameDelay(longframeDelay)設置動畫幀之間的間隙時間,調整幀率,減少動畫過程中頻繁繪制界面,而在不影響動畫效果的前提下減少CPU資源消耗。

三、 關鍵接口介紹

1. ObjectAnimator介紹

Animator框架封裝得比較完美,對外提供的接口非常簡單,創建壹個ObjectAnimator只需通過如下圖所示的靜態工廠類直接返回壹個ObjectAnimator對象。傳的參數包括壹個對象和對象的屬性名字,但這個屬性必須有get和set函數,內部會通過java反射機制來調用set函數修改對象屬性值。還包括屬性的初始值,最終值,還可以調用setInterpolator設置曲線函數。

2. AnimatorSet介紹

AnimatorSet主要是組合多個AnimatorSet和ObjectAnimator形成壹個動畫,並可以控制動畫的播放順序,其中還有個輔助類通過調用play函數獲得。

3. AnimatorUpdateListner介紹

通過實現AnimatorUpdateListner,來獲得屬性值發生變化時的事件,在這個回調中發起重繪屏幕事件。

四、 使用實例

在Android4.0中的ApiDemo中有個BouncingBalls實例,描述了Animator框架的使用,當點擊屏幕時,繪制壹個球從點擊位置掉到屏幕底部,碰到底部時球有壓扁的效果,然後回彈到點擊位置再消失。

代碼如下:

ShapeHolder newBall =addBall(event.getX(), event.getY());

// Bouncing animation with squash and stretch

float startY = newBall.getY();

float endY = getHeight() - 50f;

float h = (float)getHeight();

float eventY = event.getY();

int duration = (int)(500 * ((h - eventY)/h));

ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY);

bounceAnim.setDuration(duration);

bounceAnim.setInterpolator(new AccelerateInterpolator());

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());

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());

ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "y", endY,

endY + 25f);

stretchAnim1.setDuration(duration/4);

stretchAnim1.setRepeatCount(1);

stretchAnim1.setInterpolator(new DecelerateInterpolator());

stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);

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);

ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall, "y", endY,

startY);

bounceBackAnim.setDuration(duration);

bounceBackAnim.setInterpolator(newDecelerateInterpolator());

// Sequence the down/squash&stretch/upanimations

AnimatorSet bouncer = new AnimatorSet();

bouncer.play(bounceAnim).before(squashAnim1);

bouncer.play(squashAnim1).with(squashAnim2);

bouncer.play(squashAnim1).with(stretchAnim1);

bouncer.play(squashAnim1).with(stretchAnim2);

bouncer.play(bounceBackAnim).after(stretchAnim2);

// Fading animation - remove the ball when theanimation is done

ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);

fadeAnim.setDuration(250);

fadeAnim.addListener(new AnimatorListenerAdapter() {

@Override

public void onAnimationEnd(Animatoranimation) {

balls.remove(((ObjectAnimator)animation).getTarget());

}

});

// Sequence the two animations to play oneafter the other

AnimatorSet animatorSet = new AnimatorSet();

animatorSet.play(bouncer).before(fadeAnim);

// Start the animation

animatorSet.start();

  • 上一篇:上廁所最忌諱的七件事
  • 下一篇:DNF手遊12月15日新禮包上線
  • copyright 2024編程學習大全網