In Android applications, animations can be used to give users a better sense of how to do animations that are implemented via XML or Android code.
Animation animation effect can be implemented in two ways, one is tweened animation (gradient animation), the other is a frame by frame animation (picture conversion animation).
tweened Animation has the following two types of gradient animations:
1.alpha Gradient Transparency Animation effect
2.scale Gradient size Stretch animation effect
Frame by Frame animation screen conversion animations are available in the following two categories:
1.translate picture Conversion position Move animation effect
2.rotate Picture Transfer rotation animation effect
In this case, I use XML to animate. Implement basic animations, such as fade in, rotate, etc.
steps: 1 . First create a new Anim folder in the Res directory and create a new animated XML resource file in the Anim.
Anim/alpha.xml (gradient animation)
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <alpha android:fromalpha= "0.1" android:toalpha= "1.0" android:duration= "/> <!" --Transparency Control animation effect Alpha float value: The Fromalpha property is the start of the animation when the transparency 0.0 represents the full transparent toalpha property for the end of the animation transparency 1.0 means completely opaque the above values take a numeric long integer value of float data type between 0.0-1.0 : The duration property is the animation duration in milliseconds--></set>
Anim/scale.xml (Stretch animation)
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <scale android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" Android:fromx scale= "0.0" android:toxscale= "1.4" android:fromyscale= "0.0" android:toyscale= "1.4" and roid:pivotx= "50%" android:pivoty= "50%" android:fillafter= "false" android:duration= "" "/>&L t;/set><!--Scaling Animation effect Scale property: interpolator Specifies an animated insert during the experiment, three kinds of animation inserts were found when using resources in Android.res.anim : Accelerate_decelerate_interpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation insert Decelerate_interpolator Deceleration-Animation insert other specific animation effects floating-point values: The Fromxscale property is the scaling dimension on the x-coordinate at the start of the animation The Toxscale property is the scaling dimension on the x-coordinate at the end of the animation Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation Toyscale property is the scaling dimension on the y-coordinate at the end of the animation Description: The above four property values 0.0 means that shrinking to no 1.0 means that a normal no-flex value of less than 1.0 means that the shrink value is greater than 1.0 to enlarge The Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is the start position of the animation relative to the object's y-coordinate: The above two attribute values are taken from the 0%-100% 50% is the long value of the midpoint position on the x or y-coordinate of the object: The Duration property is the animation duration in milliseconds for the Boolean value: Fil Lafter property When set to True, the animation conversion is applied after the end of the animation
Anim/translate.xml (Mobile animation)
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:fromxdelta= "android:toxdelta=" -80 " android:fromydelta=" Android:toydelta= "android:duration=" /><!--Translate position transfer animation effect integer value: fromxdelta The Toxdelta property is the position on the x-coordinate at the start of the animation the position of the x-coordinate at the end of the animation Fromydelta property is the position on the y-coordinate at the start of the animation Toydelta property is at the end of the animation Position on the y-coordinate Note: when Fromxtype toxtype fromytype Toytype is not specified, the default is to use itself as a relative reference long Integer value: duration The--></set> property is the animation duration in milliseconds
Anim/rotate.xml (rotate animation)
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <rotate android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" Android:fromdegr ees= "0" android:todegrees= "+350" android:pivotx= "50%" android:pivoty= "50%" and roid:duration=/> <!--rotate rotation animation effect properties: interpolator Specifies an animated insert during the experiment, use the Android.res.anim Resources when found there are three kinds of animation insert: accelerate_decelerate_interpolator acceleration-deceleration animation Insert Accelerate_int Erpolator Acceleration-Animation insert Decelerate_interpolator deceleration-Animation insert other animation effects that belong to a specific Floating-point value: The Fromdegrees property is the angle of the object at the beginning of the animation Todegrees property is the angle of the object rotation at the end of the animation can be large Note at 360 degrees: when the angle is negative--indicates counterclockwise rotation when the angle is positive--Indicates clockwise rotation ( Negative from--to positive: clockwise rotation) (Negative from--to negative: counterclockwise rotation) (Positive from--to positive: clockwise rotation) (Positive from--to negative number: counterclockwise rotation) The Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is the beginning of the animation relative to the object's y-coordinate Note: The above two attribute values from the 0%-100% value 50% is the object's X or y-direction coordinates on the midpoint position Long value: Duration property for animated continuous Time in milliseconds--></set>
2. After the XML resource file is created, the next step is to invoke these resource files. Take Alpha as an example and create a alphaactivity first.
Put a picture of the background in Activity_alpha.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:background= "@drawable/test_bg" tools:context= ". Alphaactivity "></RelativeLayout>
Alphaactivity.java
Package Com.example.animation_demo; Import Android.os.bundle;import Android.app.activity;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.animationutils;import Android.widget.Toast; public class Alphaactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); View View=view.inflate (this, r.layout.activity_alpha, null); Setcontentview (view); Use the static method of the Animationutils class Loadanimation () to load the animation XML file in XML Animation animation=animationutils.loadanimation (this, R.anim.alpha); View.startanimation (animation); Animation.setanimationlistener (New Animationlistener () {@Override public void Onanimationstart (Anim ation arg0) {}//used @Override public void onanimationrepeat (Animation arg0) {} at the beginning of the animation Use @Overr when animations are repeatedIDE public void Onanimationend (Animation arg0) {toast.maketext (alphaactivity.this, "Use at end of animation", Toast.length_short). Show (); } }); }}
This is an animation of the entire layout file, and it allows any UI element to invoke the Staranimation method.
For example: Textmsg.startanimation (animation); The following code animation is not required to populate the specified XML.
View View=view.inflate (this, r.layout.activity_alpha, null); Setcontentview (view);
Direct Setcontentview (R.layout.activity_alpha); It's OK. Stroke
I just used it today and posted the picture later.
Attach some of the basic XML effects found:
Fade in: Fade in
Alpha is the gradient transparency effect, with values from 0 to 1
Fade_in.xml <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "/http Schemas.android.com/apk/res/android " android:fillafter=" true "> <alpha android:duration=" 1000 " android:fromalpha=" 0.0 " android:interpolator=" @android: Anim/accelerate_interpolator " android: Toalpha= "1.0"/>
Fade out: Fade out
With fade in just the opposite, the value from 1 to 0
Fade_out.xml <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "/http Schemas.android.com/apk/res/android " android:fillafter=" true "> <alpha android:duration=" 1000 " android:fromalpha=" 1.0 " android:interpolator=" @android: Anim/accelerate_interpolator " android: Toalpha= "0.0"/> </set>
Cross fading: Crossfade and Fade in
Simultaneous use of fade in and fade out can achieve crossover effect
public class Crossfadeactivity extends Activity implements Animationlistener {TextView txtMessage1, txtMessage2; Button btnstart; Animation Animfadein, Animfadeout; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub s Uper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_crossfade); TxtMessage1 = (TextView) Findviewbyid (r.id.txtmessage1); TxtMessage2 = (TextView) Findviewbyid (r.id.txtmessage2); btnstart = (Button) Findviewbyid (R.id.btnstart); Load Animations Animfadein = Animationutils.loadanimation (Getapplicationcontext (), R.ANIM.FAD E_IN); Animfadeout = Animationutils.loadanimation (Getapplicationcontext (), r.anim.fade_out); Set Animation listeners Animfadein.setanimationlistener (this); Animfadeout.setanimationlistener (this); button click EVent Btnstart.setonclicklistener (New View.onclicklistener () {@Override public void O Nclick (View v) {txtmessage2.setvisibility (view.visible); Txtmessage2.startanimation (Animfadein); Txtmessage1.startanimation (animfadeout); } }); } @Override public void Onanimationend (Animation Animation) {if (Animation = = animfadeout) { Txtmessage1.setvisibility (View.gone); } if (animation = = Animfadein) {txtmessage2.setvisibility (view.visible); }} @Override public void Onanimationrepeat (Animation Animation) {//TODO auto-generated method stub} @Override public void Onanimationstart (Animation Animation) {//TODO auto-generated met Hod stub}}
BLink: Looming
Blink.xml <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/ Apk/res/android "> <alpha android:fromalpha=" 0.0 " android:toalpha=" 1.0 " android:interpolator=" @android: Anim/accelerate_interpolator " android:duration=" " android:repeatmode=" reverse " Android : repeatcount= "Infinite"/> </set>
Zoom in : Enlarge
Zoom_in.xml <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "/http Schemas.android.com/apk/res/android " android:fillafter=" true "> <scale xmlns:android="/http/ Schemas.android.com/apk/res/android " android:duration=" " android:fromxscale=" 1 " android: fromyscale= "1" android:pivotx= "50%" android:pivoty= "50%" android:toxscale= "3" android: toyscale= "3" > </scale> </set>
Zoom Out: Shrink
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/res/ Android " android:fillafter=" true "> <scale xmlns:android=" http://schemas.android.com/apk/res/ Android " android:duration=" " android:fromxscale=" 1.0 " android:fromyscale=" 1.0 " android: pivotx= "50%" android:pivoty= "50%" android:toxscale= "0.5" android:toyscale= "0.5" > </ Scale> </set>
Rotate: Rotate
Rotate.xml<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/ Res/android "> <rotate android:fromdegrees=" 0 " android:todegrees=" android:pivotx= " 50%" android:pivoty= "50%" android:duration= " android:repeatmode=" restart " android:repeatcount=" "Infinite" android:interpolator= "@android: Anim/cycle_interpolator"/> </set>
It's so long ... Now that you have seen this, say a few more words.
Properties of the root node <set>:
Name |
Property |
Note |
Android:shareinterpolator |
Whether to share the insertion device |
When sharing, four child nodes are used with one insert |
Android:interpolator |
Specifies the insertion of an animation |
Using System resources |
Android:fillenabled |
|
When set to True, Fillafter and fill, Befroe will be true, ignoring Fillbefore and fillafter two properties |
Android:fillafter |
Whether the animation conversion is applied after the animation is finished |
Boolean |
Android:fillbefore |
Whether the animation conversion is applied before the animation starts |
Boolean |
Android:repeatmode |
Repeating mode |
"Restart" or "reverse" |
Android:repeatcount |
Number of repetitions |
Integer |
Android:duration |
Animation duration |
Integer |
Android:startoffset |
Animation time interval |
Long |
Android:zadjustment |
Defines the transformation of the animation Z order |
[Normal] or [top] or [bottom] |
Android:detachwallpaper |
|
Boolean |
XML use of Android animation animations