編輯:初階開發
Android 的動畫既可以在 XML 中定義,也可以完全用程式碼產生。下面以 RotateAnimation 為例示範兩種寫法,其他類型(ScaleAnimation、AlphaAnimation、TranslateAnimation)的原理完全一樣,只是參數不同。
在 res/anim 下建立 my_rotate_action.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="3000" />
</set>
屬性說明:
interpolator:指定動畫的插入器,用來控制速度變化曲線。accelerate_decelerate 是「先加速後減速」,另有 accelerate(愈來愈快)、decelerate(愈來愈慢)、linear(等速)等常見選項。
fromDegrees:動畫起始時物件的角度。
toDegrees:動畫結束時的角度,正值代表順時針。
duration:動畫持續時間,單位為毫秒。
在 Java 中載入並套用,效果是點擊按鈕後 TextView 旋轉一週:
package com.ray.animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class TestAnimation extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);
findViewById(R.id.TextView01).startAnimation(anim);
}
}
package com.ray.animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
public class TestAnimation extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Button);
btn.setOnClickListener(this);
}
public void onClick(View v) {
Animation anim = new RotateAnimation(0.0f, 360.0f);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(3000);
findViewById(R.id.TextView01).startAnimation(anim);
}
}
兩種做法效果相同,選擇標準很實際:參數固定、設計師會一起參與調校的,寫在 XML 裡比較方便;參數需要依執行期條件計算(例如依目前角度決定旋轉量)的,就用程式碼。
private AlphaAnimation myAnimation_Alpha; // fromAlpha:起始透明度;toAlpha:結束透明度 // 0.0 表示完全透明,1.0 表示完全不透明 myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f); myAnimation_Alpha.setDuration(5000); // 持續 5000 毫秒
常見用法是淡入(0 → 1)與淡出(1 → 0)。
// 參數依序:fromX, toX, fromY, toY,
// pivotXType, pivotXValue, pivotYType, pivotYValue
//
// 前四個是 X / Y 軸的伸縮比例:
// 0.0 表示收縮到沒有,1.0 表示正常無伸縮
// 小於 1.0 為收縮,大於 1.0 為放大
//
// 後四個決定縮放的「中心點」在哪裡
myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Scale.setDuration(700);
這裡的 pivot(樞點)是最容易搞錯的部分。RELATIVE_TO_SELF 搭配 0.5f 代表「相對於自身尺寸的 50% 處」,也就是正中心;若想從左下角放大,就改成 X 用 0f、Y 用 1f。另外還有 RELATIVE_TO_PARENT(相對於父容器)與 ABSOLUTE(絕對像素座標)兩種型別。
// 參數依序:fromXDelta, toXDelta, fromYDelta, toYDelta myAnimation_Translate = new TranslateAnimation(0f, 200f, 0f, 0f); myAnimation_Translate.setDuration(2000);
這裡的數值同樣可以用 Animation.RELATIVE_TO_SELF 搭配比例來表達,寫成 1.0f 就是「移動自身一個寬度」。
// 參數依序:fromDegrees, toDegrees,
// pivotXType, pivotXValue, pivotYType, pivotYValue
myAnimation_Rotate = new RotateAnimation(0.0f, 350.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Rotate.setDuration(3000);
旋轉同樣需要指定中心點,省略 pivot 參數的建構子預設以 (0, 0) 為中心,也就是物件的左上角,這往往不是你要的效果。
使用從 View 父類繼承而來的 startAnimation(),就能為 View 或其子類套用一個動畫效果:
view.startAnimation(anim);
若要讓動畫跑完後保持最後的視覺狀態,記得加上 anim.setFillAfter(true),否則物件會「跳回」原位。
上面這些屬於 View Animation(補間動畫),也就是 Tween Animation。它的特性是:只改變畫面的呈現,不真正改變物件的屬性。也就是說,一個按鈕被平移到別的位置之後,你點擊螢幕上的新位置,觸發的其實仍是它原本所在的區域。若你需要「移動後真的改到位置」,應該改用 Property Animation(API 11 起提供):
ObjectAnimator.ofFloat(view, "translationX", 0f, 300f).setDuration(2000).start(); view.animate().alpha(0f).setDuration(500);
Property Animation 直接操作物件的屬性值,動畫結束後狀態就是真的改變了,也更容易與其他動畫串接組合。新專案若要處理互動型動畫,建議直接採用這套 API;補間動畫則仍很適合「跑完就恢復原狀」的裝飾性效果。
此外,若動畫需要在版面配置的過程中觸發(例如新增項目時的進場效果),可以搭配 LayoutTransition 或在 LayoutManager 中設定,比手動呼叫 startAnimation 更省事。
10 款對 Android 開發者實用的手機應用
以下整理 10 款對開發者相當實用的 Android 應用,涵蓋遠端連線、檔案傳輸、程式編輯、資料庫操作與色彩取樣等情境。若你手上也有值得推薦的工具,歡迎補充。 1.
Android 圓形進度條顏色自訂教學
最近因專案需要研究 ListView 捲動時的資料動態更新,其中載入過程要顯示圓形進度條,卡住的地方是顏色設定。以下把查到的資料與自己的實作結果整理出來。 版面配置 在
Android 輕鬆實現語音識別功能
Android 本身就內建了語音識別的能力,多數情況下你不需要自己訓練模型,只要發一個 RecognizerIntent,系統就會叫出合適的應用來聽使用者說話,再把結果回
在 XP 主機的 VirtualBox Ubuntu 中成功掛載共享資料夾
原本打算在虛擬機的 Ubuntu 裡編譯 Android 原始碼,但直接從網路下載老是出問題。所幸 Windows 底下已經把 Android 原始碼抓好了,於是目標變成