The Android platform provides a complete set of animation frames, with two animations tween Animation (motion tween) and frame Animation (frame animation) before Android3.0.
Corresponds to the view Animation and drawable Animation in the SDK.
After Android3.0, a new animation property Animation (attribute animation) was added.
One: Motion Tweens ( res/anim/
)
Tween Animation can implement a series of transformations on the view, giving two keyframes, and using some algorithms to ramp a given attribute value between two keyframes in a given time. It can be defined by code and XML , and is defined in code by: Alphaanimation ....
Characteristics:
Tween animation can only be applied to view objects, and only a subset of properties are supported. It does not change the value of the property, but changes the location of the view object's drawing. For example, a button after the animation, no longer the original position, but the trigger click event is still the original coordinates.
XML must have a heel node, which can be a gradient <alpha>, a telescopic <scale>, a moving <translate>, a rotation <rotate>, or a <set>. <set> is an animated set that can contain one or more of the preceding.
<!-- -
<SetAndroid:shareinterpolator= "false"> < ScaleAndroid:interpolator= "@android: Anim/accelerate_decelerate_interpolator"Android:fromxscale= "1.0"Android:toxscale= "1.4"Android:fromyscale= "1.0"Android:toyscale= "0.6"Android:pivotx= "50%"Android:pivoty= "50%"Android:fillafter= "false"android:duration= "The " /> <SetAndroid:interpolator= "@android: Anim/decelerate_interpolator"> < ScaleAndroid:fromxscale= "1.4"Android:toxscale= "0.0"Android:fromyscale= "0.6"Android:toyscale= "0.0"Android:pivotx= "50%"Android:pivoty= "50%"Android:startoffset= "The "android:duration= "The "Android:fillbefore= "false" /> <Rotateandroid:fromdegrees= "0"android:todegrees= " -45"Android:toyscale= "0.0"Android:pivotx= "50%"Android:pivoty= "50%"Android:startoffset= "The "android:duration= "The " /> </Set></Set>
Used in code:
ImageView Spaceshipimage == animationutils.loadanimation (this, r.anim.hyperspace_jump); Spaceshipimage.startanimation (hyperspacejumpanimation);
Three step 1 for defining animations. Create an Animated object 2. Set the duration, 3. Turn on animations,
Interpolators Interpolator
Can let the animation according to a certain frequency movement, to achieve acceleration, deceleration, repetition, rebound and other effects.
The XML file is stored in:res/anim 下,并定义android:interpolator="@android:anim/.."来使用
Two: Frame animation (res/drawable/)
Frame Animation is a series of images that are displayed sequentially to simulate the effects of animations. The code is defined by animationdrawable .
<animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "true"> <Itemandroid:drawable= "@drawable/rocket_thrust1"android:duration= "$" /> <Itemandroid:drawable= "@drawable/rocket_thrust2"android:duration= "$" /> <Itemandroid:drawable= "@drawable/rocket_thrust3"android:duration= "$" /></animation-list>
You must use <animation-list> as the root element, <item> to represent the picture to rotate, and the duration property to indicate when the items are displayed.
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); ImageView=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Imageview.setbackgroundresource (R.drawable.drawable_anim); Anim=(animationdrawable) imageview.getbackground (); } Public Booleanontouchevent (Motionevent event) {if(event.getaction () = =Motionevent.action_down) {anim.stop (); Anim.start (); return true; } return Super. Ontouchevent (event); }
Precautions:
- To call ImageView's Setbackgroundresource method in code, set its Src property directly in the XML layout file when the animation is triggered by the FC
- Stop () before the animation start (), or it will stop at the last frame after the first animation, so the animation will only fire once
- The last point is mentioned in the SDK, do not call start in OnCreate, because animationdrawable is not fully associated with the window, if you want the interface to start animation, you can be in the onwindowfoucschanged () Call Start () in the
Three: Property animation
Property animation changes the actual properties of the object, such as the zoom of the button, and the position and size of the button are changed. It can be applied not only to view, but also to almost any object at any time (even without draws to the screen). Property animation just represents a change in a value over time, and it's up to you to decide what to do when it changes.
Property animation allows us to define properties:
- Duration: Duration, default length is.
- Time interpolation: How property values are calculated, such as fast and slow
- Repeat count and behavior: number of repetitions and ways
- Animator sets: A collection of animations that hold several animations that can be played simultaneously or set different offsets
- Frame Refresh delay: How much time is refreshed, default is ten MS, affected by system process scheduling and hardware
A: valueanimator : Contains all the core functions of the property animation animation, such as animation time, start, end attribute value, the corresponding time attribute value calculation method and so on.
The application Valueanimator has two steps to gather:
- Calculate attribute values
- Performs a corresponding action based on the value of the property, such as altering an object's properties.
The first step valuanimiator is complete, we only need to implement the Valueanimator.onupdatelistener interface
valueanimator animation =< Span style= "COLOR: #000000" > Valueanimator.offloat (0f, 1f); Animation.setduration ( 1000 new Animatorupdatelistener () {@Override public void Onanimationupdate (Valueanimator animation) {TEXTVI EW TV = new TextView (Getapplication ()); Tv.settranslationx ((Float) animation.getanimatedvalue ()); }}); Animation.setinterpolator ( new cycleinterpolator (3
Two: Objectanimator: It is the subclass of Valueanimator, it completes the valueanimator in the first two steps
To get ObjectAnimator
the correct update properties, we have to do the following:
- The object should have a setter function:set<propertyname> (hump naming method), for example, if the attribute is
foo
, you must have a setFoo()
method, if not, you can only use Valueanimator
- As in the above example, a factory method such as Offloat, the first parameter is the object name, the second is the property name, the following argument is a variable parameter, if values ... If the parameter is set to only one value, then it is assumed to be the destination value, the range of the property value is the current value to the destination value, in order to obtain the current value, the object has the corresponding property of the Getter method:get<propertyname>
- If there is a getter method, it should return a value type that corresponds to the parameter type of the corresponding setter method.
tv=(TextView) Findviewbyid (r.id.textview1); btn=(Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (new Onclicklistener () {@Override publicvoid OnClick (View v) {objectanimator oa=objectanimator.offloat (TV, "Alpha", 0f, 1f); Oa.setduration (+); Oa.start (); }});
In some cases, we may need to onAnimationUpdate()
perform in callbackinvalidate() 方法来强制屏幕重绘。
三:
Animatorset Moving Album
In the animation set, we can set the timing relationship of the animation in the group, such as simultaneous playback, sequential playback, and so on.
New= objectanimator.offloat (Newball, "Alpha", 1f, 0f); Fadeanim.setduration ( New Animatorset (); Animatorset.play (Bouncer). before (Fadeanim); Animatorset.start ();
Four: Animation monitoring
Some common interfaces for animations are defined in
animator.animatorlistener
Onanimationstart () Onanimationend () onanimationrepeat () // Called when the animation is canceled, and Onanimationend () is called. Onanimationcancel ()
Attention:
By observing that we found that the Animatorlisteneradapter class implements the Animator.animatorlistener interface {The internal implementation of each method is null}, we can inherit the class {only the method that needs to be replicated} instead of implementing the interface To achieve a streamlined code operation.
Objectanimator oa=objectanimator.offloat (TV, "Alpha", 0f, 1f); oa.setduration; Oa.addlistener ( New animatorlisteneradapter () { publicvoid on animationend ( Animator animation) { log.i ("animation", "End");} ); o A.start ();
Android Animation category