android 实现3d旋转

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值