欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)文字翻轉(zhuǎn)動(dòng)畫的效果

 更新時(shí)間:2016年10月21日 11:05:05   作者:小小藍(lán)莓082  
本文實(shí)現(xiàn)了Android程序文字翻轉(zhuǎn)動(dòng)畫的實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的朋友可以了解一下。

本文實(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ì)本站的支持!

相關(guān)文章

最新評(píng)論