編輯:關於 Android 程式設計
合適的動畫能顯著改善操作手感。這篇把最基礎的四種 View 動畫和組合動畫走一遍。
先看守屏:

MainActivity 中五個按鈕共用一個點擊入口,按 id 分派到不同的 Animation:
public class MainActivity extends Activity {
private ImageView imgObj;
private Animation animation;
private AnimationSet animationSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgObj = findViewById(R.id.imgid);
}
public void animationshow(View view) {
switch (view.getId()) {
case R.id.btn01id:
// 透明度動畫:fromAlpha 起始透明度,toAlpha 結束透明度
// 0.0f 全透明,1.0f 完全不透明
animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn02id:
// 縮放動畫:前兩個參數是 x 軸比例,中間兩個是 y 軸比例,
// 最後兩個是縮放中心座標(1.0f 表示控件本身尺寸)
animation = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 50.0f, 50.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn03id:
// 位移動畫:起始與結束的 x、y 偏移量
animation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn04id:
// 旋轉動畫:起始角度、結束角度、旋轉中心
animation = new RotateAnimation(0.0f, 360.0f, 50.0f, 50.0f);
animation.setDuration(3000);
imgObj.setAnimation(animation);
break;
case R.id.btn05id:
// 組合動畫:AnimationSet(true) 表示使用同一個插值器協調
AlphaAnimation a1 = new AlphaAnimation(0.0f, 1.0f);
ScaleAnimation a2 = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 50.0f, 50.0f);
TranslateAnimation a3 = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
RotateAnimation a4 = new RotateAnimation(0.0f, 360.0f, 50.0f, 50.0f);
animationSet = new AnimationSet(true);
animationSet.addAnimation(a1);
animationSet.addAnimation(a2);
animationSet.addAnimation(a3);
animationSet.addAnimation(a4);
animationSet.setDuration(4000);
imgObj.setAnimation(animationSet);
break;
}
}
}
佈局部分,五個按鈕通過 android:onClick 綁定同一個方法:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imgid"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_weight="1"
android:src="@drawable/img" />
<Button
android:id="@+id/btn01id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="透明動畫"
android:textSize="20sp" />
<Button
android:id="@+id/btn02id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="漸變動畫"
android:textSize="20sp" />
<Button
android:id="@+id/btn03id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="位移動畫"
android:textSize="20sp" />
<Button
android:id="@+id/btn04id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="旋轉動畫"
android:textSize="20sp" />
<Button
android:id="@+id/btn05id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="animationshow"
android:text="動畫組合"
android:textSize="20sp" />
</LinearLayout>
使用的圖片資源:

幾點實踐提醒:View 動畫只改變繪製時的矩陣,控件的真實位置、寬高和點擊區域都不變,所以位移後原本的位置依然可點,這是它最容易讓人困惑的地方;想要真正改變布局屬性,得用属性動畫(ObjectAnimator / ValueAnimator)。另外 AnimationSet 構造時傳 true 表示各子動畫共用插值器,若希望它們各跑各的曲線則傳 false。動畫結束後若不想讓控件跳回原位,記得設置 setFillAfter(true)。
Android aapt 打包工具詳解:常用命令與 APK 資源處理
aapt 是 Android Asset Packaging Tool 的縮寫,是 Android 建置流程中非常重要的資源打包工具。它位於 SDK 的 build-to
AsyncTask 異步任務:用法、源碼原理與實踐陷阱
搞懂 Android 的緩存機制之後,我打算自己動手寫一個基於 LruCache 與 DiskLruCache 二級緩存的輕量級圖片請求框架。搭框架時卡在了一個問題上:下
Android 多語言切換要點整理
一、切換為英文的程式碼 只要在切換時透過 Locale 與 Configuration 更新資源的語言設定,後續開啟的子視窗就會自動改從 values-en 目錄下的 s
深入解析 UIView 的繪製與渲染機制
對我來說,App 頁面的優化一直是個難題,最近持續在學習和歸納,發現頁面優化歸根究底離不開 View 的繪製與渲染機制。網路上有許多精彩的部落格文章,本篇借鑒了前輩們大量