編輯:Android 開發實例
日前和一位朋友聊到 Android 上 3D 旋轉效果該怎麼做,今天剛好有空,把細節寫出來。
結論先說:在 Android 中要做出 3D 旋轉,只要 animation 搭配 Camera 就够了,ApiDemo 裡就有類似範例。
1 public class Rotate3d extends Animation {
2 private float mFromDegree;
3 private float mToDegree;
4 private float mCenterX;
5 private float mCenterY;
6 private float mLeft;
7 private float mTop;
8 private Camera mCamera;
9
10 public Rotate3d(float fromDegree, float toDegree, float left, float top,
11 float centerX, float centerY) {
12 this.mFromDegree = fromDegree;
13 this.mToDegree = toDegree;
14 this.mLeft = left;
15 this.mTop = top;
16 this.mCenterX = centerX;
17 this.mCenterY = centerY;
18 }
19
20 @Override
21 public void initialize(int width, int height, int parentWidth, int parentHeight) {
22 super.initialize(width, height, parentWidth, parentHeight);
23 mCamera = new Camera();
24 }
25
26 @Override
27 protected void applyTransformation(float interpolatedTime, Transformation t) {
28 final float FromDegree = mFromDegree;
29 float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime;
30 final float centerX = mCenterX;
31 final float centerY = mCenterY;
32 final Matrix matrix = t.getMatrix();
33
34 if (degrees <= -76.0f) {
35 degrees = -90.0f;
36 mCamera.save();
37 mCamera.rotateY(degrees);
38 mCamera.getMatrix(matrix);
39 mCamera.restore();
40 } else if (degrees >= 76.0f) {
41 degrees = 90.0f;
42 mCamera.save();
43 mCamera.rotateY(degrees);
44 mCamera.getMatrix(matrix);
45 mCamera.restore();
46 } else {
47 mCamera.save();
48 // 這裡很重要:先把 Camera 往 Z 軸拉遠一點,
49 // 避免接近 90 度時視野穿透造成閃爍
50 mCamera.translate(0, 0, centerX);
51 mCamera.rotateY(degrees);
52 mCamera.translate(0, 0, -centerX);
53 mCamera.getMatrix(matrix);
54 mCamera.restore();
55 }
56
57 matrix.preTranslate(-centerX, -centerY);
58 matrix.postTranslate(centerX, centerY);
59 }
60 }
幾個關鍵細節:applyTransformation() 裡的 interpolatedTime 是 0 到 1 的插值,用它把起始角與結束角做一次線性內插,動畫才會平滑;mCamera.save() 與 restore() 必須成對出現,否則矩陣狀態會互相污染;最後那兩行 pre/post translate 是把旋轉中心從左上角改到指定座標,少了它畫面就會繞著錯誤的原點轉。
1 // 這兩句是關鍵 2 Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY); 3 Rotate3d rightAnimation = new Rotate3d(-0 + 90, -90 + 90, 0.0f, 0.0f, mCenterX, mCenterY); 4 5 leftAnimation.setFillAfter(true); 6 leftAnimation.setDuration(1000); 7 rightAnimation.setFillAfter(true); 8 rightAnimation.setDuration(1000); 9 10 mImageView1.startAnimation(leftAnimation); 11 mImageView2.startAnimation(rightAnimation);
兩個動畫一正一反、時間相同,一張圖轉出去、另一張轉進來,看起來就是卡片翻面的效果。setFillAfter(true) 是必要的,否則動畫結束後 View 會瞬間跳回原始狀態。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="wrap_content" > 6 7 <FrameLayout 8 android:layout_width="fill_parent" 9 android:layout_height="fill_parent"> 10 11 <ImageView 12 android:id="@+id/image1" 13 android:layout_gravity="center_horizontal" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:src="@drawable/image1" /> 17 18 <ImageView 19 android:id="@+id/image2" 20 android:background="#ffff0000" 21 android:layout_gravity="center_horizontal" 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:src="@drawable/image2" /> 25 26 </FrameLayout> 27 </LinearLayout>
用 FrameLayout 把兩張圖疊在同一位置,動畫才能做出「互相取代」的錯覺——這也是 FrameLayout 最典型的用途。
動畫串接時若想用「轉完 A 自動接著轉 B」的效果,記得搭配 AnimationSet 或在動畫監聽器的 onAnimationEnd 裡啟動下一段,並留意 setFillAfter 與 setFillBefore 的組合,避免出現畫面閃跳。
轉自:https://www.cnblogs.com/jk1001/archive/2010/07/28/1787009.html
Android 自訂 View 與 ViewGroup 的繪製原理與實作
先認識 View Android 中所有控制項都是 View 或其子類。View 在螢幕上佔據的其實就是一個矩形區域,用 Rect 描述:left、top 是相對於父容器
Android 登入畫面實作教學:帳密驗證與三次錯誤鎖定
登入畫面是許多 App 的第一道門。使用者可能要先登入 Facebook、微博或其他服務才能使用完整功能。本章從版面出發,依序完成輸入欄位、帳密比對,以及防止連續猜錯的保
取得 SD 卡與機身儲存空間的總容量與可用大小
很多新手不知道,Android 早就把「查詢儲存空間」這件事包好了。不必自己去解析檔案系統、也不必硬算位元組,靠 StatFs 加上 Formatter 兩個類就能乾淨地
Android JSON 解析入門:JSONArray 與 JSONObject 的分工
JSON 是 JavaScript Object Notation 的縮寫,但它其實一點也不專屬於 JavaScript。因為語法極簡、幾乎所有語言都支援,它變成了跨平台