編輯:Android 開發實例
這裡分享一個頗有意思的動畫效果:圖片的 3D 翻轉。下面的程式碼以單張圖片為例,基本可以直接跑起來。若有多張圖片要輪播,可以參考 APIDemo 裡的做法,加一個 ArrayAdapter 管理索引即可,改動不大,也能自行發揮做出更符合需求的版本。
在 FrameLayout 裡放一個 ImageView:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150px"
android:layout_y="30px"
android:src="@drawable/ro" />
</FrameLayout>
public class TestRotate extends Activity implements OnClickListener {
private ImageView imageview;
private ViewGroup mContainer;
/**
* 待顯示的圖片資源。若為多張圖片,可改成陣列:
* private static final int[] IMAGE = new int[]{
* R.drawable.ro,
* R.drawable.icon
* };
* 有多少張放多少張,這裡只做單張翻轉示範。
*/
private static final int IMAGE = R.drawable.ro;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview = (ImageView) findViewById(R.id.image);
mContainer = (ViewGroup) findViewById(R.id.container);
// 設定初始顯示的圖片(陣列時寫成 IMAGE[index])
imageview.setImageResource(IMAGE);
// 掛上點擊監聽
imageview.setClickable(true);
imageview.setFocusable(true);
imageview.setOnClickListener(this);
}
private void applyRotation(int position, float start, float end) {
// 以容器中心作為旋轉軸
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
final Rotate3d rotation =
new Rotate3d(start, end, centerX, centerY, 310.0f, true);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(position));
mContainer.startAnimation(rotation);
}
@Override
public void onClick(View v) {
/**
* 觸發翻轉。
* 三個參數依序為:目標位置、起始角度、結束角度。
* 若要回到上一張,把第一個參數設為 -1 即可。
*/
applyRotation(0, 0, 90);
}
private final class DisplayNextView implements Animation.AnimationListener {
private final int mPosition;
private DisplayNextView(int position) {
mPosition = position;
}
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews(mPosition));
}
public void onAnimationRepeat(Animation animation) {
}
}
/**
* 負責交換檢視,並啟動動畫的後半段。
*/
private final class SwapViews implements Runnable {
private final int mPosition;
public SwapViews(int position) {
mPosition = position;
}
public void run() {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
Rotate3d rotation;
if (mPosition > -1) {
imageview.setVisibility(View.VISIBLE);
imageview.requestFocus();
rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);
} else {
imageview.setVisibility(View.GONE);
rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
}
}
}
整個效果拆成前後兩半,靠動畫監聽串接:
前半加速、後半減速的組合,是讓翻轉看起來自然的關鍵。若兩段都用同一種插值,動畫會顯得生硬。
最後要提醒的是,程式中的 Rotate3d 是一個自訂的 Animation 子類,需要自行實作——核心是在 initialize() 中取得 Camera 並呼叫 rotateY(),於 applyTransformation() 中把矩陣應用上去,同時可加上透視修正避免兩側看起來不對稱。Android 的 APIDemo 中有完整的 Rotate3d 範例可直接參考。
Android 載入 Spinner:為未知的等待時間做一個轉不停的提示
使用者最討厭的不是慢,而是「不知道還要慢多久」。當一個操作沒有明確的完成百分比時,我們需要的不只是一個進度數字,而是一個持續轉動、告訴使用者「系統還活著」的提示。Andr
Android 載入動畫實作:ProgressBar 轉圈模式的三行關鍵字
使用者按下按鈕後,如果畫面完全沒反應,即使背景正在跑,他也只會覺得 App 當機了。一個小小的旋轉圓環,就能把「請稍候」這個訊息傳出去——這就是 ProgressBar
Android SharedPreferences 輕量級資料儲存完整說明與範例
幾乎每一款 App 都會有設定頁:要不要開啟通知、預設的伺服器位址、使用者上次輸入的暱稱。這類資料結構簡單、量體小、讀寫頻繁,用資料庫反而顯得笨重。Android 為此準
Android MediaPlayer 實戰:播放、暫停、快轉,一個都不能少
要在 App 裡放音樂或影片,Android 提供了 MediaPlayer 這個類別作為進入點。它被封裝在系統的媒體播放服務之上,讓你不必自己處理解碼細節,就能播放本機