歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:文字翻轉動畫的實現

Android開發教程:文字翻轉動畫的實現

日期:2017/3/1 10:18:34   编辑:Linux編程

本示例為接下來的“SurfaceView使用實例”做鋪墊(見 http://www.linuxidc.com/Linux/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. import lab.sodino.rotate.RotateAnimation.InterpolatedTimeListener;
  3. import Android.app.Activity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. /**
  11. * @author Sodino E-mail:[email protected]
  12. * @version Time:2012-6-27 上午07:32:00
  13. */
  14. public class ActRotate extends Activity implements OnClickListener, InterpolatedTimeListener {
  15. private Button btnIncrease, btnDecrease;
  16. private TextView txtNumber;
  17. private int number;
  18. /** TextNumber是否允許顯示最新的數字。 */
  19. private boolean enableRefresh;
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. btnIncrease = (Button) findViewById(R.id.btnIncrease);
  24. btnDecrease = (Button) findViewById(R.id.btnDecrease);
  25. txtNumber = (TextView) findViewById(R.id.txtNumber);
  26. btnIncrease.setOnClickListener(this);
  27. btnDecrease.setOnClickListener(this);
  28. number = 3;
  29. txtNumber = (TextView) findViewById(R.id.txtNumber);
  30. txtNumber.setText(Integer.toString(number));
  31. }
  32. public void onClick(View v) {
  33. enableRefresh = true;
  34. RotateAnimation rotateAnim = null;
  35. float cX = txtNumber.getWidth() / 2.0f;
  36. float cY = txtNumber.getHeight() / 2.0f;
  37. if (v == btnDecrease) {
  38. number--;
  39. rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_DECREASE);
  40. } else if (v == btnIncrease) {
  41. number++;
  42. rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_INCREASE);
  43. }
  44. if (rotateAnim != null) {
  45. rotateAnim.setInterpolatedTimeListener(this);
  46. rotateAnim.setFillAfter(true);
  47. txtNumber.startAnimation(rotateAnim);
  48. }
  49. }
  50. @Override
  51. public void interpolatedTime(float interpolatedTime) {
  52. // 監聽到翻轉進度過半時,更新txtNumber顯示內容。
  53. if (enableRefresh && interpolatedTime > 0.5f) {
  54. txtNumber.setText(Integer.toString(number));
  55. Log.d("ANDROID_LAB", "setNumber:" + number);
  56. enableRefresh = false;
  57. }
  58. }
  59. }

RotateAnimation.java

  1. package lab.sodino.rotate;
  2. import android.graphics.Camera;
  3. import android.graphics.Matrix;
  4. import android.view.animation.Animation;
  5. import android.view.animation.Transformation;
  6. /**
  7. * @author Sodino E-mail:[email protected]
  8. * @version Time:2012-6-27 上午07:32:00
  9. */
  10. public class RotateAnimation extends Animation {
  11. /** 值為true時可明確查看動畫的旋轉方向。 */
  12. public static final boolean DEBUG = false;
  13. /** 沿Y軸正方向看,數值減1時動畫逆時針旋轉。 */
  14. public static final boolean ROTATE_DECREASE = true;
  15. /** 沿Y軸正方向看,數值減1時動畫順時針旋轉。 */
  16. public static final boolean ROTATE_INCREASE = false;
  17. /** Z軸上最大深度。 */
  18. public static final float DEPTH_Z = 310.0f;
  19. /** 動畫顯示時長。 */
  20. public static final long DURATION = 800l;
  21. /** 圖片翻轉類型。 */
  22. private final boolean type;
  23. private final float centerX;
  24. private final float centerY;
  25. private Camera camera;
  26. /** 用於監聽動畫進度。當值過半時需更新txtNumber的內容。 */
  27. private InterpolatedTimeListener listener;
  28. public RotateAnimation(float cX, float cY, boolean type) {
  29. centerX = cX;
  30. centerY = cY;
  31. this.type = type;
  32. setDuration(DURATION);
  33. }
  34. public void initialize(int width, int height, int parentWidth, int parentHeight) {
  35. // 在構造函數之後、getTransformation()之前調用本方法。
  36. super.initialize(width, height, parentWidth, parentHeight);
  37. camera = new Camera();
  38. }
  39. public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
  40. this.listener = listener;
  41. }
  42. protected void applyTransformation(float interpolatedTime, Transformation transformation) {
  43. // interpolatedTime:動畫進度值,范圍為[0.0f,10.f]
  44. if (listener != null) {
  45. listener.interpolatedTime(interpolatedTime);
  46. }
  47. float from = 0.0f, to = 0.0f;
  48. if (type == ROTATE_DECREASE) {
  49. from = 0.0f;
  50. to = 180.0f;
  51. } else if (type == ROTATE_INCREASE) {
  52. from = 360.0f;
  53. to = 180.0f;
  54. }
  55. float degree = from + (to - from) * interpolatedTime;
  56. boolean overHalf = (interpolatedTime > 0.5f);
  57. if (overHalf) {
  58. // 翻轉過半的情況下,為保證數字仍為可讀的文字而非鏡面效果的文字,需翻轉180度。
  59. degree = degree - 180;
  60. }
  61. // float depth = 0.0f;
  62. float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;
  63. final Matrix matrix = transformation.getMatrix();
  64. camera.save();
  65. camera.translate(0.0f, 0.0f, depth);
  66. camera.rotateY(degree);
  67. camera.getMatrix(matrix);
  68. camera.restore();
  69. if (DEBUG) {
  70. if (overHalf) {
  71. matrix.preTranslate(-centerX * 2, -centerY);
  72. matrix.postTranslate(centerX * 2, centerY);
  73. }
  74. } else {
  75. //確保圖片的翻轉過程一直處於組件的中心點位置
  76. matrix.preTranslate(-centerX, -centerY);
  77. matrix.postTranslate(centerX, centerY);
  78. }
  79. }
  80. /** 動畫進度監聽器。 */
  81. public static interface InterpolatedTimeListener {
  82. public void interpolatedTime(float interpolatedTime);
  83. }
  84. }
Copyright © Linux教程網 All Rights Reserved