前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android之绕Y轴旋转

android之绕Y轴旋转

作者头像
forrestlin
发布2022-04-02 10:14:59
1K0
发布2022-04-02 10:14:59
举报
文章被收录于专栏:蜉蝣禅修之道蜉蝣禅修之道

转自:http://lzyfn123.iteye.com/blog/1426844

Android中并没有提供直接做3D翻转的动画,所以关于3D翻转的动画效果需要我们自己实现,那么我们首先来分析一下Animation 和 Transformation。

Animation动画的主要接口,其中主要定义了动画的一些属性比如开始时间,持续时间,是否重复播放等等。而 Transformation中则包含一个矩阵和alpha值,矩阵是用来做平移,旋转和缩放动画的,而alpha值是用来做alpha动画的,要实现 3D旋转动画我们需要继承自Animation类来实现,我们需要重载getTransformation和applyTransformation,在 getTransformation中Animation会根据动画的属性来产生一系列的差值点,然后将这些差值点传给 applyTransformation,这个函数将根据这些点来生成不同的Transformation。下面是具体实现:

  1. public class Rotate3dAnimation extends Animation {
  2. //开始角度
  3. private final float mFromDegrees;
  4. //结束角度
  5. private final float mToDegrees;
  6. //中心点
  7. private final float mCenterX;
  8. private final float mCenterY;
  9. private final float mDepthZ;
  10. //是否需要扭曲
  11. private final boolean mReverse;
  12. //摄像头
  13. private Camera mCamera;
  14. public Rotate3dAnimation( float fromDegrees, float toDegrees,
  15. float centerX, float centerY, float depthZ, boolean reverse) {
  16. mFromDegrees = fromDegrees;
  17. mToDegrees = toDegrees;
  18. mCenterX = centerX;
  19. mCenterY = centerY;
  20. mDepthZ = depthZ;
  21. mReverse = reverse;
  22. }
  23. @Override
  24. public void initialize( int width, int height, int parentWidth, int parentHeight) {
  25. super .initialize(width, height, parentWidth, parentHeight);
  26. mCamera = new Camera();
  27. }
  28. //生成Transformation
  29. @Override
  30. protected void applyTransformation( float interpolatedTime, Transformation t) {
  31. final float fromDegrees = mFromDegrees;
  32. //生成中间角度
  33. float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
  34. final float centerX = mCenterX;
  35. final float centerY = mCenterY;
  36. final Camera camera = mCamera;
  37. final Matrix matrix = t.getMatrix();
  38. camera.save();
  39. if (mReverse) {
  40. camera.translate( 0 .0f, 0 .0f, mDepthZ * interpolatedTime);
  41. } else {
  42. camera.translate( 0 .0f, 0 .0f, mDepthZ * ( 1 .0f - interpolatedTime));
  43. }
  44. camera.rotateY(degrees);
  45. //取得变换后的矩阵
  46. camera.getMatrix(matrix);
  47. camera.restore();
  48. matrix.preTranslate(-centerX, -centerY);
  49. matrix.postTranslate(centerX, centerY);
  50. }
  51. }

其中包括了旋转的开始和结束角度,中心点、是否扭曲、和一个Camera,这里我们主要分析applyTransformation函 数,其中第一个参数就是通过getTransformation函数传递的差指点,然后我们根据这个差值通过线性差值算法计算出一个中间角度 degrees,Camera类是用来实现绕Y轴旋转后透视投影的,因此我们首先通过t.getMatrix()取得当前的矩阵,然后通过 camera.translate来对矩阵进行平移变换操作,camera.rotateY进行旋转。这样我们就可以很轻松的实现3D旋转效果了,该例子 的原意是通过一个列表来供用户选择要实现翻转的图像,所以我们分析至少需要定义两个控件:ListView和ImageView(要翻转的图像),主界面 的xml布局定义如下所示。

  1. < FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  2. android:id = "@+id/container"
  3. android:layout_width = "match_parent"
  4. android:layout_height = "match_parent" >
  5. < ListView
  6. android:id = "@android:id/list"
  7. android:persistentDrawingCache = "animation|scrolling"
  8. android:layout_width = "match_parent"
  9. android:layout_height = "match_parent"
  10. android:layoutAnimation = "@anim/layout_bottom_to_top_slide" />
  11. < ImageView
  12. android:id = "@+id/picture"
  13. android:scaleType = "fitCenter"
  14. android:layout_width = "match_parent"
  15. android:layout_height = "match_parent"
  16. android:visibility = "gone" />
  17. </ FrameLayout >

然后准备好需要的资源,在onCreate函数中准备好ListView和ImageView,因为要旋转所以我们需要保存视图的缓存 信息,通过setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);可以设 置该功能,当我们选择列表中的图像资源后在onItemClick中将选择的资源Id对应的图像设置到ImageView中,然后通过 applyRotation来启动一个动画,前面有了Rotate3dAnimation的实现,我们要完成3D翻转动画就很简单,直接构建一个 Rotate3dAnimation对象,设置其属性(包括动画监听),这里将动画的监听设置为DisplayNextView,可以用来显示下一个视 图,在其中的动画结束监听(onAnimationEnd)中,通过一个县城SwapViews来交换两个画面,交换过程则是设置ImageView和 ListView的显示相关属性,并构建一个Rotate3dAnimation对象,对另一个界面进行旋转即可,然后启动动画,整个转换过程实际上就是 将第一个界面从0度转好90度,然后就爱你过第二个界面从90度转到0度,这样就形成了一个翻转动画,完整代码如下,我们也加入了一些必要的注解,大家也 可以参考APIDemo中的Transition3d例子。

  1. public class Transition3d extends Activity implements
  2. AdapterView.OnItemClickListener, View.OnClickListener {
  3. //照片列表
  4. private ListView mPhotosList;
  5. private ViewGroup mContainer;
  6. private ImageView mImageView;
  7. // 照片的名字,用于显示在list中
  8. private static final String[] PHOTOS_NAMES = new String[] {
  9. "Lyon" ,
  10. "Livermore" ,
  11. "Tahoe Pier" ,
  12. "Lake Tahoe" ,
  13. "Grand Canyon" ,
  14. "Bodie"
  15. };
  16. // 资源id
  17. private static final int [] PHOTOS_RESOURCES = new int [] {
  18. R.drawable.photo1,
  19. R.drawable.photo2,
  20. R.drawable.photo3,
  21. R.drawable.photo4,
  22. R.drawable.photo5,
  23. R.drawable.photo6
  24. };
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super .onCreate(savedInstanceState);
  28. setContentView(R.layout.animations_main_screen);
  29. mPhotosList = (ListView) findViewById(android.R.id.list);
  30. mImageView = (ImageView) findViewById(R.id.picture);
  31. mContainer = (ViewGroup) findViewById(R.id.container);
  32. // 准备ListView
  33. final ArrayAdapter adapter = new ArrayAdapter( this ,
  34. android.R.layout.simple_list_item_1, PHOTOS_NAMES);
  35. mPhotosList.setAdapter(adapter);
  36. mPhotosList.setOnItemClickListener( this );
  37. // 准备ImageView
  38. mImageView.setClickable( true );
  39. mImageView.setFocusable( true );
  40. mImageView.setOnClickListener( this );
  41. //设置需要保存缓存
  42. mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
  43. }
  44. /**
  45. * Setup a new 3D rotation on the container view.
  46. *
  47. * @param position the item that was clicked to show a picture, or -1 to show the list
  48. * @param start the start angle at which the rotation must begin
  49. * @param end the end angle of the rotation
  50. */
  51. private void applyRotation( int position, float start, float end) {
  52. // 计算中心点
  53. final float centerX = mContainer.getWidth() / 2 .0f;
  54. final float centerY = mContainer.getHeight() / 2 .0f;
  55. // Create a new 3D rotation with the supplied parameter
  56. // The animation listener is used to trigger the next animation
  57. final Rotate3dAnimation rotation =
  58. new Rotate3dAnimation(start, end, centerX, centerY, 310 .0f, true );
  59. rotation.setDuration( 500 );
  60. rotation.setFillAfter( true );
  61. rotation.setInterpolator( new AccelerateInterpolator());
  62. //设置监听
  63. rotation.setAnimationListener( new DisplayNextView(position));
  64. mContainer.startAnimation(rotation);
  65. }
  66. public void onItemClick(AdapterView parent, View v, int position, long id) {
  67. // 设置ImageView
  68. mImageView.setImageResource(PHOTOS_RESOURCES[position]);
  69. applyRotation(position, 0 , 90 );
  70. }
  71. //点击图像时,返回listview
  72. public void onClick(View v) {
  73. applyRotation(- 1 , 180 , 90 );
  74. }
  75. /**
  76. * This class listens for the end of the first half of the animation.
  77. * It then posts a new action that effectively swaps the views when the container
  78. * is rotated 90 degrees and thus invisible.
  79. */
  80. private final class DisplayNextView implements Animation.AnimationListener {
  81. private final int mPosition;
  82. private DisplayNextView( int position) {
  83. mPosition = position;
  84. }
  85. public void onAnimationStart(Animation animation) {
  86. }
  87. //动画结束
  88. public void onAnimationEnd(Animation animation) {
  89. mContainer.post( new SwapViews(mPosition));
  90. }
  91. public void onAnimationRepeat(Animation animation) {
  92. }
  93. }
  94. /**
  95. * This class is responsible for swapping the views and start the second
  96. * half of the animation.
  97. */
  98. private final class SwapViews implements Runnable {
  99. private final int mPosition;
  100. public SwapViews( int position) {
  101. mPosition = position;
  102. }
  103. public void run() {
  104. final float centerX = mContainer.getWidth() / 2 .0f;
  105. final float centerY = mContainer.getHeight() / 2 .0f;
  106. Rotate3dAnimation rotation;
  107. if (mPosition > - 1 ) {
  108. //显示ImageView
  109. mPhotosList.setVisibility(View.GONE);
  110. mImageView.setVisibility(View.VISIBLE);
  111. mImageView.requestFocus();
  112. rotation = new Rotate3dAnimation( 90 , 180 , centerX, centerY, 310 .0f, false );
  113. } else {
  114. //返回listview
  115. mImageView.setVisibility(View.GONE);
  116. mPhotosList.setVisibility(View.VISIBLE);
  117. mPhotosList.requestFocus();
  118. rotation = new Rotate3dAnimation( 90 , 0 , centerX, centerY, 310 .0f, false );
  119. }
  120. rotation.setDuration( 500 );
  121. rotation.setFillAfter( true );
  122. rotation.setInterpolator( new DecelerateInterpolator());
  123. //开始动画
  124. mContainer.startAnimation(rotation);
  125. }
  126. }
  127. }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档