Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫筆記:View 動畫與 Drawable 動畫

編輯:關於 Android 程式設計

不論是以 XML 撰寫還是以程式碼建立的動畫,都能定義連續播放的效果。一般建議使用 XML 來撰寫動畫,因為它更容易完成、更容易重複利用,也更方便日後修改。XML 動畫檔案放在 res/anim 目錄中,根節點必須是 <alpha>、<scale>、<translate>、<rotate> 或 <set> 之一。預設情況下所有動畫會同時進行,若要讓它們依序播放,可以透過 startOffset 屬性來控制。

一、View 動畫的 XML 範例

以下範例示範縮放與旋轉動畫的組合,透過 startOffset 讓第二段動畫在第一段結束後才開始:

<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:pivotX="50%"
         android:pivotY="50%"
         android:startOffset="700"
         android:duration="400" />
   </set>
</set>

螢幕座標以左上角為原點 (0,0),往右與往下依序遞增。像 pivotX 這類值可以設為相對值,例如 50% 代表相對於自身的一半,若不加上百分比符號則代表相對於父容器的一半。此外也可以自訂 Interpolator(速度插入器)來控制動畫的速度曲線。

在程式碼中載入並啟動動畫的方式如下:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

若不呼叫 startAnimation,也可以改用 animation.setStartTime(),當時間抵達該點時動畫會自動播放。

二、Drawable 動畫

Drawable 動畫能將一張張的 Drawable 資源依序播放出來,就像傳統的動畫一樣,其基礎類別為 AnimationDrawable。由於本質上是由 Drawable 資源組成,其 XML 檔案通常放在 res/drawable 中,以 <animation-list> 作為根節點,並以 <item> 描述每一格畫面:

<animation-list xmlns:android="https://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" 代表只播放一次並停在最後一張,設為 false 則會持續循環播放。以下範例示範在螢幕被觸碰時載入並播放動畫:

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);
}

值得注意的是,start() 方法不能寫在 onCreate() 中呼叫,因為此時要播放動畫的 View 尚未被加入視窗。若想讓動畫一進入就立刻播放,可以覆寫 onWindowFocusChanged() 方法,在 Activity 被放到最前方時自動執行。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved