免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3295 | 回复: 0
打印 上一主题 下一主题

Android系列之浅谈Android 3D旋转 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-20 13:10 |只看该作者 |倒序浏览
转:neil-zhao

Android系列之浅谈Android 3D旋转

我们今天就来说说android中的3D效果,那么android都用到哪些东西才能来实现一个3D的效果那,其实android中的3D效果是用animation配合camera就可以实现在apidemo里就有这样的实例我们首先做一个继承animation的类Rotate3d.java

  1. package eoe.3d;


  2. public class Rotate3d extends Animation {
  3. private float mFromDegree;
  4. private float mToDegree;
  5. private float mCenterX;
  6. private float mCenterY;
  7. private float mLeft;
  8. private float mTop;
  9. private Camera mCamera;
  10. private static final String TAG = "Rotate3d";

  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. @Override
  21. public void initialize(int width, int height, int parentWidth,
  22. int parentHeight) {
  23. super.initialize(width, height, parentWidth, parentHeight);
  24. mCamera = new Camera();
  25. }

  26. @Override
  27. protected void applyTransformation(float interpolatedTime, Transformation t) {
  28. final float FromDegree = mFromDegree;
  29. float degrees = FromDegree + (mToDegree - mFromDegree)
  30. * interpolatedTime;
  31. final float centerX = mCenterX;
  32. final float centerY = mCenterY;
  33. final Matrix matrix = t.getMatrix();

  34. if (degrees <= -76.0f) {
  35. degrees = -90.0f;
  36. mCamera.save();
  37. mCamera.rotateY(degrees);
  38. mCamera.getMatrix(matrix);
  39. mCamera.restore();
  40. } else if(degrees >=76.0f){
  41. degrees = 90.0f;
  42. mCamera.save();
  43. mCamera.rotateY(degrees);
  44. mCamera.getMatrix(matrix);
  45. mCamera.restore();
  46. }else{
  47. mCamera.save();
  48. //这里很重要哦。
  49. mCamera.translate(0, 0, centerX);
  50. mCamera.rotateY(degrees);
  51. mCamera.translate(0, 0, -centerX);
  52. mCamera.getMatrix(matrix);
  53. mCamera.restore();
  54. }

  55. matrix.preTranslate(-centerX, -centerY);
  56. matrix.postTranslate(centerX, centerY);
  57. }
  58. }

  59. public class Rotate3d extends Animation {

  60. private float mFromDegree;

  61. private float mToDegree;

  62. private float mCenterX;

  63. private float mCenterY;

  64. private float mLeft;

  65. private float mTop;

  66. private Camera mCamera;

  67. private static final String TAG = "Rotate3d";


  68. public Rotate3d(float fromDegree, float toDegree, float left, float top,

  69. float centerX, float centerY) {

  70. this.mFromDegree = fromDegree;

  71. this.mToDegree = toDegree;

  72. this.mLeft = left;

  73. this.mTop = top;

  74. this.mCenterX = centerX;

  75. this.mCenterY = centerY;


  76. }


  77. @Override

  78. public void initialize(int width, int height, int parentWidth,

  79. int parentHeight) {

  80. super.initialize(width, height, parentWidth, parentHeight);

  81. mCamera = new Camera();

  82. }


  83. @Override

  84. protected void applyTransformation(float interpolatedTime, Transformation t) {

  85. final float FromDegree = mFromDegree;

  86. float degrees = FromDegree + (mToDegree - mFromDegree)

  87. * interpolatedTime;

  88. final float centerX = mCenterX;

  89. final float centerY = mCenterY;

  90. final Matrix matrix = t.getMatrix();


  91. if (degrees <= -76.0f) {

  92. degrees = -90.0f;

  93. mCamera.save();

  94. mCamera.rotateY(degrees);

  95. mCamera.getMatrix(matrix);

  96. mCamera.restore();

  97. } else if(degrees >=76.0f){

  98. degrees = 90.0f;

  99. mCamera.save();

  100. mCamera.rotateY(degrees);

  101. mCamera.getMatrix(matrix);

  102. mCamera.restore();

  103. }else{

  104. mCamera.save();

  105. //这里很重要哦。

  106. mCamera.translate(0, 0, centerX);

  107. mCamera.rotateY(degrees);

  108. mCamera.translate(0, 0, -centerX);

  109. mCamera.getMatrix(matrix);

  110. mCamera.restore();

  111. }


  112. matrix.preTranslate(-centerX, -centerY);

  113. matrix.postTranslate(centerX, centerY);

  114. }

  115. }


复制代码
有了这个类一切都会变得简单的,接着只要在activity中写两个Rotate3d的对象,让两个view,分别做这两个对象的animation就好了;



  1. //下面两句很关键哦,
  2. Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY);
  3. Rotate3d rightAnimation = new Rotate3d(-0+90, -90+90, 0.0f, 0.0f, mCenterX, mCenterY);

  4. leftAnimation.setFillAfter(true);
  5. leftAnimation.setDuration(1000);
  6. rightAnimation.setFillAfter(true);
  7. rightAnimation.setDuration(1000);

  8. mImageView1.startAnimation(leftAnimation);
  9. mImageView2.startAnimation(rightAnimation);
复制代码
最后就是还要写一下xml.

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

  7. <FrameLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">

  10. <ImageView
  11. android:id="@+id/image1"
  12. android:layout_gravity="center_horizontal"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:src="@drawable/image1"
  16. />
  17. <ImageView
  18. android:id="@+id/image2"
  19. android:background="#ffff0000"
  20. android:layout_gravity="center_horizontal"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:src="@drawable/image2"
  24. />

  25. </FrameLayout>
  26. </LinearLayout>



复制代码
也不知道我理解的对不对,希望大家多多的指点我,这样我才能在短时间内有所进步,谢谢大家的支持。

  1. //下面两句很关键哦,
  2. Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY);
  3. Rotate3d rightAnimation = new Rotate3d(-0+90, -90+90, 0.0f, 0.0f, mCenterX, mCenterY);

  4. leftAnimation.setFillAfter(true);
  5. leftAnimation.setDuration(1000);
  6. rightAnimation.setFillAfter(true);
  7. rightAnimation.setDuration(1000);

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


复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP