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

Android 圖形動畫(四)AnimationListener 介紹與實例

編輯:關於 Android 程式設計

就像 Button 需要監聽器來接收點擊,動畫也有自己的監聽器。實作 AnimationListener 就能在動畫的不同階段插入自訂邏輯——動畫結束後移除 view、循環時播放音效、開始時打 log,都靠它。

三個回呼方法

private class MyListenr implements AnimationListener {
    @Override
    public void onAnimationEnd(Animation arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub
    }
}

各自代表的時機:

  • onAnimationStart:動畫開始執行時觸發,可以在此做準備工作。
  • onAnimationRepeat:動畫重複執行時觸發。注意它只在動畫設定了 repeatCount > 0 的情況下才會被呼叫,且每次重複呼叫一次。
  • onAnimationEnd:動畫執行完畢時觸發,最常見的用途就是在這裡移除或替換 view。

實例:淡出後自動移除圖片

下面這個例子點擊「刪除」按鈕,圖片慢慢淡去,動畫結束後真正從 ViewGroup 中移除;點擊「添加」則新增一個圖片並淡入。

public class MainActivity extends Activity {

    private Button button;
    private Button button2;
    private ImageView imageView;
    private ViewGroup viewGroup;

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

        button = (Button) findViewById(R.id.button_add);
        button2 = (Button) findViewById(R.id.button_delete);
        imageView = (ImageView) findViewById(R.id.imageView1);
        viewGroup = (ViewGroup) findViewById(R.id.viewGroup);

        button.setOnClickListener(new Mybutton());
        button2.setOnClickListener(new Mybutton());
    }

    private class Mybutton implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button_add:
                Add();
                break;
            case R.id.button_delete:
                Delete();
                break;
            default:
                break;
            }
        }
    }

    public void Add() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setStartOffset(500);

        ImageView imageViewAdd = new ImageView(MainActivity.this);
        imageViewAdd.setImageResource(R.drawable.ic_launcher);
        viewGroup.addView(imageViewAdd);
        imageViewAdd.startAnimation(alphaAnimation);
    }

    public void Delete() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setStartOffset(500);
        imageView.startAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new MyListenr());
    }

    private class MyListenr implements AnimationListener {
        @Override
        public void onAnimationEnd(Animation arg0) {
            viewGroup.removeView(imageView);
            Log.d("BruceZhang", "Animation End!");
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            Log.d("BruceZhang", "Animation Repeat!");
        }

        @Override
        public void onAnimationStart(Animation arg0) {
            Log.d("BruceZhang", "Animation Start!");
        }
    }

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

整個效果的核心在 onAnimationEnd 裡的 viewGroup.removeView(imageView):先讓圖片在視覺上淡出,等動畫真正結束才把它從檢視階層移除。若反過來先 remove 再播動畫,使用者就看不到任何效果了——「視覺先消失、物件後移除」是這類動畫的基本順序。

版面配置

注意根標籤上要給 ViewGroup 一個 id,程式碼才抓得到:

<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/viewGroup"
>

    <Button
        android:id="@+id/button_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_x="0dp"
        android:layout_y="367dp"
        android:text="添加圖片" />

    <Button
        android:id="@+id/button_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_x="0dp"
        android:layout_y="410dp"
        android:text="刪除圖片" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="153dp"
        android:layout_y="155dp"
        android:src="@drawable/ic_launcher" />

</AbsoluteLayout>

幾個實作上的提醒

Tween 動畫不會改變 View 的真實位置。這是最經典的坑:AlphaAnimation、TranslateAnimation 這類 Tween 動畫只是改變了「繪製時的外觀」,View 在版面配置系統裡的位置與大小完全沒變。所以位移動畫播完後,view 會「跳回」原位。要真正改變位置,得改用 Property Animation(ObjectAnimator、ViewPropertyAnimator),它動的是 View 的實際屬性。

repeated 使用要注意 removeView 的時機。若動畫設了重複次數,onAnimationEnd 只會在最後一次結束時呼叫一次,這通常是你要的行為;但若中途 Activity 被銷毀,仍要確保 view 參照不會造成洩漏。

建議改用 Toolbar 而非 AbsoluteLayout。本例用 AbsoluteLayout 是為了示範簡單,但它已被標示為不建議使用——不同螢幕尺寸下絕對座標會導致版面錯位。實務請改用 LinearLayout 或 ConstraintLayout。

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