原文地址:http://blog.csdn.net/x_i_a_o_h_a_i/article/details/40449847

其实网上的3D旋转的例子很多,在这里我只是想把其代码做一个解释。

先上图:

代码:

TurnAroundActivity

  1. /**
  2. * 图片浏览器的主Activity。
  3. *
  4. * @author guolin
  5. */
  6. public class TurnAroundActivity extends Activity {
  7.  
  8. /**
  9. * 根布局
  10. */
  11. private RelativeLayout layout;
  12.  
  13. /**
  14. * 用于展示图片列表的ListView
  15. */
  16. private ListView picListView;
  17.  
  18. /**
  19. * 翻转后的布局
  20. */
  21. private LinearLayout picture;
  22.  
  23. /**
  24. * 图片列表的适配器
  25. */
  26. private PictureAdapter adapter;
  27.  
  28. /**
  29. * 存放所有图片的集合
  30. */
  31. private List<Picture> picList = new ArrayList<Picture>();
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. requestWindowFeature(Window.FEATURE_NO_TITLE);
  37. setContentView(R.layout.activity_around);
  38. // 对图片列表数据进行初始化操作
  39. initPics();
  40. layout = (RelativeLayout) findViewById(R.id.layout);
  41. picListView = (ListView) findViewById(R.id.pic_list_view);
  42. picture = (LinearLayout) findViewById(R.id.picture);
  43. adapter = new PictureAdapter(this, 0, picList);
  44. picListView.setAdapter(adapter);
  45. picListView.setOnItemClickListener(new OnItemClickListener() {
  46. @Override
  47. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  48.  
  49. // 获取布局的中心点位置,作为旋转的中心点
  50. float centerX = layout.getWidth() / 2f;
  51. float centerY = layout.getHeight() / 2f;
  52. // 构建3D旋转动画对象,旋转角度为0到90度,这使得ListView将会从可见变为不可见
  53. final Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,
  54. 310.0f, true);
  55. // 动画持续时间500毫秒
  56. rotation.setDuration(500);
  57. // 动画完成后保持完成的状态
  58. rotation.setFillAfter(true);
  59. rotation.setInterpolator(new AccelerateInterpolator());
  60. // 设置动画的监听器
  61. rotation.setAnimationListener(new TurnToImageView());
  62. layout.startAnimation(rotation);
  63. }
  64. });
  65. picture.setOnClickListener(new OnClickListener() {
  66. @Override
  67. public void onClick(View v) {
  68. // 获取布局的中心点位置,作为旋转的中心点
  69. float centerX = layout.getWidth() / 2f;
  70. float centerY = layout.getHeight() / 2f;
  71. // 构建3D旋转动画对象,旋转角度为360到270度,这使得ImageView将会从可见变为不可见,并且旋转的方向是相反的
  72. final Rotate3dAnimation rotation = new Rotate3dAnimation(360, 270, centerX,
  73. centerY, 310.0f, true);
  74. // 动画持续时间500毫秒
  75. rotation.setDuration(500);
  76. // 动画完成后保持完成的状态
  77. rotation.setFillAfter(true);
  78. rotation.setInterpolator(new AccelerateInterpolator());
  79. // 设置动画的监听器
  80. rotation.setAnimationListener(new TurnToListView());
  81. layout.startAnimation(rotation);
  82. }
  83. });
  84. }
  85.  
  86. /**
  87. * 初始化图片列表数据。
  88. */
  89. private void initPics() {
  90. Picture bird = new Picture("Bird", R.drawable.bird);
  91. picList.add(bird);
  92. Picture winter = new Picture("Winter", R.drawable.winter);
  93. picList.add(winter);
  94. Picture autumn = new Picture("Autumn", R.drawable.autumn);
  95. picList.add(autumn);
  96. Picture greatWall = new Picture("Great Wall", R.drawable.great_wall);
  97. picList.add(greatWall);
  98. Picture waterFall = new Picture("Water Fall", R.drawable.water_fall);
  99. picList.add(waterFall);
  100. }
  101.  
  102. /**
  103. * 注册在ListView点击动画中的动画监听器,用于完成ListView的后续动画。
  104. *
  105. * @author guolin
  106. */
  107. class TurnToImageView implements AnimationListener {
  108.  
  109. @Override
  110. public void onAnimationStart(Animation animation) {
  111. }
  112.  
  113. /**
  114. * 当ListView的动画完成后,还需要再启动ImageView的动画,让ImageView从不可见变为可见
  115. */
  116. @Override
  117. public void onAnimationEnd(Animation animation) {
  118. // 获取布局的中心点位置,作为旋转的中心点
  119. float centerX = layout.getWidth() / 2f;
  120. float centerY = layout.getHeight() / 2f;
  121. // 将ListView隐藏
  122. picListView.setVisibility(View.GONE);
  123. // 将ImageView显示
  124. picture.setVisibility(View.VISIBLE);
  125. picture.requestFocus();
  126. // 构建3D旋转动画对象,旋转角度为270到360度,这使得ImageView将会从不可见变为可见
  127. final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,
  128. 310.0f, false);
  129. // 动画持续时间500毫秒
  130. rotation.setDuration(500);
  131. // 动画完成后保持完成的状态
  132. rotation.setFillAfter(true);
  133. rotation.setInterpolator(new AccelerateInterpolator());
  134. layout.startAnimation(rotation);
  135. }
  136.  
  137. @Override
  138. public void onAnimationRepeat(Animation animation) {
  139. }
  140.  
  141. }
  142.  
  143. /**
  144. * 注册在ImageView点击动画中的动画监听器,用于完成ImageView的后续动画。
  145. *
  146. * @author guolin
  147. */
  148. class TurnToListView implements AnimationListener {
  149.  
  150. @Override
  151. public void onAnimationStart(Animation animation) {
  152. }
  153.  
  154. /**
  155. * 当ImageView的动画完成后,还需要再启动ListView的动画,让ListView从不可见变为可见
  156. */
  157. @Override
  158. public void onAnimationEnd(Animation animation) {
  159. // 获取布局的中心点位置,作为旋转的中心点
  160. float centerX = layout.getWidth() / 2f;
  161. float centerY = layout.getHeight() / 2f;
  162. // 将ImageView隐藏
  163. picture.setVisibility(View.GONE);
  164. // 将ListView显示
  165. picListView.setVisibility(View.VISIBLE);
  166. picListView.requestFocus();
  167. // 构建3D旋转动画对象,旋转角度为90到0度,这使得ListView将会从不可见变为可见,从而回到原点
  168. final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
  169. 310.0f, false);
  170. // 动画持续时间500毫秒
  171. rotation.setDuration(500);
  172. // 动画完成后保持完成的状态
  173. rotation.setFillAfter(true);
  174. rotation.setInterpolator(new AccelerateInterpolator());
  175. layout.startAnimation(rotation);
  176. }
  177.  
  178. @Override
  179. public void onAnimationRepeat(Animation animation) {
  180. }
  181.  
  182. }
  183.  
  184. }

关键类:

  1. Rotate3dAnimation
  1. /**
  2. * An animation that rotates the view on the Y axis between two specified
  3. * angles. This animation also adds a translation on the Z axis (depth) to
  4. * improve the effect.
  5. */
  6. public class Rotate3dAnimation extends Animation {
  7. private final float mFromDegrees;
  8. private final float mToDegrees;
  9. private final float mCenterX;
  10. private final float mCenterY;
  11. private final float mDepthZ;
  12. private final boolean mReverse;
  13. private Camera mCamera;
  14.  
  15. /**
  16. * Creates a new 3D rotation on the Y axis. The rotation is defined by its
  17. * start angle and its end angle. Both angles are in degrees. The rotation
  18. * is performed around a center point on the 2D space, definied by a pair of
  19. * X and Y coordinates, called centerX and centerY. When the animation
  20. * starts, a translation on the Z axis (depth) is performed. The length of
  21. * the translation can be specified, as well as whether the translation
  22. * should be reversed in time.
  23. *
  24. * @param fromDegrees
  25. * the start angle of the 3D rotation
  26. * @param toDegrees
  27. * the end angle of the 3D rotation
  28. * @param centerX
  29. * the X center of the 3D rotation
  30. * @param centerY
  31. * the Y center of the 3D rotation
  32. * @param reverse
  33. * true if the translation should be reversed, false otherwise
  34. */
  35. public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY,
  36. float depthZ, boolean reverse) {
  37. mFromDegrees = fromDegrees;
  38. mToDegrees = toDegrees;
  39. mCenterX = centerX;
  40. mCenterY = centerY;
  41. mDepthZ = depthZ;
  42. mReverse = reverse;
  43. }
  44.  
  45. @Override
  46. public void initialize(int width, int height, int parentWidth, int parentHeight) {
  47. super.initialize(width, height, parentWidth, parentHeight);
  48. mCamera = new Camera();
  49. }
  50.  
  51. @Override
  52. protected void applyTransformation(float interpolatedTime, Transformation t) {
  53. final float fromDegrees = mFromDegrees;
  54. float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
  55. final float centerX = mCenterX;
  56. final float centerY = mCenterY;
  57. final Camera camera = mCamera;
  58. final Matrix matrix = t.getMatrix();
  59. camera.save();
  60. //z轴上的景深
  61. if (mReverse) {
  62. camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
  63. } else {
  64. camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
  65. }
  66. camera.rotateY(degrees);
  67. camera.getMatrix(matrix);
  68. camera.restore();
  69. //camera.rotateY(degrees);其实围绕y轴旋转的坐标点是在(0,0);
  70. //为了让其在围绕(centerX, centerY)点
  71. //preTranslate 作用为在rotateY开始之前先把坐标(centerX, centerY)移到中心点
  72. matrix.preTranslate(-centerX, -centerY);
  73. //postTranslate 当执行完rotateY后再把中心点移动回来
  74. matrix.postTranslate(centerX, centerY);
  75. }
  76. }

layout文件:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. >
  6.  
  7. <ListView
  8. android:id="@+id/pic_list_view"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. >
  12. </ListView>
  13.  
  14. <LinearLayout
  15. android:id="@+id/picture"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:clickable="true"
  19. android:orientation="vertical"
  20. android:visibility="gone"
  21. >
  22. <TextView
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:id="@+id/text"
  26. android:text="ddddddd"/>
  27. <ImageView
  28. android:layout_width="200dp"
  29. android:layout_height="200dp"
  30. android:src="@drawable/bird"
  31. />
  32. <Button android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="ddddddd"/>
  35.  
  36. </LinearLayout>
  37.  
  38. </RelativeLayout>

android3D动画,绕y轴旋转的更多相关文章

  1. three.js 对象绕任意轴旋转--模拟门转动

    说了几篇的数学方法,这篇放松一下,郭先生说说绕任意轴转动.说一说其中一种方法,也是比较容易理解的一种,它的原理就是将子对象放到一个盒子中,然后改变子对象相对于父对象的位置(因为子对象的原点默认还是在盒 ...

  2. CSS3 动画3D视角下 旋转圆环

    首先是  transform 属性: transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 加上对应属性则可得到3D透视效果下的形态 本次以圆形( ...

  3. X、Y轴抖动的动画

    实现这个动画效果用到了interpolator属性,这样就能让一些控件产生自定义的抖动效果 这是用作interpolator的文件,用来做动画循环 cycle.xml <?xml version ...

  4. 如何让一个sprite绕一个点旋转,同时又可以实现指定旋转角度并慢慢停下的效果

    如何让一个sprite绕一个点旋转,同时又可以实现指定旋转角度并慢慢停下的效果 首先列出sprite围绕一个点旋转的公式,这个可以自己推导,假设sprite的起始位置为(x1,y1),围绕旋转的中心点 ...

  5. JavaScript求两点之间相对于Y轴的顺时针旋转角度

    需求: 已知一个向量,初始位置在y轴方向,如图红色箭头,绕中心点(x1, y1)旋转若干角度后,到达Line(x2,y2 x1,y1)的位置,求旋转角度 分析: 坐标点(x1, y1)(x2, y2) ...

  6. chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法[bubuko.com]

    chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法,原文:http://bubuko.com/infodetail-328671.html 默认情况下如下图 Y轴并不是从0开始 ...

  7. 3d照片环效果(修改版--添加了x轴y轴双向转动和修复模糊度的bug)

    今天用用前两天总结的css3新效果写了一个3d照片环的效果,其中还有些bug大家可以看一看,一起改进. <!DOCTYPE html> <html lang="en&quo ...

  8. echarts使用笔记四:双Y轴

    1.双Y轴显示数量和占比 app.title = '坐标轴刻度与标签对齐'; option = { title : { //标题 x : 'center', y : 5, text : '数量和占比图 ...

  9. fusioncharts Y轴不显示中文的解决方法(转载)

    使用fusionChart主要是被其界面吸引了,各类图表都很好看,下载以后文档也很周全,支持的语言也很多种 ,容易上手.fusionChart工作原理主要是通过后台传xml数据源给报表前台flash ...

随机推荐

  1. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  2. JS组件系列——BootstrapTable 行内编辑解决方案:x-editable

    前言:之前介绍bootstrapTable组件的时候有提到它的行内编辑功能,只不过为了展示功能,将此一笔带过了,罪过罪过!最近项目里面还是打算将行内编辑用起来,于是再次研究了下x-editable组件 ...

  3. CURL HELP

    CURL下载 在windows的系统环境变量中,将CURL的路径(curl.exe存放的路径)复制到"Path"变量的结尾 Usage: curl [options...] < ...

  4. ADO.NET常用对象

    一.Connection对象 Connection对象也称为数据库连接对象,Connection对象的功能是负责对数据源的连接.所有Connection对象的基类都是DbConnection类. Co ...

  5. PL/SQL异常获取

    1.no_data SET SERVEROUTPUT ON DECLARE pename EMP.ENAME % TYPE ; BEGIN SELECT ename INTO pename FROM ...

  6. UVA1585

    #include<stdio.h> #include<string.h> int main(){ int n; ]; scanf("%d",&n); ...

  7. python的错误和异常

    python错误和异常 错误 错误分为语法错误和逻辑错误 语法错误 >>> if File "<stdin>", line 1 if ^ Syntax ...

  8. Table 表单

    <style> table th { white-space: nowrap; } .chk { white-space: nowrap; } </style> <tab ...

  9. 美国在研新药_读取单个PDF

    QQ:231469242 读取下载美国在研新药PDF内数据:unii,分子式,分子重量,药品名,who,编码,.... PDF无逻辑规则,不能百分之百提取,只能部分提取 几个默认字段为空 # -*- ...

  10. 使用vlc播放器做rtsp流媒体服务器

    可参考: 使用vlc播放器播放rtsp视频 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器做rts ...