Android_之動畫1

    近一週以來一直在搞android動畫,被android動畫搞的焦頭爛額,不過最艱難的時間已經過去,現在寫一篇博客,來給自己做一個梳理和總結。

    Android 3.0以後新增了屬性動(property animation),至此Android包含三種動畫類型,即補間動畫(Tween Animation),逐幀動畫(Drawable animation),屬性動畫(Property animation),今天這篇博客着重對屬性動畫做一個詳細闡述。


  1.      補間動畫(Tween Animation)

    補間動畫類似於之前學過的Flash動畫,設定好初始幀和結束幀以後,中間的值由系統自動進行插值,然後可以形成一種動畫效果,補間動畫需要用戶設定好初始值和結束值,系統自動計算之間值。對於這種動畫來說,建議用戶使用XML文件配置動畫,anim.xml文件應存放於res/anim文件夾下,xml文件必須以<alpha><scale><translate><rotate><set>作爲根元素。官方文件中提供的xml示例代碼:


<set android:shareInterpolator="false">
   
<scale
       
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
       
android:fromXScale="1.0"
       
android:toXScale="1.4"
       
android:fromYScale="1.0"
       
android:toYScale="0.6"
       
android:pivotX="50%"
       
android:pivotY="50%"
       
android:fillAfter="false"
       
android:duration="700" />
   
<set android:interpolator="@android:anim/decelerate_interpolator">
       
<scale
           
android:fromXScale="1.4"
           
android:toXScale="0.0"
           
android:fromYScale="0.6"
           
android:toYScale="0.0"
           
android:pivotX="50%"
           
android:pivotY="50%"
           
android:startOffset="700"
           
android:duration="400"
           
android:fillBefore="false" />
       
<rotate
           
android:fromDegrees="0"
           
android:toDegrees="-45"
           
android:toYScale="0.0"
           
android:pivotX="50%"
           
android:pivotY="50%"
           
android:startOffset="700"
           
android:duration="400" />
   
</set>

讓動畫跑起來也是十分簡單的,定義好動畫xml文件以後,使用如下代碼使用對象和動畫的綁定


ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, 動畫文件);spaceshipImage.startAnimation(hyperspaceJumpAnimation);

  補間動畫只是啓動空間顯示的位置,但是沒有移動或者改變空間在佈局當中實際文字,例如一個Button在空間中的位置(100,100),X軸方向移動n個單位以後,Button實際位置依然在(100,100)。 

2. 逐幀動畫(Frame animation)

    逐幀動畫類似於電影播放的原理,一個圖片資源代表一幀,以一定時間間隔進行切換圖片資源,形成一種動畫效果。一般情況下,逐幀動畫也是通過xml文件定義的,下面是官方提供的一個示例代碼:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
   
android:oneshot="true">
   
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
   
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
   
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

其中有一個屬性是非常重要的,android:oneshot="true",若爲true,則代表動畫完成以後,停留在最後一幀,反之停留在第一幀。

讓動畫開始也是非常簡單的,看官方代碼:

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState);
  setContentView
(R.layout.main);

 
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage
.setBackgroundResource(R.drawable.rocket_thrust);
  rocketAnimation
= (AnimationDrawable) rocketImage.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
 
if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation
.start();
   
return true;
 
}
 
return super.onTouchEvent(event);
}

有一點是非常重要的,不能在onCreate()函數中啓動動畫,因爲在OnCreate函數中,AnimationDrawable還沒有完全和window連接起來,所以如果你想一開始就加載動畫,可以在 onWindowFocusChanged()  啓動動畫。
























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章