Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android系列之淺談Android 3D旋轉

Android系列之淺談Android 3D旋轉

編輯:Android開發實例

最近和一哥們在聊Android的3D旋轉效果的技術實現的事情,今天正好有點時間就把技術細節寫了出來

在ANDROID中實現3D旋轉直接使用animation配合camera就可以實現,在apidemo裡就有這樣的實例

我們首先做一個繼承animation的類Rotate3d.java

 

  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 private static final String TAG = "Rotate3d";
10
11 public Rotate3d(float fromDegree, float toDegree, float left, float top,
12 float centerX, float centerY) {
13 this.mFromDegree = fromDegree;
14 this.mToDegree = toDegree;
15 this.mLeft = left;
16 this.mTop = top;
17 this.mCenterX = centerX;
18 this.mCenterY = centerY;
19
20 }
21
22 @Override
23 public void initialize(int width, int height, int parentWidth,
24 int parentHeight) {
25 super.initialize(width, height, parentWidth, parentHeight);
26 mCamera = new Camera();
27 }
28
29 @Override
30 protected void applyTransformation(float interpolatedTime, Transformation t) {
31 final float FromDegree = mFromDegree;
32 float degrees = FromDegree + (mToDegree - mFromDegree)
33 * interpolatedTime;
34 final float centerX = mCenterX;
35 final float centerY = mCenterY;
36 final Matrix matrix = t.getMatrix();
37
38 if (degrees <= -76.0f) {
39 degrees = -90.0f;
40 mCamera.save();
41 mCamera.rotateY(degrees);
42 mCamera.getMatrix(matrix);
43 mCamera.restore();
44 } else if(degrees >=76.0f){
45 degrees = 90.0f;
46 mCamera.save();
47 mCamera.rotateY(degrees);
48 mCamera.getMatrix(matrix);
49 mCamera.restore();
50 }else{
51 mCamera.save();
52 //這裡很重要哦。
53   mCamera.translate(0, 0, centerX);
54 mCamera.rotateY(degrees);
55 mCamera.translate(0, 0, -centerX);
56 mCamera.getMatrix(matrix);
57 mCamera.restore();
58 }
59
60 matrix.preTranslate(-centerX, -centerY);
61 matrix.postTranslate(centerX, centerY);
62 }
63 }
64
65  public class Rotate3d extends Animation {
66 private float mFromDegree;
67 private float mToDegree;
68 private float mCenterX;
69 private float mCenterY;
70 private float mLeft;
71 private float mTop;
72 private Camera mCamera;
73 private static final String TAG = "Rotate3d";
74
75 public Rotate3d(float fromDegree, float toDegree, float left, float top,
76 float centerX, float centerY) {
77 this.mFromDegree = fromDegree;
78 this.mToDegree = toDegree;
79 this.mLeft = left;
80 this.mTop = top;
81 this.mCenterX = centerX;
82 this.mCenterY = centerY;
83
84 }
85
86 @Override
87 public void initialize(int width, int height, int parentWidth,
88 int parentHeight) {
89 super.initialize(width, height, parentWidth, parentHeight);
90 mCamera = new Camera();
91 }
92
93 @Override
94 protected void applyTransformation(float interpolatedTime, Transformation t) {
95 final float FromDegree = mFromDegree;
96 float degrees = FromDegree + (mToDegree - mFromDegree)
97 * interpolatedTime;
98 final float centerX = mCenterX;
99 final float centerY = mCenterY;
100 final Matrix matrix = t.getMatrix();
101
102 if (degrees <= -76.0f) {
103 degrees = -90.0f;
104 mCamera.save();
105 mCamera.rotateY(degrees);
106 mCamera.getMatrix(matrix);
107 mCamera.restore();
108 } else if(degrees >=76.0f){
109 degrees = 90.0f;
110 mCamera.save();
111 mCamera.rotateY(degrees);
112 mCamera.getMatrix(matrix);
113 mCamera.restore();
114 }else{
115 mCamera.save();
116 //這裡很重要哦。
117   mCamera.translate(0, 0, centerX);
118 mCamera.rotateY(degrees);
119 mCamera.translate(0, 0, -centerX);
120 mCamera.getMatrix(matrix);
121 mCamera.restore();
122 }
123
124 matrix.preTranslate(-centerX, -centerY);
125 matrix.postTranslate(centerX, centerY);
126 }
127 }
128
129  

有了這個類一切都會變得簡單的,接著只要在activity中寫兩個Rotate3d的對象,讓兩個view,分別做這兩個對象的animation就好了;

 

 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);
12

最後就是還要寫一下mImageView1,mImageView2的xml,

 

 1 <?xml version="1.0" encoding="utf-8"?>  
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="wrap_content"
6 >
7
8 <FrameLayout
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent">
11
12 <ImageView
13 android:id="@+id/image1"
14 android:layout_gravity="center_horizontal"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:src="@drawable/image1"
18 />
19 <ImageView
20 android:id="@+id/image2"
21 android:background="#ffff0000"
22 android:layout_gravity="center_horizontal"
23 android:layout_width="fill_parent"
24 android:layout_height="wrap_content"
25 android:src="@drawable/image2"
26 />
27
28 </FrameLayout>
29 </LinearLayout>

轉自:http://www.cnblogs.com/jk1001/archive/2010/07/28/1787009.html

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved