The traditional way of rotating animation is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "Http://schemas.android.com/apk/res/android" > <rotate android:fromdegrees= "0" android:todegrees= "359" android:duration= " Android:repeatcount= "-1" android:pivotx= "50%" android:pivoty= "50%"/> </set>
Can only be rotated within a 2-dimensional plane, to achieve 3D can only use OpenGL or simplified 3D animation implementationCameraClass.
Rotate3danimation in API demo how to use camera to implement 3D flip animation, this article explains how to apply this animation to the digital rollover.
The most common thing about digital flipping is that in a calendar application, when the date changes, the number flips to another value, and the flip differs from the general one, which has the following characteristics:
1. The number increases or decreases the direction of the flip, the general increase is clockwise, the reduction is counterclockwise.
2. Flipping a number requires changing the value of the number at the same time: it needs to update its display when the rollover is halfway.
3. This is not important. We need a font that can beautify numbers.
4. In order to make the flip more vivid needs to change the transparency according to certain regularity during the flip process.
The following diagram shows the 4-5 rollover process:
Realize:
We wrote a rotate3dnumberanimation in the API demo Rotate3danimation:
The Rotate3dnumberanimation constructor needs to have three parameters, which describe the position of the center point and the direction of rotation of the animation component respectively.
Rotate3dnumberanimation.initialize () initializes the width of the animation component and its parent container, usually with additional initialization, which is used in this example to perform an instantiation assignment to the camera.
Rotate3dnumberanimation.applytransformation () The first parameter is the progress time value of the animation, the value range is [0.0f,1.0f], The second parameter, transformation, records the original data that was deformed in a frame of animation. This method is called during the display of each frame of the animation.
In the flip process, in order to avoid the animation in the lower part of the image as the mirror effect of the reading of the digital, the flip angle should be 180 degrees of subtraction. The code is in Rotate3dnumberanimation.applytransformation ():
if (overhalf) { //flip over half of the case, to ensure that the number is still readable text, but not the mirror effect of the text, you need to flip 180 degrees. degree = degree-180;}
when the animation flips to half, the digital content should be updated. In order to know the rollover progress, design an internal static interface class "Interpolatedtimelistener" in rotate3dnumberanimation , which has only one method "Interpolatedtime ( float interpolatedtime) "Can pass the animation progress to the listener initiator. In addition to updating the digital content after flipping half, we also need to change the transparency of the numbers according to this listener.
@SuppressLint ("Newapi") @Overridepublic void Interpolatedtime (float interpolatedtime) { //supervisor hears 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; } Change the transparency if (Interpolatedtime > 0.5f) { Txtnumber.setalpha ((interpolatedtime-0.5f) * 2); } else { Txtnumber.setalpha (1-interpolatedtime * 2);} }
When the flip half of the transparency is changed from 1 to 0, then the transparency is changed from 0 to 1 during the subsequent rollover process. We used the@SuppressLint("NewApi")是因为view.setAlpha方法不支持api 11以下,如果你想兼容2.x的设备,请用nineoldandroids库,并用ViewHelper.setAlpha()替代。
To beautify the numbers, we put the font HELVETICANEUELTPRO-THEX.OTF in the assets/fonts directory. The code for setting the font is as follows:
Typeface localtypeface = Typeface.createfromasset (Getassets (), "FONTS/HELVETICANEUELTPRO-THEX.OTF"); Txtnumber.settypeface (Localtypeface);
Also add a shadow effect to the font:
<style name= "Text_shadow_style" > <item name= "Android:shadowcolor" > #80000000 </item> <item name= "ANDROID:SHADOWDX" >1.0</item> <item name= "Android:shadowdy" >1.0</item> <item name= "Android:shadowradius" >0.5</item> </style>
Here's all the Java code:
mainactivity
Package Com.example.rotatedemo; Import Com.example.rotatedemo.Rotate3DN Umberanimation.interpolatedtimelistener;import Android.annotation.suppresslint;import android.app.Activity; Import Android.graphics.typeface;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 www.jcodecraeer.com * */public class Mainactivity extends Activity implements Onclicklistener, Interpolatedtimelistener {Private Button btnincrease, btndecrease; Private TextView Txtnumber; private int number; /** Textnumber whether to allow the latest numbers to be displayed. */Private Boolean Enablerefresh; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_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; Typeface localtypeface = Typeface.createfromasset (Getassets (), "FONTS/HELVETICANEUELTPRO-THEX.OTF"); Txtnumber.settypeface (Localtypeface); Txtnumber.settext (integer.tostring (number)); The public void OnClick (View v) {Enablerefresh = true; Rotate3dnumberanimation Rotateanim = null; float CX = txtnumber.getwidth ()/2.0f; float CY = txtnumber.getheight ()/2.0f; if (v = = btndecrease) {number--; Rotateanim = new Rotate3dnumberanimation (CX, CY, rotate3dnumberanimation.rotate_decrease); } else if (v = = btnincrease) {number++; Rotateanim = new Rotate3dnumberanimatIon (CX, CY, rotate3dnumberanimation.rotate_increase); } if (Rotateanim! = null) {Rotateanim.setinterpolatedtimelistener (this); Rotateanim.setfillafter (TRUE); Txtnumber.startanimation (Rot Ateanim); }} @SuppressLint ("Newapi") @Override public void Interpolatedtime (float interpolatedtime) {//Monitor to hear rollover progress At half time, update txtnumber display content. if (Enablerefresh && interpolatedtime > 0.5f) {txtnumber.settext (integer.tostring (number)); LOG.D ("Android_lab", "Setnumber:" + number); Enablerefresh = false; }//Change the transparency if (Interpolatedtime > 0.5f) {txtnumber.setalpha ((interpolatedtime-0.5f) * 2); } else {Txtnumber.setalpha (1-interpolatedtime * 2); } }}rotate3dnumberanimation
Package Com.example.rotatedemo;import Android.graphics.camera;import Android.graphics.matrix;import Android.view.animation.animation;import android.view.animation.transformation;/** * @author www.jcodecraeer.com * * public class Rotate3dnumberanimation extends Animation {/** value is true to explicitly view the direction of rotation of the animation. */public static final Boolean DEBUG = false; /** along the y-axis positive direction, the value minus 1 o'clock animation counterclockwise rotation. */public static final Boolean rotate_decrease = true; /** along the y-axis positive direction, the value minus 1 o'clock animation clockwise rotation. */public static final Boolean rotate_increase = false; /** the maximum depth on the z axis. */public static final float depth_z = 0.0f; /** Animation Displays the length of time. */public static final long DURATION = 500; /** picture Flip type. */Private final Boolean type; Private final float CenterX; Private final float centery; Private camera camera; The/** is used to monitor the animation progress. You need to update the contents of Txtnumber when you are halfway. */private Interpolatedtimelistener listener; Public rotate3dnumberanimation (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, Gettransformatio Call this method before N (). 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 [0.0f,1.0f] if (listener! = null) {listener.interpolatedtime (interpolatedtime); float from = 0.0f and 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 over half of the case, to ensure that the number is still readable text rather than the mirror effect of the 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 {//ensures that the picture's rollover process is always at the center point of the component Matrix.pretranslate (-centerx,-centery); Matrix.posttranslate (CenterX, centery); }}/** animation progress listener. */public static interface Interpolatedtimelistener {public void Interpolatedtime (float interpolatedtime); }}Project Address: http://jcodecraeer.com/a/opensource/2014/1012/1755.html
Note: The background picture is larger, if you compare cards on the low-end machine, you can change the activity background to a solid color.
[Android] 3d rollover effect for digital