浅谈 Android 3D旋转
2010-08-23 01:27:00 来源:WEB开发网最近和一哥们在聊Android的3D旋转效果的技术实现的事情,今天正好有点时间就把技术细节写了出来
在ANDROID中实现3D旋转直接使用animation配合camera就可以实现,在apidemo里就有这样的实例
我们首先做一个继承animation的类Rotate3d.java
1 public class Rotate3d extends Animation { 2 private float mFromDegree; 3 private float mToDegree; 4 private float mCenterX; 5 private float mCenterY; 6 private float mLeft; 7 private float mTop; 8 private Camera mCamera; 9 private static final String TAG = "Rotate3d"; 10 11 public Rotate3d(float fromDegree, float toDegree, float left, float top, 12 float centerX, float centerY) { 13 this.mFromDegree = fromDegree; 14 this.mToDegree = toDegree; 15 this.mLeft = left; 16 this.mTop = top; 17 this.mCenterX = centerX; 18 this.mCenterY = centerY; 19 20 } 21 22 @Override 23 public void initialize(int width, int height, int parentWidth, 24 int parentHeight) { 25 super.initialize(width, height, parentWidth, parentHeight); 26 mCamera = new Camera(); 27 } 28 29 @Override 30 protected void applyTransformation(float interpolatedTime, Transformation t) { 31 final float FromDegree = mFromDegree; 32 float degrees = FromDegree + (mToDegree - mFromDegree) 33 * interpolatedTime; 34 final float centerX = mCenterX; 35 final float centerY = mCenterY; 36 final Matrix matrix = t.getMatrix(); 37 38 if (degrees <= -76.0f) { 39 degrees = -90.0f; 40 mCamera.save(); 41 mCamera.rotateY(degrees); 42 mCamera.getMatrix(matrix); 43 mCamera.restore(); 44 } else if(degrees >=76.0f){ 45 degrees = 90.0f; 46 mCamera.save(); 47 mCamera.rotateY(degrees); 48 mCamera.getMatrix(matrix); 49 mCamera.restore(); 50 }else{ 51 mCamera.save(); 52 //这里很重要哦。 53 mCamera.translate(0, 0, centerX); 54 mCamera.rotateY(degrees); 55 mCamera.translate(0, 0, -centerX); 56 mCamera.getMatrix(matrix); 57 mCamera.restore(); 58 } 59 60 matrix.preTranslate(-centerX, -centerY); 61 matrix.postTranslate(centerX, centerY); 62 } 63 } 64 65 public class Rotate3d
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接