热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android开发教程:文字翻转动画的实现

nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd

本示例为接下来的“SurfaceView使用实例”做铺垫(见 2012-06/64050.htm )。

先上效果图如下:



要求:
沿Y轴正方向看,数值减1时动画逆时针旋转,数值加1时动画顺时针旋转。

实现动画的具体细节见"RotateAnimation.java"。为方便查看动画旋转方向,可以将RotateAnimation.DEBUG值设置为true即可。

         
RotateAnimation参考自APIDemos的Rotate3DAnimation

         RotateAnimation的构造函数需有三个参数,分别说明动画组件的中心点位置及旋转方向。

         RotateAnimation.initialize()将初始化动画组件及其父容器的宽高;通常亦可进行另外的初始化工作,本例中用于执行对camera进行实例化赋值。

         RotateAnimation.applyTransformation()第一个参数为动画的进度时间值,取值范围为[0.0f,1.0f],第二个参数Transformation记录着动画某一帧中变形的原始数据。该方法在动画的每一帧显示过程中都会被调用。

         
在翻转过程中,为了避免在动画下半部分时图像为镜面效果影响数字的阅读,应将翻转角度做180度的减法。代码为RotateAnimation.applyTransformation()中的:

  1. if (overHalf) {  
  2.     // 翻转过半的情况下,为保证数字仍为可读的文字而非镜面效果的文字,需翻转180度。   
  3.     degree = degree - 180;  
  4. }  

动画翻转到一半后,应更新数字内容。为了得知翻转进度,于RotateAnimation中设计一内部静态接口类"InterpolatedTimeListener",该接口只有一个方法"interpolatedTime(float interpolatedTime)"可以将动画进度传递给监听发起者。

Java代码如下,XML请根据效果图自行实现:


ActRotate.java

  1. package lab.sodino.rotate;  
  2.   
  3. import lab.sodino.rotate.RotateAnimation.InterpolatedTimeListener;  
  4. import Android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * @author Sodino E-mail:sodinoopen@hotmail.com 
  14.  * @version Time:2012-6-27 上午07:32:00 
  15.  */  
  16. public class ActRotate extends Activity implements OnClickListener, InterpolatedTimeListener {  
  17.     private Button btnIncrease, btnDecrease;  
  18.     private TextView txtNumber;  
  19.     private int number;  
  20.     /** TextNumber是否允许显示最新的数字。 */  
  21.     private boolean enableRefresh;  
  22.   
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.   
  27.         btnIncrease = (Button) findViewById(R.id.btnIncrease);  
  28.         btnDecrease = (Button) findViewById(R.id.btnDecrease);  
  29.         txtNumber = (TextView) findViewById(R.id.txtNumber);  
  30.   
  31.         btnIncrease.setOnClickListener(this);  
  32.         btnDecrease.setOnClickListener(this);  
  33.   
  34.         number = 3;  
  35.         txtNumber = (TextView) findViewById(R.id.txtNumber);  
  36.         txtNumber.setText(Integer.toString(number));  
  37.     }  
  38.   
  39.     public void onClick(View v) {  
  40.         enableRefresh = true;  
  41.         RotateAnimation rotateAnim = null;  
  42.         float cX = txtNumber.getWidth() / 2.0f;  
  43.         float cY = txtNumber.getHeight() / 2.0f;  
  44.         if (v == btnDecrease) {  
  45.             number--;  
  46.             rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_DECREASE);  
  47.         } else if (v == btnIncrease) {  
  48.             number++;  
  49.             rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_INCREASE);  
  50.         }  
  51.         if (rotateAnim != null) {  
  52.             rotateAnim.setInterpolatedTimeListener(this);  
  53.             rotateAnim.setFillAfter(true);  
  54.             txtNumber.startAnimation(rotateAnim);  
  55.         }  
  56.     }  
  57.   
  58.     @Override  
  59.     public void interpolatedTime(float interpolatedTime) {  
  60.         // 监听到翻转进度过半时,更新txtNumber显示内容。   
  61.         if (enableRefresh && interpolatedTime > 0.5f) {  
  62.             txtNumber.setText(Integer.toString(number));  
  63.             Log.d("ANDROID_LAB""setNumber:" + number);  
  64.             enableRefresh = false;  
  65.         }  
  66.     }  
  67. }  

RotateAnimation.java

  1. package lab.sodino.rotate;  
  2.   
  3. import android.graphics.Camera;  
  4. import android.graphics.Matrix;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.Transformation;  
  7.   
  8. /** 
  9.  * @author Sodino E-mail:sodinoopen@hotmail.com 
  10.  * @version Time:2012-6-27 上午07:32:00 
  11.  */  
  12. public class RotateAnimation extends Animation {  
  13.     /** 值为true时可明确查看动画的旋转方向。 */  
  14.     public static final boolean DEBUG = false;  
  15.     /** 沿Y轴正方向看,数值减1时动画逆时针旋转。 */  
  16.     public static final boolean ROTATE_DECREASE = true;  
  17.     /** 沿Y轴正方向看,数值减1时动画顺时针旋转。 */  
  18.     public static final boolean ROTATE_INCREASE = false;  
  19.     /** Z轴上最大深度。 */  
  20.     public static final float DEPTH_Z = 310.0f;  
  21.     /** 动画显示时长。 */  
  22.     public static final long DURATION = 800l;  
  23.     /** 图片翻转类型。 */  
  24.     private final boolean type;  
  25.     private final float centerX;  
  26.     private final float centerY;  
  27.     private Camera camera;  
  28.     /** 用于监听动画进度。当值过半时需更新txtNumber的内容。 */  
  29.     private InterpolatedTimeListener listener;  
  30.   
  31.     public RotateAnimation(float cX, float cY, boolean type) {  
  32.         centerX = cX;  
  33.         centerY = cY;  
  34.         this.type = type;  
  35.         setDuration(DURATION);  
  36.     }  
  37.   
  38.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  39.         // 在构造函数之后、getTransformation()之前调用本方法。   
  40.         super.initialize(width, height, parentWidth, parentHeight);  
  41.         camera = new Camera();  
  42.     }  
  43.   
  44.     public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {  
  45.         this.listener = listener;  
  46.     }  
  47.   
  48.     protected void applyTransformation(float interpolatedTime, Transformation transformation) {  
  49.         // interpolatedTime:动画进度值,范围为[0.0f,10.f]   
  50.         if (listener != null) {  
  51.             listener.interpolatedTime(interpolatedTime);  
  52.         }  
  53.         float from = 0.0f, to = 0.0f;  
  54.         if (type == ROTATE_DECREASE) {  
  55.             from = 0.0f;  
  56.             to = 180.0f;  
  57.         } else if (type == ROTATE_INCREASE) {  
  58.             from = 360.0f;  
  59.             to = 180.0f;  
  60.         }  
  61.         float degree = from + (to - from) * interpolatedTime;  
  62.         boolean overHalf = (interpolatedTime > 0.5f);  
  63.         if (overHalf) {  
  64.             // 翻转过半的情况下,为保证数字仍为可读的文字而非镜面效果的文字,需翻转180度。   
  65.             degree = degree - 180;  
  66.         }  
  67.         // float depth = 0.0f;   
  68.         float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;  
  69.         final Matrix matrix = transformation.getMatrix();  
  70.         camera.save();  
  71.         camera.translate(0.0f, 0.0f, depth);  
  72.         camera.rotateY(degree);  
  73.         camera.getMatrix(matrix);  
  74.         camera.restore();  
  75.         if (DEBUG) {  
  76.             if (overHalf) {  
  77.                 matrix.preTranslate(-centerX * 2, -centerY);  
  78.                 matrix.postTranslate(centerX * 2, centerY);  
  79.             }  
  80.         } else {  
  81.             //确保图片的翻转过程一直处于组件的中心点位置   
  82.             matrix.preTranslate(-centerX, -centerY);  
  83.             matrix.postTranslate(centerX, centerY);  
  84.         }  
  85.     }  
  86.   
  87.     /** 动画进度监听器。 */  
  88.     public static interface InterpolatedTimeListener {  
  89.         public void interpolatedTime(float interpolatedTime);  
  90.     }  
  91. }  

推荐阅读
  • 本文介绍了Java的集合及其实现类,包括数据结构、抽象类和具体实现类的关系,详细介绍了List接口及其实现类ArrayList的基本操作和特点。文章通过提供相关参考文档和链接,帮助读者更好地理解和使用Java的集合类。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 集合的遍历方式及其局限性
    本文介绍了Java中集合的遍历方式,重点介绍了for-each语句的用法和优势。同时指出了for-each语句无法引用数组或集合的索引的局限性。通过示例代码展示了for-each语句的使用方法,并提供了改写为for语句版本的方法。 ... [详细]
  • Oracle优化新常态的五大禁止及其性能隐患
    本文介绍了Oracle优化新常态中的五大禁止措施,包括禁止外键、禁止视图、禁止触发器、禁止存储过程和禁止JOB,并分析了这些禁止措施可能带来的性能隐患。文章还讨论了这些禁止措施在C/S架构和B/S架构中的不同应用情况,并提出了解决方案。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文介绍了Java的公式汇总及相关知识,包括定义变量的语法格式、类型转换公式、三元表达式、定义新的实例的格式、引用类型的方法以及数组静态初始化等内容。希望对读者有一定的参考价值。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • C++语言入门:数组的基本知识和应用领域
    本文介绍了C++语言的基本知识和应用领域,包括C++语言与Python语言的区别、C++语言的结构化特点、关键字和控制语句的使用、运算符的种类和表达式的灵活性、各种数据类型的运算以及指针概念的引入。同时,还探讨了C++语言在代码效率方面的优势和与汇编语言的比较。对于想要学习C++语言的初学者来说,本文提供了一个简洁而全面的入门指南。 ... [详细]
author-avatar
欣儿2502862161
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有