论坛首页 Java企业应用论坛

android 3d 旋转

浏览 11281 次
精华帖 (0) :: 良好帖 (5) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-07-26   最后修改:2010-07-28

在javaeye里看到了关于3d旋转的文章,可是博主没有透入什么技术细节。由于一直想做出那种旋转效果,所以就想啊想,终于想出来了(我是个小菜鸟)。呵呵,不管怎样,希望对想做还没做出来的朋友一些帮助。

先上一个效果图:


 

这是你想要的吗?如果是就继续往下看吧。

其实,这个效果是用animation配合camera做出来的,相信大家在apidemo里面看过类似的。

那么先写一个继承animation的类:Rotate3d

 

public class Rotate3d 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 Rotate3d(float fromDegree, float toDegree, float left, float top,
			float centerX, float centerY) {
		this.mFromDegree = fromDegree;
		this.mToDegree = toDegree;
		this.mLeft = left;
		this.mTop = top;
		this.mCenterX = centerX;
		this.mCenterY = centerY;

	}

	@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, -centerY);
		matrix.postTranslate(centerX, centerY);
	}
}

 

 有了这个类一切都会变得简单的,接着只要在activity中写两个Rotate3d的对象,让两个view,分别做这两个对象的animation就好了。(原来就这么简单啊!无语)

 

//下面两句很关键哦,呵呵,心照不宣。
    	Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY);
        Rotate3d rightAnimation = new Rotate3d(-0+90, -90+90, 0.0f, 0.0f, mCenterX, mCenterY);

        leftAnimation.setFillAfter(true);
        leftAnimation.setDuration(1000);
        rightAnimation.setFillAfter(true);
        rightAnimation.setDuration(1000);

        mImageView1.startAnimation(leftAnimation);
        mImageView2.startAnimation(rightAnimation);
 

 

 

 

还要写一下mImageView1,mImageView2的xml,

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

                   <ImageView
	    			android:id="@+id/image1"
	    			android:layout_gravity="center_horizontal"
	    			android:layout_width="fill_parent"
	    			android:layout_height="wrap_content"
	    			android:src="@drawable/image1"
	    			/>
                   <ImageView
	    			android:id="@+id/image2"
	    			android:background="#ffff0000"
	    			android:layout_gravity="center_horizontal"
	    			android:layout_width="fill_parent"
	    			android:layout_height="wrap_content"
	    			android:src="@drawable/image2"
	    			/>

    </FrameLayout>
</LinearLayout>

 写完收工。如果有不足之处,还请朋友们不吝指教。

 

 

 

 

 

 

  • 大小: 19.5 KB
   发表时间:2010-07-26  
没做过,但是看着不简单,良好了!!
0 请登录后投票
   发表时间:2010-07-26  
明显新手帖...旋转就是3D中的HELLOWORLD~
0 请登录后投票
   发表时间:2010-07-26  
可否放上源代码 想了解了解ANDROID
0 请登录后投票
   发表时间:2010-07-27  
Android是很不错的!
0 请登录后投票
   发表时间:2010-07-27  
qiren83 写道
可否放上源代码 想了解了解ANDROID

上面的代码几乎已经全了,照着做一遍吧
0 请登录后投票
   发表时间:2010-07-27  
放上源代码 

放个demo 到上面

马上就可直接运行  快速  大家都喜欢这样
0 请登录后投票
   发表时间:2010-07-28  
嗯,我觉的这样已经可以了,正好想研究下这方面的. 谢过.不过做个Demo更好.
0 请登录后投票
   发表时间:2010-07-28  
请问里边的图片是预设做成这样的,还是自动可以把图片这样变形并且连贯起来?
0 请登录后投票
   发表时间:2010-07-28  
这里的mCenterX和mCenterY有什么特定的要求?
我测试了一下,如果mCenterX和mCenterY取的是二分之一的屏幕长和宽的话是可以正常运行的,但是需要所选的图的宽必须是屏幕的宽,不然会出现图片居中显示,两个图片转动并不是贴在一起的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics