热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

利用Android的ApiDemos的Rotate3dAnimation实现了个图片3D旋转的动画,围绕Y轴进行旋转,还可以实现Z轴的缩放。点击开始按钮开始旋转,点击结束按钮停止旋转。

利用Android的ApiDemos的Rotate3dAnimation实现了个图片3D旋转的动画,围绕Y轴进行旋转,还可以实现Z轴的缩放。点击开始按钮开始旋转,点击结束按钮停止旋转。

代码如下::

Rotate3dAnimation.java

public class Rotate3dAnimation extends Animation { 
 private final float mFromDegrees; 
 private final float mToDegrees; 
 private final float mCenterX; 
 private final float mCenterY; 
 private final float mDepthZ; 
 private final boolean mReverse; 
 private Camera mCamera; 
 /** 
 * Creates a new 3D rotation on the Y axis. The rotation is defined by its 
 * start angle and its end angle. Both angles are in degrees. The rotation 
 * is performed around a center point on the 2D space, definied by a pair 
 * of X and Y coordinates, called centerX and centerY. When the animation 
 * starts, a translation on the Z axis (depth) is performed. The length 
 * of the translation can be specified, as well as whether the translation 
 * should be reversed in time. 
 * 
 * @param fromDegrees the start angle of the 3D rotation 
 * @param toDegrees the end angle of the 3D rotation 
 * @param centerX the X center of the 3D rotation 
 * @param centerY the Y center of the 3D rotation 
 * @param reverse true if the translation should be reversed, false otherwise 
 */ 
 public Rotate3dAnimation(float fromDegrees, float toDegrees, 
 float centerX, float centerY, float depthZ, boolean reverse) { 
 mFromDegrees = fromDegrees; 
 mToDegrees = toDegrees; 
 mCenterX = centerX; 
 mCenterY = centerY; 
 mDepthZ = depthZ; 
 mReverse = reverse; 
 } 
 @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 fromDegrees = mFromDegrees; 
 float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); 
 final float centerX = mCenterX; 
 final float centerY = mCenterY; 
 final Camera camera = mCamera; 
 final Matrix matrix = t.getMatrix(); 
 //保存一次camera初始状态,用于restore() 
 camera.save(); 
 if (mReverse) { 
 camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); 
 } else { 
 camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); 
 } 
 //围绕Y轴旋转degrees度 
 camera.rotateY(degrees); 
 //行camera中取出矩阵,赋值给matrix 
 camera.getMatrix(matrix); 
 //camera恢复到初始状态,继续用于下次的计算 
 camera.restore(); 
 matrix.preTranslate(-centerX, -centerY); 
 matrix.postTranslate(centerX, centerY); 
 } 
} 

Test3DRotateActivity.java

public class Test3DRotateActivity extends Activity { 
 /** Called when the activity is first created. */ 
 private final String TAG="Test3DRotateActivity"; 
 private ImageView image; 
 private Button start ,stop; 
 private Rotate3dAnimation rotation; 
 private StartNextRotate startNext; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
 image = (ImageView) findViewById(R.id.image); 
 start=(Button) findViewById(R.id.start); 
 stop = (Button) findViewById(R.id.stop); 
 start.setOnClickListener(new OnClickListener() { 
 public void onClick(View v) { 
 // TODO Auto-generated method stub 
 //进行360度的旋转 
 startRotation(0,360); 
 } 
 }); 
 stop.setOnClickListener(new OnClickListener() { 
 public void onClick(View v) { 
 // TODO Auto-generated method stub 
 image.clearAnimation(); 
 } 
 }); 
 } 
 private void startRotation(float start, float end) { 
 // 计算中心点 
 final float centerX = image.getWidth() / 2.0f; 
 final float centerY = image.getHeight() / 2.0f; 
 Log.d(TAG, "centerX="+centerX+", centerY="+centerY); 
 // Create a new 3D rotation with the supplied parameter 
 // The animation listener is used to trigger the next animation 
 //final Rotate3dAnimation rotation =new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true); 
 //Z轴的缩放为0 
 rotation =new Rotate3dAnimation(start, end, centerX, centerY, 0f, true); 
 rotation.setDuration(2000); 
 rotation.setFillAfter(true); 
 //rotation.setInterpolator(new AccelerateInterpolator()); 
 //匀速旋转 
 rotation.setInterpolator(new LinearInterpolator()); 
 //设置监听 
 startNext = new StartNextRotate(); 
 rotation.setAnimationListener(startNext); 
 image.startAnimation(rotation); 
 } 
 private class StartNextRotate implements AnimationListener{ 
 public void onAnimationEnd(Animation animation) { 
 // TODO Auto-generated method stub 
 Log.d(TAG, "onAnimationEnd......"); 
 image.startAnimation(rotation); 
 } 
 public void onAnimationRepeat(Animation animation) { 
 // TODO Auto-generated method stub 
 } 
 public void onAnimationStart(Animation animation) { 
 // TODO Auto-generated method stub 
 } 
 } 
} 

main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
 
 

代码中用Camera来实现动画,Camera就是一个摄像机,一个物体原地不动,我们带着摄像机按设定的角度进行移动,之后从Camera中取出完成该动画的Matrix,然后画我们的物体,这个就是这个3D动画实现的原理。
具体的解释见代码中注释部分,重点说一下Rotate3dAnimation.java中的

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

由于旋转是以(0,0)为中心的,所以为了把界面的中心与(0,0)对齐,就要preTranslate(-centerX, -centerY),旋转完成后,调用postTranslate(centerX, centerY),再把图片移回来,这样看到的动画效果就是activity的界面图片从在centerX为中心绕Y轴旋转了。
你还可以把上面代码改成

matrix.preTranslate(-centerX, 0); 
matrix.postTranslate(centerX, 0); 

看有什么不同效果。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。


推荐阅读
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了如何使用Spring Boot进行高效开发,涵盖了配置、实例化容器以及核心注解的使用方法。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文介绍如何在 Unity 的 XML 配置文件中,将参数传递给自定义生命周期管理器的构造函数。我们将详细探讨 CustomLifetimeManager 类的实现及其配置方法。 ... [详细]
  • 本文详细介绍了 Java 中 org.apache.xmlbeans.SchemaType 类的 getBaseEnumType() 方法,提供了多个代码示例,并解释了其在不同场景下的使用方法。 ... [详细]
  • 本文详细介绍了如何解决MyBatis中常见的BindingException错误,提供了多种排查和修复方法,确保Mapper接口与XML文件的正确配置。 ... [详细]
author-avatar
Era_zhou
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有