Android實(shí)現(xiàn)文字翻轉(zhuǎn)動(dòng)畫的效果
本文實(shí)現(xiàn)了Android程序文字翻轉(zhuǎn)動(dòng)畫的小程序,具體代碼如下:
先上效果圖如下:
要求:
沿Y軸正方向看,數(shù)值減1時(shí)動(dòng)畫逆時(shí)針旋轉(zhuǎn),數(shù)值加1時(shí)動(dòng)畫順時(shí)針旋轉(zhuǎn)。
實(shí)現(xiàn)動(dòng)畫的具體細(xì)節(jié)見"RotateAnimation.Java"。為方便查看動(dòng)畫旋轉(zhuǎn)方向,可以將RotateAnimation.DEBUG值設(shè)置為true即可。
RotateAnimation參考自APIDemos的Rotate3DAnimation
RotateAnimation的構(gòu)造函數(shù)需有三個(gè)參數(shù),分別說明動(dòng)畫組件的中心點(diǎn)位置及旋轉(zhuǎn)方向。
RotateAnimation.initialize()將初始化動(dòng)畫組件及其父容器的寬高;通常亦可進(jìn)行另外的初始化工作,本例中用于執(zhí)行對(duì)camera進(jìn)行實(shí)例化賦值。
RotateAnimation.applyTransformation()第一個(gè)參數(shù)為動(dòng)畫的進(jìn)度時(shí)間值,取值范圍為[0.0f,1.0f],第二個(gè)參數(shù)Transformation記錄著動(dòng)畫某一幀中變形的原始數(shù)據(jù)。該方法在動(dòng)畫的每一幀顯示過程中都會(huì)被調(diào)用。
在翻轉(zhuǎn)過程中,為了避免在動(dòng)畫下半部分時(shí)圖像為鏡面效果影響數(shù)字的閱讀,應(yīng)將翻轉(zhuǎn)角度做180度的減法。代碼為
RotateAnimation.applyTransformation()中的: if (overHalf) { // 翻轉(zhuǎn)過半的情況下,為保證數(shù)字仍為可讀的文字而非鏡面效果的文字,需翻轉(zhuǎn)180度。 degree = degree - 180; }
動(dòng)畫翻轉(zhuǎn)到一半后,應(yīng)更新數(shù)字內(nèi)容。為了得知翻轉(zhuǎn)進(jìn)度,于RotateAnimation中設(shè)計(jì)一內(nèi)部靜態(tài)接口類"InterpolatedTimeListener",該接口只有一個(gè)方法"interpolatedTime(float interpolatedTime)"可以將動(dòng)畫進(jìn)度傳遞給監(jiān)聽發(fā)起者。
Java代碼如下,XML請(qǐng)根據(jù)效果圖自行實(shí)現(xiàn):
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是否允許顯示最新的數(shù)字。 */ 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) { // 監(jiān)聽到翻轉(zhuǎn)進(jìn)度過半時(shí),更新txtNumber顯示內(nèi)容。 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時(shí)可明確查看動(dòng)畫的旋轉(zhuǎn)方向。 */ public static final boolean DEBUG = false; /** 沿Y軸正方向看,數(shù)值減1時(shí)動(dòng)畫逆時(shí)針旋轉(zhuǎn)。 */ public static final boolean ROTATE_DECREASE = true; /** 沿Y軸正方向看,數(shù)值減1時(shí)動(dòng)畫順時(shí)針旋轉(zhuǎn)。 */ public static final boolean ROTATE_INCREASE = false; /** Z軸上最大深度。 */ public static final float DEPTH_Z = 310.0f; /** 動(dòng)畫顯示時(shí)長。 */ public static final long DURATION = 800l; /** 圖片翻轉(zhuǎn)類型。 */ private final boolean type; private final float centerX; private final float centerY; private Camera camera; /** 用于監(jiān)聽動(dòng)畫進(jìn)度。當(dāng)值過半時(shí)需更新txtNumber的內(nèi)容。 */ 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) { // 在構(gòu)造函數(shù)之后、getTransformation()之前調(diào)用本方法。 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:動(dòng)畫進(jìn)度值,范圍為[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) { // 翻轉(zhuǎn)過半的情況下,為保證數(shù)字仍為可讀的文字而非鏡面效果的文字,需翻轉(zhuǎn)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 { //確保圖片的翻轉(zhuǎn)過程一直處于組件的中心點(diǎn)位置 matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } } /** 動(dòng)畫進(jìn)度監(jiān)聽器。 */ public static interface InterpolatedTimeListener { public void interpolatedTime(float interpolatedTime); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android動(dòng)畫之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android圖片翻轉(zhuǎn)動(dòng)畫簡易實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動(dòng)畫效果
- android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫
- Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫效果
- Android利用Camera實(shí)現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
- Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫
- android camera yuv幀水平翻轉(zhuǎn)實(shí)例
- android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn)
相關(guān)文章
android socket聊天室功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了android socket聊天室功能實(shí)現(xiàn)方法,不單純是聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android開發(fā)實(shí)現(xiàn)圖片平移、縮放、倒影及旋轉(zhuǎn)功能的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)圖片平移、縮放、倒影及旋轉(zhuǎn)功能的方法,涉及Android針對(duì)圖片的讀取、寫入、屬性設(shè)置及矩陣運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android實(shí)戰(zhàn)打飛機(jī)游戲之菜單頁面設(shè)計(jì)(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之菜單頁面設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android編程自定義title bar(標(biāo)題欄)示例
這篇文章主要介紹了Android編程自定義title bar(標(biāo)題欄)的方法,結(jié)合實(shí)例形式分析了Android針對(duì)標(biāo)題欄的設(shè)置與頁面布局操作相關(guān)技巧,需要的朋友可以參考下2016-10-10Android如何通過命令行操作Sqlite3數(shù)據(jù)庫的方法
這篇文章主要介紹了Android如何通過命令行操作Sqlite3數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Android適配安卓6.0藍(lán)牙通訊實(shí)現(xiàn)過程
這篇文章主要為大家詳細(xì)介紹了Android適配安卓6.0藍(lán)牙通訊實(shí)現(xiàn)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09android 自定義圓角button效果的實(shí)例代碼(自定義view Demo)
這篇文章主要介紹了android 自定義圓角button(自定義View Demo),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09