編輯:關於 Android 程式設計
下拉刷新時那個轉圈的加載動畫,其實就是一組幀圖在循環播放。要讓它能隨時開關、又能到處複用,最乾脆的做法是繼承 ImageView,把播放與停止包成方法,並掛在 setVisibility 上——顯示就播,隱藏就停。
package com.example.loading_drawable;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class MyImgView extends ImageView {
private AnimationDrawable bgAnim;
public MyImgView(Context context) {
super(context, null);
initView();
}
public MyImgView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
initView();
}
public MyImgView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
setBackgroundResource(R.drawable.flash_anim);
bgAnim = (AnimationDrawable) getBackground();
}
/** 開始播放 */
public void startAnim() {
if (bgAnim != null && !bgAnim.isRunning()) {
bgAnim.start();
}
}
/** 停止播放 */
public void stopAnim() {
if (bgAnim != null && bgAnim.isRunning()) {
bgAnim.stop();
}
}
/** 顯示時自動播放,隱藏時自動停止 */
@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if (visibility == View.VISIBLE) {
startAnim();
} else {
stopAnim();
}
}
}
這裡有個容易忽略的細節:三個建構子都要呼叫 initView()。因為程式碼 new 出來的 view 走的是一參數版本,XML 宣告的走兩參數版本,帶 defStyle 的則是三位元組版本。只寫一處,另一種建立方式就會拿到未初始化的空殼。
在 res/drawable/ 下建立 flash_anim.xml,用 animation-list 把各幀串起來。oneshot="false" 表示循環播放,每幀 duration 為 50 毫秒,換算下來約每秒 20 幀:
<?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/a01_02" android:duration="50" />
<item android:drawable="@drawable/a01_04" android:duration="50" />
<item android:drawable="@drawable/a01_06" android:duration="50" />
<item android:drawable="@drawable/a01_08" android:duration="50" />
<item android:drawable="@drawable/a01_10" android:duration="50" />
<item android:drawable="@drawable/a01_12" android:duration="50" />
<item android:drawable="@drawable/a01_14" android:duration="50" />
<item android:drawable="@drawable/a01_16" android:duration="50" />
<item android:drawable="@drawable/a01_25" android:duration="50" />
<item android:drawable="@drawable/a01_26" android:duration="50" />
<item android:drawable="@drawable/a01_27" android:duration="50" />
<item android:drawable="@drawable/a01_28" android:duration="50" />
<item android:drawable="@drawable/a01_30" android:duration="50" />
<item android:drawable="@drawable/a01_31" android:duration="50" />
<item android:drawable="@drawable/a01_32" android:duration="50" />
<item android:drawable="@drawable/a01_41" android:duration="50" />
<item android:drawable="@drawable/a01_42" android:duration="50" />
<item android:drawable="@drawable/a01_43" android:duration="50" />
<item android:drawable="@drawable/a01_44" android:duration="50" />
<item android:drawable="@drawable/a01_45" android:duration="50" />
<item android:drawable="@drawable/a01_46" android:duration="50" />
<item android:drawable="@drawable/a01_47" android:duration="50" />
<item android:drawable="@drawable/a01_48" android:duration="50" />
<item android:drawable="@drawable/a01_57" android:duration="50" />
<item android:drawable="@drawable/a01_58" android:duration="50" />
<item android:drawable="@drawable/a01_59" android:duration="50" />
<item android:drawable="@drawable/a01_60" android:duration="50" />
<item android:drawable="@drawable/a01_61" android:duration="50" />
<item android:drawable="@drawable/a01_62" android:duration="50" />
<item android:drawable="@drawable/a01_63" android:duration="50" />
<item android:drawable="@drawable/a01_64" android:duration="50" />
</animation-list>
這裡用純程式碼組版面,方便直接嵌進現有專案:
package com.example.loading_drawable;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
/** 慕課網風格的下拉刷新進度顯示控件 */
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);
rootLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
rootLayout.setGravity(Gravity.CENTER);
Button btn = new Button(this);
btn.setText("展現動畫");
final MyImgView imgView = new MyImgView(MainActivity.this);
imgView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
imgView.setVisibility(View.GONE); // 初始隱藏,動畫不會播放
rootLayout.addView(btn);
rootLayout.addView(imgView);
setContentView(rootLayout);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
imgView.setVisibility(View.VISIBLE); // 顯示即播放
}
});
}
}
這個做法的巧妙之處在於把「播放時機」綁進 setVisibility:呼叫端完全不需要記得去呼叫 startAnim(),只要照平常的方式控制顯示/隱藏,動畫就會跟著開關。用在下拉刷新、上拉加載這種「需要時才出現」的場景特別合適。
實務上要留意兩點。第一,幀動畫的所有圖片都會一次性載入記憶體,31 張圖若解析度偏高,記憶體佔用量會很可觀;圖片建議先壓到實際顯示尺寸,必要時改用 AnimationDrawable 搭配 Bitmap 池或替換成 ProgressBar/屬性動畫實作。第二,start() 在 view 尚未 attach 到視窗時不會生效,若在 onCreate 中立刻顯示,記得用 post() 延後一輪。
Android 啟動頁優化:消除黑屏與應用秒開技巧
啟動時的黑屏,通常出現在第一幀畫面之前。若想消除黑屏,可以自訂啟動頁主題,把 windowBackground 設為品牌圖,或使用透明主題延後顯示。 自訂啟動背景 sty
Android Studio 官方文檔導讀:初識 Android Studio
一、前言 Android Studio 是官方基於 IntelliJ IDEA 整合打造的 Android 應用開發環境。在 IntelliJ 強大的代碼編輯器與開發工具
Android 通知開發入門:Notification、PendingIntent 與自定義通知
從通知到可互動的系統提示 在 Android 中,Notification 不只是短暫的提示,更是應用與使用者溝通的重要橋梁。當系統發出通知時,狀態列會出現圖示,使用者下
Android DevicePolicyManager:設備管理與鎖屏控制實作
DevicePolicyManager 是 Android 提供的設備管理服務,可讓企業或系統管理應用程式執行鎖屏、設定密碼、停用相機與恢復出廠設定等操作。 運作前提 使