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

Android 動畫筆記(五):AnimationListener 與生命週期回調

編輯:關於 Android 程式設計

這篇聚焦在 Animation 的 AnimationListener——它讓你能在動畫的開始、結束、重複這三個時機插入自己的邏輯。下面的範例做了一個「淡入淡出切換 ImageView」的小效果。

MainActivity.java

package com.example.animationlistener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ViewGroup viewGroup = null;
    private Button addButton = null;
    private Button removeButton = null;
    private ImageView imageView = null;

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

        addButton = (Button) findViewById(R.id.addButton);
        addButton.setOnClickListener(new AddButtonListener());

        removeButton = (Button) findViewById(R.id.removeButton);
        removeButton.setOnClickListener(new RemoveButtonListener());

        viewGroup = (ViewGroup) findViewById(R.id.layout_id);
        imageView = (ImageView) findViewById(R.id.myImage);
    }

    /** 淡出並移除 ImageView */
    class RemoveButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
            alphaAnimation.setDuration(2000);
            // 掛上監聽器,動畫結束後才真正移除 View
            alphaAnimation.setAnimationListener(new RemoveAnimationListener());
            imageView.startAnimation(alphaAnimation);
        }
    }

    /** 建立 ImageView 並淡入 */
    class AddButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
            alphaAnimation.setDuration(2000);

            ImageView addImageView = new ImageView(MainActivity.this);
            addImageView.setId(R.id.myImage);
            addImageView.setImageResource(R.drawable.ic_launcher);

            // viewGroup 對應 main.xml 中的整個版面
            viewGroup.addView(addImageView,
                    new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            imageView = addImageView;
            addImageView.startAnimation(alphaAnimation);
        }
    }

    /** 動畫生命週期回調 */
    class RemoveAnimationListener implements AnimationListener {

        // 動畫結束時呼叫:這裡才把 View 從版面移除
        @Override
        public void onAnimationEnd(Animation animation) {
            System.out.println("Animation end");
            viewGroup.removeView(imageView);
        }

        // 動畫重複執行時呼叫
        @Override
        public void onAnimationRepeat(Animation animation) {
            System.out.println("Animation repeat");
        }

        // 動畫開始時呼叫
        @Override
        public void onAnimationStart(Animation animation) {
            System.out.println("Animation start");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

版面配置 main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/layout_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/myText"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/addButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/myImage"
        android:text="add image" />

    <Button
        android:id="@+id/removeButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/addButton"
        android:text="remove image" />

</RelativeLayout>

三個回調時機的實際用途

onAnimationStart():動畫剛啟動時觸發,適合做狀態初始化,例如停用互動控制項。

onAnimationRepeat():動畫循環執行時觸發。要注意它只在設定了 repeatCount 大於 0 的情況下才會被呼叫。

onAnimationEnd():動畫播完時觸發。這個回調最實用——範例中正是靠它來把 ImageView 從版面移除。

為什麼要放在 onAnimationEnd 而不是直接移除?因為 AlphaAnimation 只是改變了顯示的 alpha 值,View 本身還在版面裡。如果在動畫開始時就 removeView,淡出效果根本看不到。把「視覺效果」與「實際資料變更」分離到不同時機,是動畫開發中很值得養成的習慣。

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