Android realizes the effect of text flip animation _android

Source: Internet
Author: User
Tags readable

This article realizes the Android program text to flip the animation of the small program, the specific code is as follows:

The first effect chart is as follows:

Requirements:

Along the y-axis positive direction, the value minus 1 o'clock the animation counterclockwise rotation, the value plus 1 o'clock animation clockwise rotation.

To achieve the specific details of the animation see "Rotateanimation.java". To facilitate viewing of the animation rotation direction, you can set the Rotateanimation.debug value to true.

Rotateanimation Reference from Apidemos rotate3danimation

The Rotateanimation constructor requires three parameters that describe the center point and direction of rotation of the animation component.

Rotateanimation.initialize () initializes the height of the animation component and its parent container, and can often be used for additional initialization, in this case to perform an instantiation assignment to camera.

Rotateanimation.applytransformation () The first parameter is the progress time value of the animation, the value range is [0.0f,1.0f], and the second parameter transformation records the original data that is distorted in one frame of the animation. This method is invoked during the display of each frame of the animation.

In the flip process, in order to avoid in the lower part of the animation image for the mirror effect of the digital reading, the flip angle should be done 180 degree subtraction. Code is

Rotateanimation.applytransformation ():
if (overhalf) { 
  //flip over half of the case, to ensure that the number is still readable text rather than specular effects of text, you need to flip 180 degrees. 
  degree = degree-180; 
} 

After you flip the animation to half, you should update the numeric content. In order to know the flip progress, in rotateanimation design an internal static interface class "Interpolatedtimelistener", the interface has only one method "Interpolatedtime" (float Interpolatedtime) "Can pass the animation progress to the listener initiator.

The Java code is as follows, and XML is implemented according to the effect diagram:

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 Morning 07:32:00 * * public class Actrotate E 
  Xtends activity implements Onclicklistener, Interpolatedtimelistener {private Button btnincrease, btndecrease; 
  Private TextView Txtnumber; 
  private int number; /** Textnumber Whether the latest numbers are allowed to be displayed. 
 
  * * 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) {//supervisor hears more than half of the rollover progress, update txtnumber display content. 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 Morning 07:32:00 * * public class Rotateanima tion extends Animation {/** value is true to see the rotation direction of the animation explicitly. 
  */public static final Boolean DEBUG = false; /** along the y-axis positive direction, the value minus 1 o'clock the animation rotates counterclockwise. 
  * * public static final Boolean rotate_decrease = true; /** along the y-axis positive direction, the value minus 1 o'clock the animation rotates clockwise. 
  * * public static final Boolean rotate_increase = false; /** the maximum depth on the z axis. 
  * * public static final float depth_z = 310.0f; /** Animation Displays the length of time. 
  * * public static final long DURATION = 800l; /** a picture flip type. 
  * Private Final Boolean type; 
  Private final float CenterX; 
  Private final float centery; 
  Private Camera Camera; /** is used to monitor animation progress. You need to update the contents of Txtnumber when you are halfway through. 
 
  * * 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) {//after constructor, Gettransformat 
    This method is called before Ion (). 
    Super.initialize (width, height, parentwidth, parentheight); 
  Camera = new camera (); 
  public void Setinterpolatedtimelistener (Interpolatedtimelistener listener) {This.listener = listener; } protected void Applytransformation (float interpolatedtime, transformation transformation) {//Interpolatedtim 
    E: Animation Progress value, range is [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) {//Flip half of the case, to ensure that the number is still readable text rather than specular effects of text, you need to flip 180 degrees. 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 {//ensure that the image rollover process is always at the center point of the component Matrix.pretranslate (-centerx,-centery); 
    Matrix.posttranslate (CenterX, centery); }/** the animation progress listener. 
  */public static interface Interpolatedtimelistener {public void Interpolatedtime (float interpolatedtime); 

 } 
}

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.