編輯:關於 Android 程式設計
在 Android 中,Animation 為元件提供了豐富的動畫能力。本文將介紹四種基本動畫,並說明如何透過 Interpolator 控制變化節奏、以 AnimationListener 監聽動畫事件,以及逐幀動畫與 LayoutAnimationController 的應用。
以程式碼實作:
ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(1000); alphaAnimation.setStartOffset(10000); animationSet.addAnimation(alphaAnimation); animationSet.setFillBefore(false); animationSet.setFillAfter(true); imageView.startAnimation(animationSet);
以 XML 實作,先在 res/anim 建立 alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fillAfter="true"
android:fillBefore="false">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="1000"
android:duration="1000" />
</set>
再於 Activity 中載入:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 1f);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(1000);
imageView.startAnimation(animationSet);
對應的 XML(scale.xml):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_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" />
</set>
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
對應的 XML(rotate.xml):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<rotate android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" />
</set>
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
對應的 XML(translate.xml),其中 100%p 表示相對於父容器的位置:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="1000" />
</set>
可透過 AnimationSet 為單一元件添加多個動畫,程式碼寫法:
AnimationSet animationSet = new AnimationSet(false);
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(alpha);
animationSet.addAnimation(scale);
animationSet.setDuration(2000);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
imageView.startAnimation(animationSet);
XML 寫法(alpha.xml):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
android:fillAfter="true">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="1000"
android:fillAfter="true"
android:duration="2000" />
<scale android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="1000"
android:duration="2000" />
</set>
Interpolator 定義了動畫變化的速率或規律,常見的子類包含:
AccelerateDecelerateInterpolator 開始與結束較慢,中段加速 AccelerateInterpolator 起步慢而後加速 CycleInterpolator 以正弦規律重複指定次數 DecelerateInterpolator 起步快而後減速 LinearInterpolator 等速變化
可在 set 標籤中統一設定,或為每個動畫標籤分別指定:
// set 標籤中定義
<set ...
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true" ...>
// 於程式碼中設定
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
// 或分別設定
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setInterpolator(new AccelerateInterpolator());
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setInterpolator(new AccelerateDecelerateInterpolator());
先準備好 run1.png ~ run4.png 四張圖片放入 drawable 目錄,建立 anim_run.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/run1" android:duration="100" />
<item android:drawable="@drawable/run2" android:duration="100" />
<item android:drawable="@drawable/run3" android:duration="100" />
<item android:drawable="@drawable/run4" android:duration="100" />
</animation-list>
於 Activity 中設定背景並啟動動畫:
imageView.setBackgroundResource(R.drawable.anim_run); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start();
先建立控制動畫的 list_anim.xml,再於版面配置中加入 ListView,最後透過程式碼或 XML 設定:
// 程式碼方式
Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
LayoutAnimationController lac = new LayoutAnimationController(animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
lac.setDelay(0.5f);
listView.setLayoutAnimation(lac);
// XML 方式:建立 anim_layout.xml
<layoutAnimation xmlns:android="https://schemas.android.com/apk/res/android"
android:delay="1"
android:animationOrder="normal"
android:animation="@anim/list_anim" />
可為元件設定監聽器,覆寫三個回呼方法:
private class RemoveAnimationListener implements AnimationListener{
@Override
public void onAnimationEnd(Animation animation) {
// 動畫結束後刪除某個 TextView
viewGroup.removeView(textView);
}
@Override
public void onAnimationRepeat(Animation animation) {
System.out.println("onAnimationRepeat");
}
@Override
public void onAnimationStart(Animation animation) {
System.out.println("onAnimationStart");
}
}
透過監聽器,就能在動畫開始、重複與結束的各個階段插入自訂邏輯,例如完成淡出後移除元件,或反之在動畫中動態加入元件。
Android 自定義 View:用觀察者模式實作字母索引欄
字母索引欄常見於通訊錄、音樂與相簿。這個自定義 View 會依觸控位置判斷目前選到的字母,並透過監聽器回傳給 Activity。 實作時,先定義 OnItemListe
Android Activity 四種 launchMode 啟動模式完整解析
Activity 的啟動模式決定了實例創建、任務棧管理與頁面復用方式。Android 中常見的 launchMode 有四種:standard、singleTop、sin
Android 視頻錄製常見問題整理
在 Android 中實作視頻錄製時,常會遇到流程設計、預覽顯示、清晰度、檔案大小與影片方向等問題。本文整理常見環節,提供開發時的參考方向。 錄製流程 以常見的短視頻錄製
Android 跨進程通訊解析(二):ContentProvider 實作與特性
一、ContentProvider 簡介 在 Android 中,有些資料(如通訊錄、音訊、視訊檔等)需要供多個應用程式共用。為了更好地對外提供資料,系統提供了 Cont