First, the use of animationdrawable
See: The use of drawable class and Xmldrawable
Added: You can add a listening process to a view animation by Animation's Setanimationlistener ()
Second, custom view animation (slightly, after the empty fill up) three, frame animation
Definition: Play a predefined set of pictures in sequence.
Use: ①, define <animation-list> tag ② in res/drawable/, start using in Java code
< animation-list > < item android:drawable = "@mipmap/img_1" = " /> Span style= "color: #0000ff;" >< item android:drawable Span style= "color: #0000ff;" >= "@mipmap/img_2" android:duration = " " /> </ animation-list >
Frame_animation.xml
Button btn = (button) Findviewbyid (R.ID.MAIN_BTN); animationdrawable animation = (animationdrawable) btn.getbackground ();//Start animation Animation.start ()
Mainactivity.javaIv. Special use of view animations
(a), layoutanimation
Function: Acts on the ViewGroup, when its child elements appear, will have this animation effect (commonly used in the ListView)
Use: ①, creating Layoutanimation② in Res/anim, animating ③ in Res/anim, using in Layout
< layoutaniamtion = "0.5" = "normal" = "@anim/anim_item"/> <!--andorid:delay indicates how long the element needs to be delayed to play the animation andorid:animationorder the order in which the child elements are animated is normal: Sequential display Reverse: Reverse display random: Shuffle the entry animation andorid:aniamtion indicates the animation of the specified play--
Anim_layout
<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"> <AlphaAndroid:fromalpha= "0.5"Android:toalpha= "1"/> <TranslateAndroid:fromxdelta= "0.0"Android:toxdelta= "$"/></Set>
Anim_item
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns: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"Tools:context= "Com.maikefengchao.tabteamproject.MainActivity"> <ListViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"<!---Set the properties of the animation-> android:layoutanimation= "@anim/anim_ilayout" ></ListView></Relativelayout>
To load an animation in your code:
@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView LV=(ListView) Findviewbyid (R.ID.MAIN_LV); //Get Anim_itemAnimation Animation = animationutils.loadanimation ( This, R.anim.anim_item); //Code Creation LayoutanimationLayoutanimationcontroller Controller =NewLayoutanimationcontroller (animation); //Configuration ParametersController.setdelay (0.5f); Controller.setorder (Layoutanimationcontroller.order_normal); //ListView Load Layoutanimationlv.setlayoutanimation (Controller); }Mainactivity.java
(ii) switching effect of acitivty and fragment
Activity:
Method used: overridependingtransition (int enteranim,int exitanim).
Note: This method must be called after startactivity () or finish () to take effect
Example:
startactiivity (intent); // This is called after the call overridependingtrasition (Enteranim,exitanim); // Enteranim: Animated Exitanim when opened : animation when exiting
Mainactivity.java
Fragment:
The Setcustomanimations () method adds an animation effect to the fragment
V. Attribute animation
Definition: You can animate the properties of any object not just the view, the default interval of 300ms.
Function: Change one property value to another in one time interval
Introduction: Several commonly used animation classes Valueanimator, Objectanimator (Inherit from Valueanimator), Animatorset
Objectanimator: Changing the properties of an object
Valueanimator: Set time, repeat effects
Animatorset: A collection of objectanimator animations
Use:
In the Java code
/* Use of Objectanimator */ objectanimator.offloat (MyView,"Translationy", 0.1,100.5). Start (); // Parse: The Offloat method calls the Ofint method if the input parameter is int. Parameters: View, the property to be modified, the initial value, the conversion value. Last start () Start
Objectanimator
// Changing Properties Vlueanimator Coloranim = Objectanimator.ofint (MView, "backgroundcolor",/*White*/0x000, /* Gray */0x999); // configuration coloranim.setduration (+); Coloranim.setrepeatcount (valueanimator.infinite); Coloranim.start ( );
Valueanimator
New Animationset (); Set.playtogether ( objectanimator.offloat (MyView,"RotationX", 0,360), Objectanimator.offloat (MyView,"RotationY", 0,90), ...); Set.setduraction (5*1000). Start ();
Animatorset
In XML, under the Res/animator directory, you can define three animation classes (Java code is recommended in real-world development)
<set>--animatorset <animator>--ordering <objectanimation>--objectanimati On
Use:
Parameter resolution:
Properties in <set>: Android:ordering property has together: Sub-animation plays simultaneously sequentially: sub-animation sequential playback
Attributes in the remaining tags:
android:propertyname--represents the property name of the Action object (which property of the object is modified)
android:duration--Animation duration
The starting value of the android:valuefrom--property
The terminating value of the android:valueto--property
android:startoffest--delay time to play the animation
android:repeatcount--repeated playback (default = 0,-1 is infinite loop)
android:repeatmode--has repeat: continuous repetition reverse: reverse repetition, refers to the first play after the second play backwards, the third time this play, and so on.
android:valuetype--has inttype/floattype, similar to Offloat/ofint
Called in Java:
// Get Object Animatorset set = (Animatortset) animatorinflater.loadanimator (this, r.anim.property_animator); // on which view to load Set.settarget (Mbutton); // Start animation Set.start ();
Mainactivity.javaSix, Interpolator (Interpolator) and estimator (Evaluator)
Interpolation device
Effect: Calculates the percentage of the current property value change based on the percentage of elapsed time.
System prefabrication: Linearinterpolator (constant speed animation), Acceleratedecelerateinterpolator (two slow middle fast), decelerateinterpolator (animation getting slower)
Valuation Device:
Function: Calculates the changed attribute value based on the percentage of the current property change
System presets: Intevaluator (for integer), Floatevaluator (for floating-point), Argbevaluator (for color properties)
(b) Relationship between the two
Constant speed Animation: linear interpolation, and the overall valuation method.
Example: The x attribute of the view is transformed from the 0~40. When the default animation is 10ms/frame, when t=20, the percentage is 0.5 (that is, the interpolator is 0.5), and the result returned in the estimator is x 20
If you animate other types, you must customize the estimator and the Interpolator (see P281)
Animation of any attribute
The principle of property animation: An object that requires animation provides a get and set method for the property, and the property animation invokes the set method multiple times with the effect of the animation, based on the initial and final values of the property passed by the outside world.
So, for the animation to take effect, it must satisfy:
(1) object to have
In-depth analysis of Android animations