Android實現文字翻轉動畫的效果_Android

來源:互聯網
上載者:User

本文實現了Android程式文字翻轉動畫的小程式,具體代碼如下:

先上效果圖如下:

要求:

沿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()中的:if (overHalf) {   // 翻轉過半的情況下,為保證數字仍為可讀的文字而非鏡面效果的文字,需翻轉180度。   degree = degree - 180; } 

動畫翻轉到一半後,應更新數字內容。為了得知翻轉進度,於RotateAnimation中設計一內部靜態介面類"InterpolatedTimeListener",該介面只有一個方法"interpolatedTime(float interpolatedTime)"可以將動畫進度傳遞給監聽發起者。

Java代碼如下,XML請根據效果圖自行實現:


ActRotate.java

package lab.sodino.rotate;  import lab.sodino.rotate.RotateAnimation.InterpolatedTimeListener; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;  /**  * @author Sodino E-mail:sodinoopen@hotmail.com  * @version Time:2012-6-27 上午07:32:00  */ public class ActRotate extends Activity implements OnClickListener, InterpolatedTimeListener {   private Button btnIncrease, btnDecrease;   private TextView txtNumber;   private int number;   /** TextNumber是否允許顯示最新的數字。 */   private boolean enableRefresh;    public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      btnIncrease = (Button) findViewById(R.id.btnIncrease);     btnDecrease = (Button) findViewById(R.id.btnDecrease);     txtNumber = (TextView) findViewById(R.id.txtNumber);      btnIncrease.setOnClickListener(this);     btnDecrease.setOnClickListener(this);      number = 3;     txtNumber = (TextView) findViewById(R.id.txtNumber);     txtNumber.setText(Integer.toString(number));   }    public void onClick(View v) {     enableRefresh = true;     RotateAnimation rotateAnim = null;     float cX = txtNumber.getWidth() / 2.0f;     float cY = txtNumber.getHeight() / 2.0f;     if (v == btnDecrease) {       number--;       rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_DECREASE);     } else if (v == btnIncrease) {       number++;       rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_INCREASE);     }     if (rotateAnim != null) {       rotateAnim.setInterpolatedTimeListener(this);       rotateAnim.setFillAfter(true);       txtNumber.startAnimation(rotateAnim);     }   }    @Override   public void interpolatedTime(float interpolatedTime) {     // 監聽到翻轉進度過半時,更新txtNumber顯示內容。     if (enableRefresh && interpolatedTime > 0.5f) {       txtNumber.setText(Integer.toString(number));       Log.d("ANDROID_LAB", "setNumber:" + number);       enableRefresh = false;     }   } } 

RotateAnimation.java

import android.view.animation.Animation; import android.view.animation.Transformation;  /**  * @author Sodino E-mail:sodinoopen@hotmail.com  * @version Time:2012-6-27 上午07:32:00  */ public class RotateAnimation extends Animation {   /** 值為true時可明確查看動畫的旋轉方向。 */   public static final boolean DEBUG = false;   /** 沿Y軸正方向看,數值減1時動畫逆時針旋轉。 */   public static final boolean ROTATE_DECREASE = true;   /** 沿Y軸正方向看,數值減1時動畫順時針旋轉。 */   public static final boolean ROTATE_INCREASE = false;   /** Z軸上最大深度。 */   public static final float DEPTH_Z = 310.0f;   /** 動畫顯示時間長度。 */   public static final long DURATION = 800l;   /** 圖片翻轉類型。 */   private final boolean type;   private final float centerX;   private final float centerY;   private Camera camera;   /** 用於監聽動畫進度。當值過半時需更新txtNumber的內容。 */   private InterpolatedTimeListener listener;    public RotateAnimation(float cX, float cY, boolean type) {     centerX = cX;     centerY = cY;     this.type = type;     setDuration(DURATION);   }    public void initialize(int width, int height, int parentWidth, int parentHeight) {     // 在建構函式之後、getTransformation()之前調用本方法。     super.initialize(width, height, parentWidth, parentHeight);     camera = new Camera();   }    public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {     this.listener = listener;   }    protected void applyTransformation(float interpolatedTime, Transformation transformation) {     // interpolatedTime:動畫進度值,範圍為[0.0f,10.f]     if (listener != null) {       listener.interpolatedTime(interpolatedTime);     }     float from = 0.0f, to = 0.0f;     if (type == ROTATE_DECREASE) {       from = 0.0f;       to = 180.0f;     } else if (type == ROTATE_INCREASE) {       from = 360.0f;       to = 180.0f;     }     float degree = from + (to - from) * interpolatedTime;     boolean overHalf = (interpolatedTime > 0.5f);     if (overHalf) {       // 翻轉過半的情況下,為保證數字仍為可讀的文字而非鏡面效果的文字,需翻轉180度。       degree = degree - 180;     }     // float depth = 0.0f;     float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;     final Matrix matrix = transformation.getMatrix();     camera.save();     camera.translate(0.0f, 0.0f, depth);     camera.rotateY(degree);     camera.getMatrix(matrix);     camera.restore();     if (DEBUG) {       if (overHalf) {         matrix.preTranslate(-centerX * 2, -centerY);         matrix.postTranslate(centerX * 2, centerY);       }     } else {       //確保圖片的翻轉過程一直處於組件的中心點位置       matrix.preTranslate(-centerX, -centerY);       matrix.postTranslate(centerX, centerY);     }   }    /** 動畫進度監聽器。 */   public static interface InterpolatedTimeListener {     public void interpolatedTime(float interpolatedTime);   } } 

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.