当前位置:首页 > 开发 > 移动开发 > 正文

android 实现3d旋转

发表于: 2011-08-29   作者:348725767   来源:转载   浏览:
摘要: 通过继承Animation动画类  封装了一个3d旋转的效果代码如下 import android.graphics.Camera; import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.Transformation;

通过继承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旋转

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号