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

Android 動畫效果(三):AnimationSet 與 Interpolator

編輯:關於 Android 程式設計

AnimationSet 是什麼

  1. 它是 Animation 的子類別。
  2. 一個 AnimationSet 裡可以裝一系列 Animation。
  3. 對 AnimationSet 設定的常見屬性(如 startOffset、duration),會被其中包含的動畫共同繼承。

Interpolator 決定「變化的快慢」

Interpolator 定義了動畫的變化速率,framework 內建的幾種:

  • AccelerateDecelerateInterpolator:頭尾較慢,中段加速。
  • AccelerateInterpolator:起步慢,之後持續加速。
  • CycleInterpolator:循環播放特定次數,速率沿正弦曲線變化。
  • DecelerateInterpolator:起步快,之後逐漸減速。
  • LinearInterpolator:全程等速。

讓兩個動畫同時發生

MainActivity.java:

public class MainActivity extends Activity {

    private Button button = null;
    private ImageView imageView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonId);
        button.setOnClickListener(new buttonListener());

        imageView = (ImageView) findViewById(R.id.imageViewId);
    }

    class buttonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            // 方法一:直接從 xml 載入一組動畫
            // Animation animation =
            //     AnimationUtils.loadAnimation(MainActivity.this, R.anim.flishes);
            // imageView.startAnimation(animation);

            // 方法二:用程式碼組出 AnimationSet
            AnimationSet animationSet = new AnimationSet(true); // true 表示共用一個 Interpolator
            animationSet.setInterpolator(new AccelerateDecelerateInterpolator());

            ScaleAnimation scaleAnimation = new ScaleAnimation(
                    1, 0.1f, 1, 0.1f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);

            RotateAnimation rotateAnimation = new RotateAnimation(
                    0, 360,
                    Animation.RELATIVE_TO_PARENT, 1f,
                    Animation.RELATIVE_TO_PARENT, 0f);

            animationSet.addAnimation(rotateAnimation);
            animationSet.addAnimation(scaleAnimation);
            animationSet.setDuration(2000);
            animationSet.setStartOffset(500);

            imageView.startAnimation(animationSet);
        }
    }
}

用 XML 描述同一組動畫

檔案放在 res/anim/flishes.xml:

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">

    <!-- shareInterpolator="true":讓底下的 scale、alpha 共用同一個 Interpolator -->

    <scale
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000" />

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500" />
</set>

這裡 startOffset 的效果值得留意:scale 先跑,alpha 延後 500 毫秒才啟動,於是視覺上會看到「先縮小、再淡出」的錯落感,而不是兩個效果同時開始。

activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="測試動畫效果" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageViewId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="50px"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</RelativeLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved