Android 文字翻转动画的实现

简介:

http://blog.csdn.net/sodino/article/details/7703980

本示例为接下来的“SurfaceView使用实例做铺垫。


先上效果图如下:



要求:
沿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()中的:

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

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

本文章由Sodino所有,转载请注明出处:http://blog.csdn.net/sodino/article/details/7703980


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


ActRotate.java

[java]  view plain copy
  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

[java]  view plain copy
  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. }  

相关文章
|
10天前
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
35 2
基于Android P,自定义Android开机动画的方法
|
3天前
|
Android开发
Android经典实战之Textview文字设置不同颜色、下划线、加粗、超链接等效果
本文介绍了 `SpannableString` 在 Android 开发中的强大功能,包括如何在单个字符串中应用多种样式,如颜色、字体大小、风格等,并提供了详细代码示例,展示如何设置文本颜色、添加点击事件等,助你实现丰富文本效果。
19 3
|
25天前
|
数据处理 开发工具 数据安全/隐私保护
Android平台RTMP推送|轻量级RTSP服务|GB28181接入之文字、png图片水印的精进之路
本文探讨了Android平台上推流模块中添加文字与PNG水印的技术演进。自2015年起,为了满足应急指挥及安防领域的需求,逐步发展出三代水印技术:第一代为静态文字与图像水印;第二代实现了动态更新水印内容的能力,例如实时位置与时间信息;至第三代,则优化了数据传输效率,直接使用Bitmap对象传递水印数据至JNI层,减少了内存拷贝次数。这些迭代不仅提升了用户体验和技术效率,也体现了开发者追求极致与不断创新的精神。
|
4月前
|
Java Android开发 开发者
Android10 修改开发者选项中动画缩放默认值
Android10 修改开发者选项中动画缩放默认值
104 0
|
4月前
|
XML Java Android开发
android的三种动画
android的三种动画
32 0
|
2月前
|
XML Android开发 数据格式
Android 中如何设置activity的启动动画,让它像dialog一样从底部往上出来
在 Android 中实现 Activity 的对话框式过渡动画:从底部滑入与从顶部滑出。需定义两个 XML 动画文件 `activity_slide_in.xml` 和 `activity_slide_out.xml`,分别控制 Activity 的进入与退出动画。使用 `overridePendingTransition` 方法在启动 (`startActivity`) 或结束 (`finish`) Activity 时应用这些动画。为了使前 Activity 保持静止,可定义 `no_animation.xml` 并在启动新 Activity 时仅设置新 Activity 的进入动画。
49 12
|
2月前
|
XML Android开发 UED
Android动画之共享元素动画简单实践
本文介绍Android共享元素动画, 实现两Activity间平滑过渡特定UI元素。通过设置`transitionName`属性和使用`ActivityOptions.makeSceneTransitionAnimation`启动目标Activity实现动画效果。可自定义过渡动画提升体验。
39 0
|
3月前
|
Android开发 UED
Android Item平移动画
【6月更文挑战第18天】
|
2月前
|
Android开发
android 动画 插值器和估值器
android 动画 插值器和估值器
|
4月前
|
自然语言处理 算法 搜索推荐
Android文字匹配度算法
【5月更文挑战第15天】