android使用屬性動畫代替補間動畫

本文參考Android屬性動畫徹底解析(上),初識屬性動畫的基本用法 android

android3.0以前一共有兩種動畫,分別是frame動畫和tween動畫,關於這兩種動畫若是不瞭解能夠查看我以前的文章android之frame動畫詳解android之tween動畫詳解 ,frame動畫就是逐幀動畫,把多張圖片放在一塊兒連續播放實現一種相似於gif圖片的動畫效果,tween動畫就是補間動畫,主要是對圖像進行平移,縮放,旋轉,改變透明度等。然而tween動畫有很大的侷限性,咱們看看官方文檔:
這裏寫圖片描述ide

tween動畫被稱做View Animation,第一句就說的很清楚了,你能夠使用View Animation System 來在View上實現tween動畫。也就是說tween動畫只能做用在view上,但是若是咱們想變換一個自定義View的顏色該怎麼辦?抱歉,tween沒法作到。動畫

android3.0推出了一種新的動畫實現方式,那就是屬性動畫,屬性動畫,顧名思義,就是做用在View的屬性上,咱們可讓View的屬性實現動畫效果。spa

說到這裏咱們不得不介紹ObjectAnimator類,這個也是在實際開發中用的最多的,追根溯源,咱們發現ObjectAnimator繼承自ValueAnimator,ValueAnimator能夠幫助咱們實現一些簡單的數值的變化,不過到目前爲止還沒用上這個東東。3d

下面咱們先來看看怎麼使用屬性動畫來實現tween動畫效果:code

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f,
                    0.5f);
            animator.setDuration(5000);
            animator.start();

咱們經過ofFloat方法來得到一個ObjectAnimator實例,先看看該方法的源碼:xml

public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
        anim.setFloatValues(values);
        return anim;
    }

從源碼中能夠看到該方法接收N個參數,第一個是咱們要設置動畫的對象,第二個參數給哪一個屬性設置動畫,後面兩個參數表示控件從不透明變爲半透明,後面的參數能夠傳N多個,好比對象

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f,
                    0.5f,1f,0f);

表示控件從不透明到半透明再到不透明,最後到全透明這樣一個狀態。有了這個例子以後,我想要實現其餘tween動畫實現的效果都不是問題了。blog

旋轉:繼承

ObjectAnimator animator2 = ObjectAnimator.ofFloat(tv2, "rotation",
                    0f, 360f);
            animator2.setDuration(5000);
            animator2.start();

平移:

float curTranslationX = tv.getTranslationX();
            ObjectAnimator animator = ObjectAnimator.ofFloat(tv,
                    "translationX", curTranslationX, -1000f, curTranslationX);
            animator.setDuration(3000);
            animator.start();

若是要實現組合動畫效果呢?

ObjectAnimator moveIn = ObjectAnimator.ofFloat(tv, "translationX",
                    -500f, 0f);
            ObjectAnimator rotate = ObjectAnimator.ofFloat(tv, "rotation", 0f,
                    360f);
            ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(tv, "alpha", 1f,
                    0f, 1f);
            AnimatorSet animSet = new AnimatorSet();
            animSet.play(rotate).with(fadeInOut).after(moveIn);
            animSet.setDuration(5000);
            animSet.start();

哈哈,仍是很簡單吧。
這裏有一個須要注意的地方就是:


// after(Animator anim) 現有動畫在傳入的動畫以後執行
    // after(long delay) 現有動畫延遲指定毫秒後執行
    // before(Animator anim) 現有動畫在傳入的動畫以前執行
    // with(Animator anim) 現有動畫和傳入的動畫同時執行

固然,屬性動畫和tween動畫同樣,便可以使用代碼實現也能夠使用xml來實現,那麼咱們看看怎麼經過xml文件來實現屬性動畫:

<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" >

    <objectAnimator  android:duration="2000" android:propertyName="translationX" android:valueFrom="-1000" android:valueTo="0" android:valueType="floatType" >
    </objectAnimator>

    <set android:ordering="together" >
        <objectAnimator  android:duration="3000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" android:valueType="floatType" >
        </objectAnimator>

        <set android:ordering="sequentially" >
            <objectAnimator  android:duration="1500" android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" >
            </objectAnimator>
            <objectAnimator  android:duration="1500" android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" >
            </objectAnimator>
        </set>
    </set>

</set>

android:ordering的值爲together表示各個維度的變化同時發生,缺省值也是together,sequentially表示動畫依次發生。

基本上就是這樣。

相關文章
相關標籤/搜索