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


本示例为接下来的“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()中的:

  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. }  

相关内容