屬性動畫
時間:2018-09-27 來源:未知
Android提供了幾種動畫類型:View Animation(補間動畫) 、Drawable Animation (幀動畫)、Property Animation (屬性動畫)。View Animation相當簡單,不過只能支持簡單的縮放、平移、旋轉、透明度基本的動畫,且有一定的局限性。比如:你希望View有一個顏色的切換動畫;你 希望可以使用3D旋轉動畫;你希望當動畫停止時,View的位置就是當前的位置;這些View Animation都無法做到。這就是Property Animation產生的 原因。
新引入的屬性動畫機制已經不再是針對于View來設計的了,也不限定于只能實現移動、縮放、旋轉和淡入淡出這幾種動畫操作,同時也不再只是一 種視覺上的動畫效果了。它實際上是一種不斷地對值進行操作的機制,并將值賦值到指定對象的指定屬性上,可以是任意對象的任意屬性。
一、相關API
ObjectAnimator 動畫的執行類
ValueAnimator 動畫的執行類
AnimatorSet 用于控制一組動畫的執行
setDuration() 設置動畫時間
start() 開始動畫
cancel() 停止動畫在當前位置
end() 動畫直接到終狀態
setRepeatMode 設置動畫重復方式
setRepeatCount設置動畫重復次數
二、ObjectAnimator使用
ObjectAnimator是屬性動畫框架中重要的實現類,創建一個ObjectAnimator只需要通過它的靜態方法直接返回一個ObjectAnimator對象。靜態方 法如下:
ofFloat(Object target, String propertyName, float... values);
ofInt(Object target, String propertyName, int... values);
ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values);
這里先關注ofFloat、ofint,方法中有三個參數:
Target:指定執行動畫的view
propertyName:指定動畫的屬性
values:可變數組參數,指定屬性對應的屬性值
如下所示,給imageview設置漸變的動畫。
ObjectAnimator animator = ObjectAnimator.ofFloat(imageview, "alpha", 1.0f, 0.0f);
// 設置時間
animator. setDuration(1000);
// 開始動畫
animator.start();
下面列舉出一些可以直接使用的屬性:
translationX、translationY:這兩個屬性作為一種增量來控制著View對象從它布局容器的左上角坐標開始的位置。
rotation、rotationX、rotationY:這三個屬性控制著View對象圍繞它的支點進行2D和3D的旋轉。
scaleX和scaleY:這兩個屬性控制著View對象圍繞它的支點進行2D縮放。
alpha:它表示View對象的alpha透明度。
二、ValueAnimator使用
ValueAnimator是整個屬性動畫中核心的一個類,前面介紹的ObjectAnimator也是繼承自ValueAnimator。
ValueAnimator本身不提供任何動畫效果,它更像一個數值發生器,用來產生具有一定規律的數字,從而讓調用者來控制動畫的實現過程。通常情 況下,在ValueAnimator的AnimatorUpdateListener中監聽數值的變化,從而完成動畫的切換。
如下所示,利用ValueAnimator給imageview設置漸變的動畫。
ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f);
animator.setDuration(1000);
animator.start();
// 反復循環,REATART從頭開始循環
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.setRepeatCount(ValueAnimator.INFINITE);// 無限循環
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView.setAlpha((Float)animation.getAnimatedValue());
}
});
三、AnimatorSet使用
AnimatorSet這個類來幫我們實現組合屬性動畫的效果。AnimatorSet這個類提供了一個play()方法,如果我們向這個方法中傳入一個Animator對象 (ObjectAnimator或者ValueAnimator)將會返回一個AnimatorSet.Builder的實例,AnimatorSet.Builder中包含了以下四個方法:
after(Animator anim) : 將現有動畫插入到傳入的動畫之后執行。
after(long delay):將現有的動畫延遲指定的毫秒后執行。
before(Animator anim):將現有的動畫插入到傳入的動畫之前執行。
with(Animator anim):將現有的動畫和傳入的動畫同時執行。
// 移動動畫
ObjectAnimator transAnimator = ObjectAnimator.ofFloat(mTextView, "translationX", -500f, 300f);
// 旋轉動畫
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mTextView, "rotation", 0f, 360f);
// 淡入淡出
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mTextView, "alpha", 1f, 0f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(rotationAnimator).with(alphaAnimator).after(transAnimator);
animatorSet.setDuration(5000);
animatorSet.start();
四、Animator監聽器
對于動畫,一般都是一些輔助效果,比喻說一個view動畫結束后里一個view開始動畫,這就需要對動畫過程進行監聽。通過實現AnimatorListener 接口即可對動畫的Start、End、Repeat、Cancel四個狀態監聽。
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.e("tag", "onAnimationStart");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.e("tag", "onAnimationEnd");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.e("tag", "onAnimationCancel");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.e("tag", "onAnimationRepeat");
}
})

