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

Android 四種基礎動畫:透明度、縮放、位移與旋轉

編輯:關於 Android 程式設計

合適的動畫能顯著改善操作手感。這篇把最基礎的四種 View 動畫和組合動畫走一遍。

先看守屏:

MainActivity 中五個按鈕共用一個點擊入口,按 id 分派到不同的 Animation:

public class MainActivity extends Activity {
    private ImageView imgObj;
    private Animation animation;
    private AnimationSet animationSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgObj = findViewById(R.id.imgid);
    }

    public void animationshow(View view) {
        switch (view.getId()) {
            case R.id.btn01id:
                // 透明度動畫:fromAlpha 起始透明度,toAlpha 結束透明度
                // 0.0f 全透明,1.0f 完全不透明
                animation = new AlphaAnimation(0.0f, 1.0f);
                animation.setDuration(3000);
                imgObj.setAnimation(animation);
                break;

            case R.id.btn02id:
                // 縮放動畫:前兩個參數是 x 軸比例,中間兩個是 y 軸比例,
                // 最後兩個是縮放中心座標(1.0f 表示控件本身尺寸)
                animation = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 50.0f, 50.0f);
                animation.setDuration(3000);
                imgObj.setAnimation(animation);
                break;

            case R.id.btn03id:
                // 位移動畫:起始與結束的 x、y 偏移量
                animation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
                animation.setDuration(3000);
                imgObj.setAnimation(animation);
                break;

            case R.id.btn04id:
                // 旋轉動畫:起始角度、結束角度、旋轉中心
                animation = new RotateAnimation(0.0f, 360.0f, 50.0f, 50.0f);
                animation.setDuration(3000);
                imgObj.setAnimation(animation);
                break;

            case R.id.btn05id:
                // 組合動畫:AnimationSet(true) 表示使用同一個插值器協調
                AlphaAnimation a1 = new AlphaAnimation(0.0f, 1.0f);
                ScaleAnimation a2 = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 50.0f, 50.0f);
                TranslateAnimation a3 = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
                RotateAnimation a4 = new RotateAnimation(0.0f, 360.0f, 50.0f, 50.0f);

                animationSet = new AnimationSet(true);
                animationSet.addAnimation(a1);
                animationSet.addAnimation(a2);
                animationSet.addAnimation(a3);
                animationSet.addAnimation(a4);
                animationSet.setDuration(4000);
                imgObj.setAnimation(animationSet);
                break;
        }
    }
}

佈局部分,五個按鈕通過 android:onClick 綁定同一個方法:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imgid"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:src="@drawable/img" />

    <Button
        android:id="@+id/btn01id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="透明動畫"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn02id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="漸變動畫"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn03id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="位移動畫"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn04id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="旋轉動畫"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn05id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="動畫組合"
        android:textSize="20sp" />
</LinearLayout>

使用的圖片資源:

幾點實踐提醒:View 動畫只改變繪製時的矩陣,控件的真實位置、寬高和點擊區域都不變,所以位移後原本的位置依然可點,這是它最容易讓人困惑的地方;想要真正改變布局屬性,得用属性動畫(ObjectAnimator / ValueAnimator)。另外 AnimationSet 構造時傳 true 表示各子動畫共用插值器,若希望它們各跑各的曲線則傳 false。動畫結束後若不想讓控件跳回原位,記得設置 setFillAfter(true)。

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