[Android] Text flip animation implementation

Source: Internet
Author: User
Tags image flip

This example paves the way for "surfaceview using instances.

First, go to the following:

Requirements:
Along the positive direction of the Y axis, when the value is reduced by 1, the animation rotates counter-clockwise. When the value is increased by 1, the animation rotates clockwise.
For details about how to implement the animation for the animation, see "rotateanimation. Java ". To view the animation rotation direction, set rotateanimation. Debug to true. Bytes

 Rotateanimation refer to rotate3danimation metrics from apidemos
The rotateanimation constructor must have three parameters to describe the center position and rotation direction of the animation component. Bytes
Rotateanimation. initialize () initializes the width and height of the animation component and its parent container. It can also be initialized. In this example, it is used to instantiate and assign values to camera. Bytes
Rotateanimation. applytransformation () the first parameter is the animation progress time value. The value range is [0.0f, 1.0f]. The second parameter transformation records the original deformation data in a frame of the animation. This method is called during the display of each frame of the animation. Bytes
During the flip process, the flip angle should be reduced by 180 degrees to avoid the impact of the number reading when the image is mirrored in the lower part of the animation. The code is in rotateanimation. applytransformation:

If (overhalf) {// if you flip more than half of the text, you need to flip 180 degrees to ensure that the number is still readable text rather than mirroring. Degree = degree-180 ;}

After the animation is flipped to half, the number content should be updated. To learn the flip progress, design an internal static interface class "interpolatedtimelistener" in rotateanimation. This interface has only one method "interpolatedtime (float interpolatedtime)" to pass the animation progress to the listener initiator.

This article by sodino all, reprint please note the Source: http://blog.csdn.net/sodino/article/details/7703980

The Java code is as follows:

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: 07:32:00 */public class act Rotate extends activity implements onclicklistener, interpolatedtimelistener {private button btnincrease, btndecrease; private textview txtnumber; private int number;/** whether textnumber allows displaying the latest numbers. */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.txt number); btnincrease. setonclicklistener (this); btndecrease. setonclicklistener (this); number = 3; txtnumber = (Textview) findviewbyid(r.id.txt number); 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 ++; Rota Teanim = new rotateanimation (CX, Cy, rotateanimation. rotate_increase);} If (rotateanim! = NULL) {rotateanim. setinterpolatedtimelistener (this); rotateanim. setfillafter (true); txtnumber. startanimation (rotateanim) ;}@ overridepublic void interpolatedtime (float interpolatedtime) {// update the display content of txtnumber when the flip progress is over half. If (enablerefresh & interpolatedtime> 0.5f) {txtnumber. settext (integer. tostring (number); log. D ("android_lab", "setnumber:" + number); enablerefresh = false ;}}}

Rotateanimation. Java

Package lab. sodino. rotate; import android. graphics. camera; import android. graphics. matrix; import android. view. animation. animation; import android. view. animation. transformation;/*** @ author sodino E-mail: sodinoopen@hotmail.com * @ version time: 07:32:00 */public class rotateanimation extends animation {/** value is true, you can clearly view the animation rotation direction. */Public static final Boolean DEBUG = false;/** the value is displayed along the positive side of the Y axis. If the value is reduced by 1, the animation rotates counterclockwise. */Public static final Boolean rotate_decrease = true;/** it is displayed along the positive side of the Y axis. If the value is reduced by 1, the animation rotates clockwise. */Public static final Boolean rotate_increase = false;/** maximum depth on the Z axis. */Public static final float depth_z = 310.0f;/** animation display duration. */Public static final long duration = 800l;/** image flip type. */Private final boolean type; private final float centerx; private final float centery; private camera;/** monitors the animation progress. When the value is over half, the txtnumber content must be updated. */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 the constructor, gettransformation () call this method before. 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: Specifies the animation progress value in the range of [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) {// if you flip over half of the text, you need to flip 180 degrees to ensure that the number is still readable text rather than mirroring. Degree = degree-180;} // float depth = 0.0f; float depth = (0.5f-math. ABS (interpolatedtime-0.5f) * depth_z; Final 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 {// Make sure that the image flip process remains at the center of the component Position matrix. pretranslate (-centerx,-centery); matrix. posttranslate (centerx, centery);}/** animation progress listener. */Public static interface interpolatedtimelistener {public void interpolatedtime (float interpolatedtime );}}

Related Article

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.