編輯:關於 Android 程式設計
這篇聚焦在 Animation 的 AnimationListener——它讓你能在動畫的開始、結束、重複這三個時機插入自己的邏輯。下面的範例做了一個「淡入淡出切換 ImageView」的小效果。
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;
}
}
<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,淡出效果根本看不到。把「視覺效果」與「實際資料變更」分離到不同時機,是動畫開發中很值得養成的習慣。
Android Studio × OpenGL ES 3.0 增強實境開發(一):建立 NDK 開發環境
一、什麼是 NDK,什麼是 JNI? NDK(Native Development Kit,原生開發工具包)允許開發者以 C / C++ 等原生語言撰寫部分程式。它主要包
Activity 啟動模式與 Task 返回堆疊詳解
Task 與 Back Stack 的基本概念 Activity 的啟動模式,本質上是在定義 Activity 實例與目前 Task 之間的關聯方式。因此,要理解啟動模式
【React Native 開發】ToolbarAndroid 工具欄控件詳解與實踐(15)
(一)前言 今天我們一起來深入解析 React Native 中的工具欄控件 ToolbarAndroid,並結合最佳實踐進行講解。 該 ToolbarAndroid 組
Android JNI 入門實戰:從 Java 層呼叫本地 C 函數
當一個 App 同時混用了 Java 與 C/C++ 代碼時,就需要 Java Native Interface(JNI)作為兩者之間的橋樑。JNI 既是一層軟體介面,也