從android3.0,系統提供了一個新的動畫-property animation, 為什么系統會提供這樣一個全新的動畫包呢,先來看看之前的補間動畫都有什么缺陷吧
1、傳統的補間動畫都是固定的編碼,功能是固定的,擴展難度大。比如傳統動畫只能實現移動、縮放、旋轉、淡入淡出,四種效果,如果去改變view的背景,就只能自己去實現。
2、補間動畫只是改變了view的顯示效果,不會真正改變view的屬性,比如一個動畫的點擊時間,view移動到另一個地方,它的監聽事件還在換來的地方。
3、傳統動畫不能對非view的對象進行動畫操作。
這估計就是property animation出現的原因了吧,說了這些來看看android.animation里面都有什么東西吧
ValueAnimator
ValueAnimator是整個動畫機制當中最核心的一個類,屬性動畫中主要的時序引擎,如動畫的時間,開始、結束屬性值,ValueAnimator還負責管理動畫的播放次數,播放模式,以及對動畫設計監聽器。
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.setDuration(300);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float currentValue = (float) animation.getAnimatedValue(); Log.d("TAG", "cuurent value is " + currentValue); }
});
anim.start();
ObjectAnimator
允許你指定要進行動畫的對象以及該對象的一個屬性。該類會根據計算得到的新值自動更新屬性。ValueAnimator是對值進行了一個平滑的動畫過渡,而objectAnimator則可以直接對任意對象的屬性進行動畫操作。
fter(Animator anim) 將現有動畫插入到傳入的動畫之后執行
after(long delay) 將現有動畫延遲指定毫秒后執行
before(Animator anim) 將現有動畫插入到傳入的動畫之前執行
with(Animator anim) 將現有動畫和傳入的動畫同時執行
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
animator.setDuration(5000);
animator.start();
AnimatorSet
使動畫組合到一起,并可設置組中動畫的時序關系,如同時播放,有序播放或延遲播放。
ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(rotate).with(fadeInOut).after(moveIn);
animSet.setDuration(5000);
animSet.start();
Interpolator
先看一下TimeInterpolator接口的定義,
public interface TimeInterpolator { /** * Maps a value representing the elapsed fraction of an animation to a value that represents * the interpolated fraction. This interpolated value is then multiplied by the change in * value of an animation to derive the animated value at the current elapsed animation time. * * @param input A value between 0 and 1.0 indicating our current point * in the animation where 0 represents the start and 1.0 represents * the end * @return The interpolation value. This value can be more than 1.0 for * interpolators which overshoot their targets, or less than 0 for * interpolators that undershoot their targets. */ float getInterpolation(float input);
}
getInterpolation()方法中接收一個input參數,這個參數的值會隨著動畫的運行而不斷變化,不過它的變化是非常有規律的,就是根據設定的動畫時長勻速增加,變化范圍是0到1。動畫一開始input的值是0, 結束時input的值是1, 而中間值則是隨著動畫運行的時長在0到1之間變化。
ViewPropertyAnimator
為了方便對view的動畫操作,在android3.1補充了ViewPropertyAnimator這個機制。
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
myView.animate().x(50f).y(100f);
為ViewGroup添加布局動畫
可以在ViewGroup內,通過LayoutTransition類為布局的變化添加動畫。
當一個ViewGroup中添加或者移除某一個item,或者調用了View的setVisibility方法,使得View 變得VISIBLE或者GONE的時候,在ViewGroup內部的View可以完成出現或者消失的動畫。當你添加或者移除View的時候,那些剩余的View也可以通過動畫的方式移動到自己的新位置。你可以通過setAnimator()方法并傳遞一個Animator對象,在LayoutTransition內部定義以下動畫。以下是幾種事件類型的常量:
APPEARING 為那些添加到父元素中的元素應用動畫
CHANGE_APPEARING 為那些由于父元素添加了新的item而受影響的item應用動畫
DISAPPEARING 為那些從父布局中消失的item應用動畫
CHANGE_DISAPPEARING 為那些由于某個item從父元素中消失而受影響的item應用動畫
你可以為這四種事件定義自己的交互動畫,或者僅僅告訴動畫系統使用默認的動畫。
API Demos中的LayoutAnimations sample向你展示了如何為布局轉換定義一個布局動畫,然后將該動畫設置到目標View對象上
Keyframes
由一個鍵值對組成,可以為動畫定義某一特定時間的特定狀態。每個keyframe可以擁有自己的插值器,用于控制前一幀和當前幀的時間間隔間內的動畫。第一個參數為:要執行該幀動畫的時間節點(elapsed time / duration),第二個參數為屬性值。
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(“rotation”, kf0, kf1, kf2);//動畫屬性名,可變參數
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
rotationAnim.setDuration(5000ms);