Android動畫分析之3D翻轉效果

 

原博文:http://yarin.blog.51cto.com/1130898/386219

看到很多人在問如何實現三維的翻轉效果,所以今天在這裏簡單的給大家分析一下,其實在APIDemo中就有這樣一個例子,那麼我們就以其爲例來學習Android中的翻轉動畫效果的實現,首先看一下運行效果如下圖所示。

Android中並沒有提供直接做3D翻轉的動畫,所以關於3D翻轉的動畫效果需要我們自己實現,那麼我們首先來分析一下Animation 和 Transformation。

Animation動畫的主要接口,其中主要定義了動畫的一些屬性比如開始時間,持續時間,是否重複播放等等。而Transformation中則包含一個矩陣和alpha值,矩陣是用來做平移,旋轉和縮放動畫的,而alpha值是用來做alpha動畫的,要實現3D旋轉動畫我們需要繼承自Animation類來實現,我們需要重載getTransformation和applyTransformation,在getTransformation中Animation會根據動畫的屬性來產生一系列的差值點,然後將這些差值點傳給applyTransformation,這個函數將根據這些點來生成不同的Transformation。下面是具體實現:

  1. public class Rotate3dAnimation extends Animation {  
  2.     //開始角度  
  3.     private final float mFromDegrees;  
  4.     //結束角度  
  5.     private final float mToDegrees;  
  6.     //中心點  
  7.     private final float mCenterX;  
  8.     private final float mCenterY;  
  9.     private final float mDepthZ;  
  10.     //是否需要扭曲  
  11.     private final boolean mReverse;  
  12.     //攝像頭  
  13.     private Camera mCamera;  
  14.     public Rotate3dAnimation(float fromDegrees, float toDegrees,  
  15.             float centerX, float centerY, float depthZ, boolean reverse) {  
  16.         mFromDegrees = fromDegrees;  
  17.         mToDegrees = toDegrees;  
  18.         mCenterX = centerX;  
  19.         mCenterY = centerY;  
  20.         mDepthZ = depthZ;  
  21.         mReverse = reverse;  
  22.     }  
  23.  
  24.     @Override 
  25.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  26.         super.initialize(width, height, parentWidth, parentHeight);  
  27.         mCamera = new Camera();  
  28.     }  
  29.     //生成Transformation  
  30.     @Override 
  31.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  32.         final float fromDegrees = mFromDegrees;  
  33.         //生成中間角度  
  34.         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);  
  35.  
  36.         final float centerX = mCenterX;  
  37.         final float centerY = mCenterY;  
  38.         final Camera camera = mCamera;  
  39.  
  40.         final Matrix matrix = t.getMatrix();  
  41.  
  42.         camera.save();  
  43.         if (mReverse) {  
  44.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
  45.         } else {  
  46.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
  47.         }  
  48.         camera.rotateY(degrees);  
  49.         //取得變換後的矩陣  
  50.         camera.getMatrix(matrix);  
  51.         camera.restore();  
  52.  
  53.         matrix.preTranslate(-centerX, -centerY);  
  54.         matrix.postTranslate(centerX, centerY);  
  55.     }  

其中包括了旋轉的開始和結束角度,中心點、是否扭曲、和一個Camera,這裏我們主要分析applyTransformation函數,其中第一個參數就是通過getTransformation函數傳遞的差指點,然後我們根據這個差值通過線性差值算法計算出一箇中間角度degrees,Camera類是用來實現繞Y軸旋轉後透視投影的,因此我們首先通過t.getMatrix()取得當前的矩陣,然後通過camera.translate來對矩陣進行平移變換操作,camera.rotateY進行旋轉。這樣我們就可以很輕鬆的實現3D旋轉效果了,該例子的原意是通過一個列表來供用戶選擇要實現翻轉的圖像,所以我們分析至少需要定義兩個控件:ListView和ImageView(要翻轉的圖像),主界面的xml佈局定義如下所示。

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:id="@+id/container" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.  
  6.     <ListView 
  7.         android:id="@android:id/list" 
  8.         android:persistentDrawingCache="animation|scrolling" 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="match_parent" 
  11.         android:layoutAnimation="@anim/layout_bottom_to_top_slide" /> 
  12.  
  13.     <ImageView 
  14.         android:id="@+id/picture" 
  15.         android:scaleType="fitCenter" 
  16.         android:layout_width="match_parent" 
  17.         android:layout_height="match_parent" 
  18.         android:visibility="gone" /> 
  19.  
  20. </FrameLayout> 

然後準備好需要的資源,在onCreate函數中準備好ListView和ImageView,因爲要旋轉所以我們需要保存視圖的緩存信息,通過setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);可以設置該功能,當我們選擇列表中的圖像資源後在onItemClick中將選擇的資源Id對應的圖像設置到ImageView中,然後通過applyRotation來啓動一個動畫,前面有了Rotate3dAnimation的實現,我們要完成3D翻轉動畫就很簡單,直接構建一個Rotate3dAnimation對象,設置其屬性(包括動畫監聽),這裏將動畫的監聽設置爲DisplayNextView,可以用來顯示下一個視圖,在其中的動畫結束監聽(onAnimationEnd)中,通過一個縣城SwapViews來交換兩個畫面,交換過程則是設置ImageView和ListView的顯示相關屬性,並構建一個Rotate3dAnimation對象,對另一個界面進行旋轉即可,然後啓動動畫,整個轉換過程實際上就是將第一個界面從0度轉好90度,然後就愛你過第二個界面從90度轉到0度,這樣就形成了一個翻轉動畫,完整代碼如下,我們也加入了一些必要的註解,大家也可以參考APIDemo中的Transition3d例子。

  1. public class Transition3d extends Activity implements 
  2.         AdapterView.OnItemClickListener, View.OnClickListener {  
  3.     //照片列表  
  4.     private ListView mPhotosList;  
  5.     private ViewGroup mContainer;  
  6.     private ImageView mImageView;  
  7.  
  8.     // 照片的名字,用於顯示在list中  
  9.     private static final String[] PHOTOS_NAMES = new String[] {  
  10.             "Lyon",  
  11.             "Livermore",  
  12.             "Tahoe Pier",  
  13.             "Lake Tahoe",  
  14.             "Grand Canyon",  
  15.             "Bodie" 
  16.     };  
  17.  
  18.     // 資源id  
  19.     private static final int[] PHOTOS_RESOURCES = new int[] {  
  20.             R.drawable.photo1,  
  21.             R.drawable.photo2,  
  22.             R.drawable.photo3,  
  23.             R.drawable.photo4,  
  24.             R.drawable.photo5,  
  25.             R.drawable.photo6  
  26.     };  
  27.  
  28.     @Override 
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.  
  32.         setContentView(R.layout.animations_main_screen);  
  33.  
  34.         mPhotosList = (ListView) findViewById(android.R.id.list);  
  35.         mImageView = (ImageView) findViewById(R.id.picture);  
  36.         mContainer = (ViewGroup) findViewById(R.id.container);  
  37.  
  38.         // 準備ListView  
  39.         final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  40.                 android.R.layout.simple_list_item_1, PHOTOS_NAMES);  
  41.  
  42.         mPhotosList.setAdapter(adapter);  
  43.         mPhotosList.setOnItemClickListener(this);  
  44.  
  45.         // 準備ImageView  
  46.         mImageView.setClickable(true);  
  47.         mImageView.setFocusable(true);  
  48.         mImageView.setOnClickListener(this);  
  49.  
  50.         //設置需要保存緩存  
  51.         mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);  
  52.     }  
  53.  
  54.     /**  
  55.      * Setup a new 3D rotation on the container view.  
  56.      *  
  57.      * @param position the item that was clicked to show a picture, or -1 to show the list  
  58.      * @param start the start angle at which the rotation must begin  
  59.      * @param end the end angle of the rotation  
  60.      */ 
  61.     private void applyRotation(int position, float start, float end) {  
  62.         // 計算中心點  
  63.         final float centerX = mContainer.getWidth() / 2.0f;  
  64.         final float centerY = mContainer.getHeight() / 2.0f;  
  65.  
  66.         // Create a new 3D rotation with the supplied parameter  
  67.         // The animation listener is used to trigger the next animation  
  68.         final Rotate3dAnimation rotation =  
  69.                 new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);  
  70.         rotation.setDuration(500);  
  71.         rotation.setFillAfter(true);  
  72.         rotation.setInterpolator(new AccelerateInterpolator());  
  73.         //設置監聽  
  74.         rotation.setAnimationListener(new DisplayNextView(position));  
  75.  
  76.         mContainer.startAnimation(rotation);  
  77.     }  
  78.  
  79.     public void onItemClick(AdapterView parent, View v, int position, long id) {  
  80.         // 設置ImageView  
  81.         mImageView.setImageResource(PHOTOS_RESOURCES[position]);  
  82.         applyRotation(position, 090);  
  83.     }  
  84.     //點擊圖像時,返回listview  
  85.     public void onClick(View v) {  
  86.         applyRotation(-118090);  
  87.     }  
  88.  
  89.     /**  
  90.      * This class listens for the end of the first half of the animation.  
  91.      * It then posts a new action that effectively swaps the views when the container  
  92.      * is rotated 90 degrees and thus invisible.  
  93.      */ 
  94.     private final class DisplayNextView implements Animation.AnimationListener {  
  95.         private final int mPosition;  
  96.  
  97.         private DisplayNextView(int position) {  
  98.             mPosition = position;  
  99.         }  
  100.  
  101.         public void onAnimationStart(Animation animation) {  
  102.         }  
  103.         //動畫結束  
  104.         public void onAnimationEnd(Animation animation) {  
  105.             mContainer.post(new SwapViews(mPosition));  
  106.         }  
  107.  
  108.         public void onAnimationRepeat(Animation animation) {  
  109.         }  
  110.     }  
  111.  
  112.     /**  
  113.      * This class is responsible for swapping the views and start the second  
  114.      * half of the animation.  
  115.      */ 
  116.     private final class SwapViews implements Runnable {  
  117.         private final int mPosition;  
  118.  
  119.         public SwapViews(int position) {  
  120.             mPosition = position;  
  121.         }  
  122.  
  123.         public void run() {  
  124.             final float centerX = mContainer.getWidth() / 2.0f;  
  125.             final float centerY = mContainer.getHeight() / 2.0f;  
  126.             Rotate3dAnimation rotation;  
  127.               
  128.             if (mPosition > -1) {  
  129.                 //顯示ImageView  
  130.                 mPhotosList.setVisibility(View.GONE);  
  131.                 mImageView.setVisibility(View.VISIBLE);  
  132.                 mImageView.requestFocus();  
  133.  
  134.                 rotation = new Rotate3dAnimation(90180, centerX, centerY, 310.0f, false);  
  135.             } else {  
  136.                 //返回listview  
  137.                 mImageView.setVisibility(View.GONE);  
  138.                 mPhotosList.setVisibility(View.VISIBLE);  
  139.                 mPhotosList.requestFocus();  
  140.  
  141.                 rotation = new Rotate3dAnimation(900, centerX, centerY, 310.0f, false);  
  142.             }  
  143.  
  144.             rotation.setDuration(500);  
  145.             rotation.setFillAfter(true);  
  146.             rotation.setInterpolator(new DecelerateInterpolator());  
  147.             //開始動畫  
  148.             mContainer.startAnimation(rotation);  
  149.         }  
  150.     }  
  151.  

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章