1. 程式人生 > >Android 3D旋轉動畫效果

Android 3D旋轉動畫效果

這篇文章主要介紹一下如何實現View的3D旋轉效果,實現的主要原理就是圍繞Y軸旋轉,同時在Z軸方面上有一個深入的縮放。

演示的demo主要有以下幾個重點:

1,自定義旋轉動畫

2,動畫做完後,重置ImageView

先看一下程式的執行效果:


1,自定義動畫類

這裡實現了一個Rotate3dAnimation的類,它擴充套件了Animation類,重寫applyTransformation()方法,提供指定時間的矩陣變換,我們在這個方法裡,就可以利用Camera類得得到一個圍繞Y軸旋轉的matrix,把這個matrix設定到Transformation物件中。  具體的實現程式碼如下:
  1. @Override
  2. protectedvoid applyTransformation(float interpolatedTime, Transformation t)  
  3. {  
  4.         finalfloat fromDegrees = mFromDegrees;  
  5.         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);  
  6.         finalfloat centerX = mCenterX;  
  7.         finalfloat centerY = mCenterY;  
  8.         final Camera camera = mCamera;  
  9.         final Matrix matrix = t.getMatrix();  
  10.         camera.save();  
  11.         if (mReverse) {  
  12.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
  13.         } else {  
  14.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
  15.         }  
  16.         camera.rotateY(degrees);  
  17.         camera.getMatrix(matrix);  
  18.         camera.restore();  
  19.         matrix.preTranslate(-centerX, -centerY);  
  20.         matrix.postTranslate(centerX, centerY);  
  21. }  

2,如何使用這個動畫類

在Activity中,我們有兩個大小一樣的ImageView,它們都放在FrameLayout中,這樣他們位置是重疊的,對最上面的ImageView做動畫(旋轉角度從0到90),當動畫做完後,再對後面的ImageView做動畫(旋轉角度從90到180),在這裡,要控制相應的ImageView隱藏或顯示。

動畫的listener實現如下: 


  1. privatefinalclass DisplayNextView implements Animation.AnimationListener {  
  2.         publicvoid onAnimationStart(Animation animation) {  
  3.         }  
  4.         publicvoid onAnimationEnd(Animation animation) {  
  5.             mContainer.post(new SwapViews());  
  6.         }  
  7.         publicvoid onAnimationRepeat(Animation animation) {  
  8.         }  
  9.     }  

動畫做完後,執行的程式碼如下:


  1. privatefinalclass SwapViews implements Runnable  
  2.     {  
  3.         @Override
  4.         publicvoid run()  
  5.         {  
  6.             mImageView1.setVisibility(View.GONE);  
  7.             mImageView2.setVisibility(View.GONE);  
  8.             mIndex++;  
  9.             if (0 == mIndex % 2)  
  10.             {  
  11.                 mStartAnimView = mImageView1;  
  12.             }  
  13.             else
  14.             {  
  15.                 mStartAnimView = mImageView2;  
  16.             }  
  17.             mStartAnimView.setVisibility(View.VISIBLE);  
  18.             mStartAnimView.requestFocus();  
  19.             Rotate3dAnimation rotation = new Rotate3dAnimation(  
  20.                     -90,  
  21.                     0,  
  22.                     mCenterX,  
  23.                     mCenterY, mDepthZ, false);  
  24.             rotation.setDuration(mDuration);  
  25.             rotation.setFillAfter(true);  
  26.             rotation.setInterpolator(new DecelerateInterpolator());  
  27.             mStartAnimView.startAnimation(rotation);  
  28.         }  
  29.     }  

點選Button的事件處理實現:


  1. @Override
  2. publicvoid onClick(View v)  
  3. {  
  4.     mCenterX = mContainer.getWidth() / 2;  
  5.     mCenterY = mContainer.getHeight() / 2;  
  6.     getDepthZ();  
  7.     applyRotation(mStartAnimView, 090);  
  8. }  

applyRotation的實現如下:

  1. privatevoid applyRotation(View animView, float startAngle, float toAngle)  
  2.     {  
  3.         float centerX = mCenterX;  
  4.         float centerY = mCenterY;  
  5.         Rotate3dAnimation rotation = new Rotate3dAnimation(  
  6.                 startAngle, toAngle, centerX, centerY, mDepthZ, true);  
  7.         rotation.setDuration(mDuration);  
  8.         rotation.setFillAfter(true);  
  9.         rotation.setInterpolator(new AccelerateInterpolator());  
  10.         rotation.setAnimationListener(new DisplayNextView());  
  11.         animView.startAnimation(rotation);  
  12.     }  


3,完整程式碼如下

Rotate3dAnimActivity.java

  1. publicclass Rotate3dAnimActivity extends Activity  
  2. {  
  3.     ImageView mImageView1 = null;  
  4.     ImageView mImageView2 = null;  
  5.     ImageView mStartAnimView = null;  
  6.     View mContainer = null;  
  7.