Android Animations

Android Animation

1 Android動畫有哪些

Android 平臺提供了一套完整的動畫框架。

在Android3.0之前有兩種動畫,一種方式是補間動畫 Tween Animation、另一種叫逐幀動畫 Frame Animation(也稱Drawable Animation )。

Tween Animation、Frame Animation只能用于View,被歸類為View Animation。

Android3.0以后增加了屬性動畫Property Animation。這樣子動畫就分成兩部分:


Google Android Docs - Animation

2 View Animation

2.1 Tween Animation

TweenAnimation只能應用于View對象,而且只支持一部分屬性,如支持縮放旋轉而不支持背景顏色的改變。而且對于TweenAnimation,并不改變屬性的值,它只是改變了View對象繪制的位置,而沒有改變View對象本身,比如,你有一個Button,坐標(100,100),Width:100,Height:100,而你有一個動畫使其移動(200,200),你會發現動畫過程中觸發按鈕點擊的區域仍是(100,100)-(200,200)。

2.1.1 定義Tween Animation

View Animation就是一系列View形狀的變換,如大小的縮放,透明度的改變,位置的改變,動畫的定義既可以用代碼定義也可以用XML定義,當然,建議用XML定義。在XML定義的資源,在Android Studio可以用Instant Run快速調試。

可以給一個View同時設置多個動畫,比如從透明至不透明的淡入效果,與從小到大的放大效果,這些動畫可以同時進行,也可以在一個完成之后開始另一個。

用XML定義的動畫放在/res/anim/文件夾內,XML文件的根元素可以為alpha, scale, translate, rotate, interpolator元素或set(表示以上幾個動畫的集合,set可以嵌套)。
默認情況下,所有動畫是同時進行的,可以通過startOffset屬性設置各個動畫的開始偏移(開始時間)來達到動畫順序播放的效果。

Tween Animation XML示例

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">    
<scale
            android:fromXScale="1.0"
            android:toXScale="2"
            android:fromYScale="1.0"
            android:toYScale="2"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="2000"
            />    

<translate
            android:duration="2000"
            android:fromXDelta="0.0%p"
            android:fromYDelta="0.0%p"
            android:toXDelta="50.0%p"
            android:toYDelta="50.0%p"
            />

<alpha
            android:fromAlpha="0.5"
            android:toAlpha="1.0"
            android:startOffset="500"
            android:duration="2000"
            />    

<rotate
            android:fromDegrees="0"
            android:toDegrees="720"
            android:pivotX="25"
            android:pivotY="25"
            android:duration="2000"
            />
</set>

注意:
android:pivotXandroid:pivotYandroid:fromXDelta,android:toXDelta
android:pivotX="50"使用絕對坐標
android:pivotX="50%"相對自己,自己的坐標也是固定的,不會隨著位移動畫變化。因為Tween動畫的View在原處,動畫效果是例外生成的圖像。
android:pivotX="50%p"相對父控件

2.1.2 運行Tween動畫

ivTween = (ImageView) getView().findViewById(R.id.iv_bat_man);
final Animation animation = AnimationUtils.loadAnimation(mActivity,R.anim.test_animation_tween);
ivTween.startAnimation(animation);  

2.1.3 TimeInterplator(時間插值器)

Time interplator定義了屬性值變化的方式,如線性均勻改變,開始慢然后逐漸快等。在Property Animation中是TimeInterplator,在View Animation中是Interplator,這兩個是一樣的,在3.0之前只有Interplator,3.0之后實現代碼轉移至了TimeInterplator。Interplator繼承自TimeInterplator,內部沒有任何其他代碼。

插值器 描述
AccelerateInterpolator 加速,開始時慢中間加速
DecelerateInterpolator 減速,開始時快然后減速
AccelerateDecelerateInterolator 先加速后減速,開始結束時慢,中間加速
AnticipateInterpolator 反向 ,先向相反方向改變一段再加速播放
AnticipateOvershootInterpolator 反向加回彈,先向相反方向改變,再加速播放,會超出目的值然后緩慢移動至目的值
BounceInterpolator 跳躍,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
CycleIinterpolator 循環,動畫循環一定次數,值的改變為一正弦函數:Math.sin(2 * mCycles * Math.PI * input)
LinearInterpolator 線性,線性均勻改變
OvershottInterpolator 回彈,最后超出目的值然后緩慢改變到目的值
TimeInterpolator 一個接口,允許你自定義interpolator,以上幾個都是實現了這個接口

2.2 Frame Animation

Drawable Animation(Frame Animation):幀動畫,就像GIF圖片,通過一系列Drawable依次顯示來模擬動畫的效果。

Java類AnimationDrawable繼承于DrawableContainer,DrawableContainer繼承于Drawable。

2.2.1 Frame Animation的XML定義

在XML中的定義方式如下:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/loading01" android:duration="200" />
    <item android:drawable="@drawable/loading02" android:duration="200" />
    <item android:drawable="@drawable/loading03" android:duration="200" />
</animation-list>

必須以animation-list為根元素,以item表示要輪換顯示的圖片,duration屬性表示各項顯示的時間。XML文件要放在/res/drawable/目錄下。示例:

2.2.2 調用示例

ivDrawable = (ImageView) getView().findViewById(R.id.iv_anim_drawable);
ivDrawable.setBackgroundResource(R.drawable.test_animation_drawable);

AnimationDrawable animationDrawable = (AnimationDrawable) ivDrawable.getBackground();
animationDrawable.start();

3 Property Animation(屬性動畫)

屬性動畫,這個是在Android 3.0中才引進的。

Property Animation可以定義在xml文件中,它用來在設定的時間內修改對象的屬性。例如背景顏色和alpha的值。它更改的是對象的實際屬性。
在View Animation(Tween Animation)中,其改變的是View的繪制效果,真正的View的屬性保持不變,比如無論你在對話中如何縮放Button的大小,Button的有效點擊區域還是沒有應用動畫時的區域,其位置與大小都不變。
而在Property Animation中,改變的是對象的實際屬性,如Button的縮放,Button的位置與大小屬性值都改變了。而且Property Animation不止可以應用于View,還可以應用于任何對象。Property Animation只是表示一個值在一段時間內的改變,當值改變時要做什么事情完全是你自己決定的。

3.1 Property Animation 動畫的設置

The property animation system允許開發者定義動畫設置:

  1. Duration: 動畫間隔,默認300ms

  2. Time interpolation: 插值器
    默認線性插值器


    3.1-1 線性勻速動畫 - Example of a linear animation

    非線性插值器(中間快前后慢)


    3.1-2 非線性動畫
  3. Repeat count and behavior: 重復次數,反向or正向重復

  4. Animator sets: 動畫集,可以一起播放動畫,或者分開播放,或者延遲播放。

  5. Frame refresh delay: Frame刷新時間,默認10ms,但是實際刷新時間是由系統資源決定。

3.2 屬性動畫的實現

How animations are calculated

ValueAnimator

ValueAnimator對象跟蹤動畫時間,例如,動畫已經運行多久,當前屬性值是多少;

封裝一個TimeInterpolator插值器;

TypeEvaluator定義當前的屬性如何計算。

例如,圖3.1-2的動畫,theTimeInterpolatorused would be AccelerateDecelerateInterpolatorand theTypeEvaluator would beIntEvaluator.

3.3 屬性動畫示例

插值器的實現

// Initialize Interpolators programmatically by loading them from their XML definitions
// provided by the framework.
mInterpolators = new Interpolator[]{
        new AnimationUtils().loadInterpolator(getActivity(),
                android.R.interpolator.linear),
        new AnimationUtils().loadInterpolator(getActivity(),
                android.R.interpolator.fast_out_linear_in),
        new AnimationUtils().loadInterpolator(getActivity(),
                android.R.interpolator.fast_out_slow_in),
        new AnimationUtils().loadInterpolator(getActivity(),
                android.R.interpolator.linear_out_slow_in)
};

動畫的實現:這個ObjectAnimator對象,使用path來指定x和y的大小變化前后值。

// This ObjectAnimator uses the path to change the x and y scale of the mView object.
ObjectAnimator animator = ObjectAnimator.ofFloat(mView, View.SCALE_X, View.SCALE_Y, path);

// Set the duration and interpolator for this animation
animator.setDuration(duration);
animator.setInterpolator(interpolator);

animator.start();

code via Google Samples - Interpolator。

3.4 Property Animation與View Animation的區別

View Animation的優點,實現起來比較簡單,只需要在XML定義并加載。但是全面性和靈活性比較低。

View Animation的不足

  • 只能針對View Objects做動畫
  • 并且變化的方面比較少:例如,背景顏色等就無法實現。
  • View Animation的動畫是畫出來的,而不是View Object真正在移動。所以如果要真正移動View的話,在動畫結束之后,自定寫View Object的位移邏輯。

Property Animation可以消除以上局限。

3.5 API Overview

你可以從android.animation找到Property Animation的大多數API。因為View Animation體系已經構建了大量的interpolator插值器在android.view.animation,你也可以在Property Animation中使用。

Animator類提供了基本的創建動畫的方法。你可以通過其子類實現自己需要的動畫:

ValueAnimator

  • 提供計時器,動畫值的計算
  • 動畫是否重復播放
  • 動畫的回調
  • the ability to set custom types to evaluate
  • 完整的屬性動畫包括:計算動畫值,設置動畫值給對象。使用ValueAnimator不會做第二部分操作。需要用戶通過回調寫賦值的邏輯。
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start();

需要在回調中綁定Animator.AnimatorListenerr設置Object的屬性。

ObjectAnimatorValueAnimator的子類。

  • 不需要用戶在回調中寫給Object賦值的邏輯。
ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);//綁定Object與其屬性
anim.setDuration(1000);
anim.start();

AnimatorSet設置動畫組合。

例如,APIDemos - Bouncing Balls (該DEMO已不在Google Samples里),實現一系列動畫:

  1. Plays bounceAnim.
  2. Plays squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 at the same time.
  3. Plays bounceBackAnim.
  4. Plays fadeAnim.
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);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
animatorSet.start();

4 動畫矢量圖片

矢量圖片讓您可以用在 XML 中定義的一個矢量圖形替換多個 png 資源。在以前的版本中,矢量圖片僅限于 Lollipop 和更高版本的設備,現在VectorDrawable和AnimatedVectorDrawable可分別通過兩個新的支持內容庫support-vector-drawable和animated-vector-drawable獲取。
Android Support Library 23.2開始支持支持矢量圖片和動畫矢量圖片。后續好好研究一下。

參考

android 動畫分類 - 張興業的博客 - 博客頻道 - CSDN.NET

Android Animation - stormzhang

Android動畫之XML(二) - 卡爾 - 博客頻道 - CSDN.NET

Google Android Developer Doc Property Animation 更多關于API Overview,ValueAnimator與ObjectAnimator的使用,可以參考。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1 背景 不能只分析源碼呀,分析的同時也要整理歸納基礎知識,剛好有人微博私信讓全面說說Android的動畫,所以今...
    未聞椛洺閱讀 2,750評論 0 10
  • 轉載一篇高質量博文,原地址請戳這里轉載下來方便今后查看。1 背景不能只分析源碼呀,分析的同時也要整理歸納基礎知識,...
    Elder閱讀 1,954評論 0 24
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,381評論 25 708
  • 我的大學 自入學那一刻起,帶著高考落敗的不...
    勇敢者無畏閱讀 241評論 0 0
  • 驅車到傳說中的大美青海湖身心舒爽《麻進偉 》
    麻進偉閱讀 246評論 0 0