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

Android 動畫全解:變換動畫、幀動畫與佈局動畫

編輯:關於 Android 程式設計

面試中經常被問到的一個問題是:Android 一共有哪幾類動畫?先記住這份清單——變換動畫(透明度、縮放、平移、旋轉)、逐幀動畫、佈局動畫、屬性動畫。

一、變換動畫(View Animation)

變換動畫既可以寫在 XML 裡,也可以用 Java 代碼創建。經驗法則是:當動畫參數需要動態計算時用代碼更靈活;參數固定的場景則寫 XML 更省事。

1. 透明度動畫

XML 方式,在 res/anim 下新建 alphademo.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <!-- alpha 取值 0.0(全透明)~1.0(完全不透明) -->
</set>

加載並啟動:

Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alphademo);
imageView.startAnimation(alphaAnimation);

代碼方式:

// 參數一:起始透明度;參數二:結束透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 0.8f);
alphaAnimation.setDuration(3000); // 持續時間,單位毫秒
imageView.startAnimation(alphaAnimation);

2. 縮放動畫

scaledemo.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <scale
        android:duration="2000"
        android:fromXScale="0.2"   <!-- X 起始比例,小於 1 為縮小 -->
        android:toXScale="1.2"     <!-- X 結束比例 -->
        android:fromYScale="0.0"
        android:toYScale="1"
        android:pivotX="50%"       <!-- 縮放基準點 X -->
        android:pivotY="50%"       <!-- 縮放基準點 Y -->
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fillAfter="true" /> <!-- 動畫結束後保持末態 -->
</set>
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scaledemo);
imageView.startAnimation(animation);

代碼方式:

// 參數依次為:X 起點、X 終點、Y 起點、Y 終點
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 0.0f, 1.0f);
scaleAnimation.setDuration(2000);
scaleAnimation.setFillAfter(true);
imageView.startAnimation(scaleAnimation);

3. 平移動畫

XML 方式與啟動代碼:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.translatedemo);
imageView.startAnimation(animation);

代碼方式:

TranslateAnimation translateAnimation = new TranslateAnimation(10.0f, 200f, 30f, 210f);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(true);
imageView.startAnimation(translateAnimation);

4. 旋轉動畫

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate
        android:duration="2000"
        android:fromDegrees="45"   <!-- 起始角度 -->
        android:toDegrees="60"     <!-- 結束角度,可以大於 360 -->
        android:fillAfter="true" />
    <!-- 角度為負表示逆時針,為正表示順時針;
         pivotX / pivotY 為相對於物件自身的基準點,取值 0%~100%,50% 即中心 -->
</set>
RotateAnimation rotateAnimation = new RotateAnimation(0f, -30f);
rotateAnimation.setDuration(2000);
rotateAnimation.setFillAfter(true);
imageView.startAnimation(rotateAnimation);

5. 組合動畫與動畫監聽

透過 Animation.AnimationListener 可以把多段動畫串接起來,例如旋轉結束後再做平移:

rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) { }

    @Override
    public void onAnimationEnd(Animation animation) {
        TranslateAnimation translateAnimation = new TranslateAnimation(0f, 200f, 0f, 50f);
        translateAnimation.setFillAfter(true);
        translateAnimation.setDuration(3000);
        imageView.startAnimation(translateAnimation);
    }

    @Override
    public void onAnimationRepeat(Animation animation) { }
});

6. 多個動畫同時播放

XML 中把多個動畫寫進同一個 <set> 即可;代碼方式則需要借助 AnimationSet:

AnimationSet animationSet = new AnimationSet(true);

TranslateAnimation translateAnimation = new TranslateAnimation(0f, 200f, 0f, 50f);
translateAnimation.setDuration(3000);
animationSet.addAnimation(translateAnimation); // 記得加入動畫集

RotateAnimation rotateAnimation = new RotateAnimation(0f, -30f, 0.5f, 0.5f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);

imageView.startAnimation(animationSet); // 傳入的是 AnimationSet

二、逐幀動畫(Frame Animation)

逐幀動畫的 XML 放在 drawable 目錄下,根元素是 animation-list,子元素為 item:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/loading1" android:duration="200" />
    <item android:drawable="@mipmap/loading2" android:duration="200" />
    <item android:drawable="@mipmap/loading3" android:duration="200" />
    <item android:drawable="@mipmap/loading4" android:duration="200" />
</animation-list>

代碼側拿到 AnimationDrawable 並作為背景設置,然後調用 start():

public class FrameAnimationActivity extends AppCompatActivity {
    private AnimationDrawable animationDrawable;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_animation);
        imageView = (ImageView) findViewById(R.id.image_view);
        animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);
        imageView.setBackground(animationDrawable);
    }

    public void startAnimation(View view) {
        animationDrawable.start(); // 停止用 stop()
    }
}

三、佈局動畫(Layout Animation)

佈局動畫由 LayoutAnimationController 驅動,用來為某個 layout 或 ViewGroup 中的子控制項批量設置入場動畫。下面的例子讓 ListView 的每個條目依次從右側滑入並淡現。

public class LayoutAnimationActivity extends AppCompatActivity {
    private ListView listView;
    private List<String> list;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_animation);
        listView = (ListView) findViewById(R.id.listview);
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("條目 " + i);
        }
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);

        AnimationSet set = new AnimationSet(true);
        Animation alpha = new AlphaAnimation(0.0f, 1.0f);
        alpha.setDuration(2000);
        set.addAnimation(alpha);

        Animation translate = new TranslateAnimation(100f, 10f, 0, 0);
        translate.setDuration(2000);
        set.addAnimation(translate);

        LayoutAnimationController controller = new LayoutAnimationController(set);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL); // 依次出現
        listView.setLayoutAnimation(controller);
        listView.startLayoutAnimation();
    }
}

最後要補充的是屬性動畫(Property Animation)。它在 API 11 之後引入,直接操作物件的屬性值,突破了變換動畫「只改畫面不改屬性」的限制,也是現代 Android 動畫開發的首選方案。

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