通过继承Animation动画类 封装了一个3d旋转的效果代码如下
Java代码
- import android.graphics.Camera;
- import android.graphics.Matrix;
- import android.view.animation.Animation;
- import android.view.animation.Transformation;
- public class Rotatw3d extends Animation
- {
- private float mFromDegree;
- private float mToDegree;
- private float mCenterX;
- private float mcenterY;
- private float mleft;
- private float mTop;
- private Camera mCamera;
- private static final String TAG="Rotate3d";
- public Rotatw3d(float mFromDegree, float mToDegree, float mCenterX,
- float mcenterY, float mleft, float mTop)
- {
- this.mFromDegree = mFromDegree;
- this.mToDegree = mToDegree;
- this.mCenterX = mCenterX;
- this.mcenterY = mcenterY;
- this.mleft = mleft;
- this.mTop = mTop;
- }
- @Override
- public void initialize(int width, int height, int parentWidth,
- int parentHeight)
- {
- super.initialize(width, height, parentWidth, parentHeight);
- mCamera=new Camera();
- }
- @Override
- protected void applyTransformation(float interpolatedTime, Transformation t)
- {
- final float FromDegree =mFromDegree;
- float degrees=FromDegree+(mToDegree-mFromDegree)*interpolatedTime;
- final float centerX=mCenterX;
- final float centerY=mcenterY;
- final Matrix matrix=t.getMatrix();
- if(degrees<=-76.0f)
- {
- degrees=-90.0f;
- mCamera.save();
- mCamera.rotateY(degrees);
- mCamera.getMatrix(matrix);
- mCamera.restore();
- }
- else if(degrees>=76.0f)
- {
- degrees=90.0f;
- mCamera.save();
- mCamera.rotateY(degrees);
- mCamera.getMatrix(matrix);
- mCamera.restore();
- }
- else
- {
- mCamera.save();
- mCamera.translate(0, 0, centerX);
- mCamera.rotateY(degrees);
- mCamera.translate(0, 0, -centerX);
- mCamera.getMatrix(matrix);
- mCamera.restore();
- }
- matrix.preTranslate(-centerX, -centerX);
- matrix.postTranslate(centerX, centerX);
- }
- }
简单的调用
Java代码
- Rotatw3d leftaction=new Rotatw3d(-0, -90, -100, -100, -100, -100);
- leftaction.setFillAfter(true);
- leftaction.setDuration(5000);
- ImageView image=(ImageView)findViewById(R.id.image);
- image.startAnimation(leftaction);
3d的旋转效果是出来了,至于如何精致,你可自由发挥。
浙公网安备 33010602011771号