国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 05:00:37
字體:
來源:轉載
供稿:網友

本文實現了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);   } } 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 治多县| 江永县| 汤阴县| 德兴市| 满洲里市| 商洛市| 巨野县| 喀喇沁旗| 大悟县| 东乡族自治县| 思茅市| 镇安县| 蓬莱市| 正阳县| 大庆市| 视频| 内江市| 龙南县| 湟源县| 晋中市| 上高县| 西林县| 韶关市| 久治县| 滦南县| 杂多县| 江安县| 宝丰县| 许昌县| 德保县| 将乐县| 和顺县| 永善县| 建宁县| 鹤庆县| 沧州市| 邵阳市| 广水市| 新建县| 常山县| 扎赉特旗|