通过继承Animation动画类  封装了一个3d旋转的效果代码如下


Java代码  收藏代码
  1. import android.graphics.Camera;  
  2. import android.graphics.Matrix;  
  3. import android.view.animation.Animation;  
  4. import android.view.animation.Transformation;  
  5.   
  6. public class Rotatw3d extends Animation  
  7. {  
  8.     private float mFromDegree;  
  9.     private float mToDegree;  
  10.     private float mCenterX;  
  11.     private float mcenterY;  
  12.     private float mleft;  
  13.     private float mTop;  
  14.     private Camera mCamera;  
  15.     private static final String TAG="Rotate3d";  
  16.     public Rotatw3d(float mFromDegree, float mToDegree, float mCenterX,  
  17.             float mcenterY, float mleft, float mTop)  
  18.     {  
  19.         this.mFromDegree = mFromDegree;  
  20.         this.mToDegree = mToDegree;  
  21.         this.mCenterX = mCenterX;  
  22.         this.mcenterY = mcenterY;  
  23.         this.mleft = mleft;  
  24.         this.mTop = mTop;  
  25.     }  
  26.     @Override  
  27.     public void initialize(int width, int height, int parentWidth,  
  28.             int parentHeight)  
  29.     {  
  30.         super.initialize(width, height, parentWidth, parentHeight);  
  31.         mCamera=new Camera();  
  32.     }  
  33.     @Override  
  34.     protected void applyTransformation(float interpolatedTime, Transformation t)  
  35.     {  
  36.         final float FromDegree =mFromDegree;  
  37.         float degrees=FromDegree+(mToDegree-mFromDegree)*interpolatedTime;  
  38.         final float centerX=mCenterX;  
  39.         final float centerY=mcenterY;  
  40.         final Matrix matrix=t.getMatrix();  
  41.         if(degrees<=-76.0f)  
  42.         {  
  43.             degrees=-90.0f;  
  44.             mCamera.save();  
  45.             mCamera.rotateY(degrees);  
  46.             mCamera.getMatrix(matrix);  
  47.             mCamera.restore();  
  48.         }  
  49.         else if(degrees>=76.0f)  
  50.         {  
  51.             degrees=90.0f;  
  52.             mCamera.save();  
  53.             mCamera.rotateY(degrees);  
  54.             mCamera.getMatrix(matrix);  
  55.             mCamera.restore();  
  56.         }  
  57.         else   
  58.         {  
  59.             mCamera.save();  
  60.             mCamera.translate(00, centerX);  
  61.             mCamera.rotateY(degrees);  
  62.             mCamera.translate(00, -centerX);  
  63.             mCamera.getMatrix(matrix);  
  64.             mCamera.restore();  
  65.         }  
  66.         matrix.preTranslate(-centerX, -centerX);  
  67.         matrix.postTranslate(centerX, centerX);  
  68.     }  
  69. }  

简单的调用

 

 

Java代码  收藏代码
  1. Rotatw3d leftaction=new Rotatw3d(-0, -90, -100, -100, -100, -100);  
  2.        leftaction.setFillAfter(true);  
  3.        leftaction.setDuration(5000);  
  4.        ImageView image=(ImageView)findViewById(R.id.image);  
  5.        image.startAnimation(leftaction);  

 3d的旋转效果是出来了,至于如何精致,你可自由发挥。

posted on 2012-04-22 21:55  kitea  阅读(86)  评论(0)    收藏  举报