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

Android 補間動畫四種效果:Alpha、Rotate、Scale、Translate 與複合動畫

編輯:關於 Android 程式設計

Android 的補間動畫共有四種基本型態:透明度漸變(Alpha)、旋轉(Rotate)、縮放(Scale)、位移(Translate)。把它們用 AnimationSet 疊在一起,還能做出複合效果。以下用五顆按鈕分別觸發,點下去就能看到對應動畫。

Activity 程式碼

package com.tmacsky;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationDemoActivity extends Activity {

    private ImageView imageView;

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

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

        Button alpha = (Button) findViewById(R.id.Alpha);
        alpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));

        Button rotate = (Button) findViewById(R.id.Rotate);
        rotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));

        Button scale = (Button) findViewById(R.id.Scale);
        scale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));

        Button translate = (Button) findViewById(R.id.Translate);
        translate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));

        Button complex = (Button) findViewById(R.id.Complex);
        complex.setOnClickListener(new AnimationClickListener(AnimationType.Complex));
    }

    /** 用列舉把「按鈕」與「動畫種類」綁在一起,避免寫五個匿名類別 */
    enum AnimationType { Alpha, Rotate, Scale, Translate, Complex }

    class AnimationClickListener implements OnClickListener {

        private AnimationType animationType;

        public AnimationClickListener(AnimationType animType) {
            animationType = animType;
        }

        public void onClick(View v) {
            switch (animationType) {

                case Alpha:
                    // 從 XML 載入漸變動畫
                    AlphaAnimation alphaAnimation = (AlphaAnimation)
                            AnimationUtils.loadAnimation(AnimationDemoActivity.this, R.anim.alpha);
                    imageView.startAnimation(alphaAnimation);
                    break;

                case Rotate:
                    // 以自身中心為軸旋轉一圈,重複三次
                    RotateAnimation rotateAnimation = new RotateAnimation(
                            0, 360,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    rotateAnimation.setDuration(3000);
                    rotateAnimation.setRepeatCount(3);
                    imageView.startAnimation(rotateAnimation);
                    break;

                case Scale:
                    // 以自身中心為基準放大到 1.5 倍,重複三次
                    ScaleAnimation scaleAnimation = new ScaleAnimation(
                            1, 1.5f, 1, 1.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    scaleAnimation.setDuration(3000);
                    scaleAnimation.setRepeatCount(3);
                    imageView.startAnimation(scaleAnimation);
                    break;

                case Translate:
                    // 從自身位置移動到兩倍寬/高的位置,重複三次
                    TranslateAnimation translateAnimation = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, 2,
                            Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, 2);
                    translateAnimation.setDuration(3000);
                    translateAnimation.setRepeatCount(3);
                    imageView.startAnimation(translateAnimation);
                    break;

                case Complex:
                    // 四種動畫同時播放
                    AnimationSet sets = new AnimationSet(false);

                    AlphaAnimation alpha1 = new AlphaAnimation(1f, 0.1f);
                    alpha1.setDuration(3000);

                    RotateAnimation rotate1 = new RotateAnimation(
                            0, 360,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    rotate1.setDuration(3000);

                    ScaleAnimation scale1 = new ScaleAnimation(
                            1, 1.5f, 1, 1.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    scale1.setDuration(3000);

                    TranslateAnimation translate1 = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, 2,
                            Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, 2);
                    translate1.setDuration(3000);

                    sets.addAnimation(alpha1);
                    sets.addAnimation(rotate1);
                    sets.addAnimation(scale1);
                    sets.addAnimation(translate1);
                    imageView.startAnimation(sets);
                    break;

                default:
                    break;
            }
        }
    }
}

版面配置 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/qa" />

    <Button android:id="@+id/Alpha"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Alpha" />
    <Button android:id="@+id/Rotate"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Rotate" />
    <Button android:id="@+id/Scale"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Scale" />
    <Button android:id="@+id/Translate"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Translate" />
    <Button android:id="@+id/Complex"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Complex" />

</LinearLayout>

動畫資源 res/anim/alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0.3"
    android:duration="2000"
    android:repeatCount="3" />

幾個關鍵觀念

AnimationListener 的三個回呼。若需要知道動畫何時開始、重複、結束,可以掛上 Animation.AnimationListener,分別實作 onAnimationStart、onAnimationRepeat、onAnimationEnd。這是串接動畫序列(例如第一段結束後播放第二段)的標準做法。

RELATIVE_TO_SELF 與 pivot 的關係。Animation.RELATIVE_TO_SELF, 0.5f 表示軸心位於自身寬/高的 50%,也就是正中心。若想讓旋轉圍繞左上角,改成 0f 即可。

AnimationSet 的 shareInterpolator。建構子傳入 false 表示各子動畫保留自己的插值器;傳 true 則共用同一組。做複合效果時通常給 false,讓每個動畫各自調整速度曲線。

補間動畫不改變真實位置。這是最容易被忽略的一點:動畫結束後 View 會瞬間回到原點,且點擊熱區仍在原本位置。若需要真正改變位置或讓狀態保留,請改用 ObjectAnimator/ViewPropertyAnimator 這類 Property Animation。

最後,若要在 XML 中做複合動畫,可以把上述四個標籤包在 <set> 裡,並用 android:interpolator 統一設定速度曲線,效果與程式碼版本等價,但更容易調整與重複使用。

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