欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 实现3d旋转

程序员文章站 2022-07-15 12:45:45
...

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


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

简单的调用

 

 

 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的旋转效果是出来了,至于如何精致,你可*发挥。

相关标签: android 3d 旋转