Android three common animation sharing

Source: Internet
Author: User

Prior to Android3.0 (IE API Level11), Android only supported 2 animations: Frame Animation (frame-by-frames) and tween Animation (motion tween). After 3.0, Android supported a new animation system called: Property Animation (Attribute animation)

First, Frame Animation: (Frame-wise animation)

This is a good understanding, a frame of the playback of pictures, the use of human eye vision residue principle, to bring us the feeling of animation. It works the same way as GIF pictures and movies play.

1. It is simpler to define frame-by-frames animations, as long as all the playback frames are defined in <animation-list...> using the <item...> child elements.

(1) Android:oneshot set whether to play only once

(2) android:drawable set each frame picture

(3) Android:duration set the switch interval between pictures

2. The habit of setting animationdrawable to ImageView background

Android:[email Protected]/frame_anim

Then we can get the Animationdrawable object in Java code.

Animationdrawable Anim = (animationdrawable) imageview.getbackground ();

(Note that animationdrawable is not played by default, calling its start () method to start playback, stop stops playing)

3. The above animation files are configured through an XML file, and if you like, you can also add frames to the animation by creating the Animationdrawable object in Java code, and then by Addframe (drawable frame, int duration) method. Then start () ...

Second, Tween Animation: (Tween animation)

Motion tweens are the only "keyframes" that we need to specify starting and ending, while the other frames in the change are computed by the system without having to define the frame.

1. Android uses animation to represent abstract animations, including four seed classes: Alphaanimation (transparency animation), scaleanimation (zoom animation), translateanimation (displacement animation), Rotateanimation (transparency animation). Android allows you to create a animation class object in Java, but typically uses an animated resource file to define the animation, separating the interface from the logic

<SetAndroid:interpolator= "@android: Anim/linear_interpolator"xmlns:android= "Http://schemas.android.com/apk/res/android">    <!--define transformations for transparency -     <Alpha> </Alpha>    <!--Defining rotation Transformations -    <Rotateandroid:duration= "3000/"android:fromdegrees= "0"Android:pivotx= "50%"Android:pivoty= "50%"android:todegrees= "1800"></Rotate></Set>

(a set can define multiple animations at the same time and execute together.) )

2. Android:[email Protected]:anim/linear_interpolator controls how many frames need to be filled during the animation, simply to control the animation speed, and some places to translate "interpolation." Interpolator has several implementation classes: Linearinterpolator, Accelerateinterpolator, Acceleratedecelerateinterpolator, Cycleinterpolator, Decelerateinterpolator, specific use can refer to the official API Demo.

3. After defining the Anim file, we can load them through the Animationutils tool class and return a animation after the load is successful. You can then start the animation by using the view's Startanimation (Anim).

Animation anim = animationutils.loadanimation (this, R.anim.anim); // sets the end state to remain after the animation is finished anim.setfillafter (true); // Setting the interpolation effect Anim.setinterpolator (interpolator); // perform an animated view of the view . Startanimation (ANIM);
Third, Property Animation: (attribute animation)

Property animation, this is introduced in Android 3.0, it can directly change the properties of our objects. In the tween animation mentioned above, just changing the view's painting effect and the real properties of the view are unchanged. Suppose you use a tween animation to move a button from the left to the right, no matter how you click the button after the move, he doesn't respond. And it reacts when you click the position of the button before moving, because the position property of the button has changed. Property animation can directly change the attribute value of the View object, which allows us to do less work and improve the efficiency and readability of the code.

(1) Valueanimator: Contains all the core functions of the property animation animation, such as animation time, start, end attribute value, corresponding time attribute value calculation method, etc. There are two steps to applying valueanimator

1 calculates the property value.

2 performs a corresponding action based on the value of the property, such as changing an object's properties.

Our Lord is the second step, need to implement Valueanimator.onupdatelistener interface, this interface has only one function onanimationupdate (), will change the view object properties of things in the interface do.

Animation.addupdatelistener (new  Animatorupdatelistener () {    @Override    public void onanimationupdate (valueanimator animation) {        //doyourwork    }});

(2) Objectanimator: Inherit from Valueanimator, to specify an object and a property of the object, when the property value calculation is completed automatically set to the corresponding property of the object, that is, complete the properties animation all two steps. In practice, Objectanimator is used to change a certain property of an object, but with objectanimator there are certain limitations, if you want to use Objectanimator, the following conditions should be met:

1. The object should have a setter function: Set (Camel name method)

2 As the following 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 ... parameter is set to a value, then it is assumed to be the destination value, the value of the property changes from 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

3 If there is a getter method, it should return a value type that corresponds to the parameter type of the corresponding setter method.

Objectanimator oa=objectanimator.offloat (TV, Alpha, 0f, 1f); Oa.setduration (+);Oa.start () ;

If the above conditions are not met, we can only use Valueanimator to create animations.

(3) Animator.animatorlistener: You can set the Animator for animation monitoring, you need to rewrite the following four methods.

Onanimationstart () Onanimationend () onanimationrepeat () Onanimationcancel ()

Here we can also implement Animatorlisteneradapter, his advantage is that you can only define the event you want to listen to without implementing each function but only define an empty function body. As follows:

Anim.addlistener (new  animatorlisteneradapter () {    publicvoid on Animationend (Animator animation) {        //doyourwork    }});

(4) Animationset: Multiple animations can be combined to work together

New Animatorset (); Bouncer.play (ANIM1). before (anim2); Bouncer.play (ANIM2). with (ANIM3), Bouncer.play (ANIM2). with ( ANIM4) Bouncer.play (ANIM5). After (amin2); Animatorset.start ();

The above code means: Play anim1 first, play anim2,anim3,anim4 at the same time, and finally play ANIM5.

(5) Timeinterplator: Similar to the interpolator in tween. Has the following several

Accelerateinterpolator acceleration, slow intermediate acceleration at start

Decelerateinterpolator slow down, start fast and slow down.

Acceleratedecelerateinterolator acceleration and deceleration, slow start, middle acceleration

Anticipateinterpolator reverse, first change to the opposite direction and then accelerate the playback

Anticipateovershootinterpolator reverse add rebound, first change in the opposite direction, and then accelerate the play, will exceed the target value and then slowly move to the destination value

Bounceinterpolator jump, fast to the destination value will jump, such as the destination value of 100, followed by the value may be 85,77,70,80,90,100

Cycleiinterpolator cycle, animation cycle a certain number of times, the value is changed to a sine function: Math.sin (2 * mcycles * Math.PI * input)

Linearinterpolator linear, linear uniform change

Overshottinterpolator rebound, finally exceeding the target value and then slowly changing to the destination value

Timeinterpolator an interface that allows you to customize the Interpolator, the above several are implemented by this interface

(6) Keyframes: Allows us to define keyframes other than start and end. Keyframe is an abstract class, to get the proper keyframe through Ofint (), Offloat (), Ofobject (), The Propertyvaluesholder object is then obtained through Propertyvaluesholder.ofkeyframe, as follows:

Keyframe kf0 = keyframe.ofint (0,keyframe.ofint = Keyframe.ofint (0.25f, 0.5f Keyframe.ofint (0.75f,= Keyframe.ofint (1f,= = Objectanimator.ofpropertyvaluesholder (BTN, pvhrotation);

The above code means: Set the Width property value of the Btn object to make it: Start width=400, the animation starts at 1/4 width=200, the animation starts at 1/2 width=400, the animation starts at 3/4 width=100, the animation ends width=500.

(7) Viewpropertyanimator: It is highly recommended to change multiple properties on a view at the same time. This class optimizes multi-attribute animations, merging some invalidate () to reduce the refresh view. And it's easy to use, but requires API level 12, which is Android 3.1 or more. Only one line of code is needed to move horizontally and vertically

Myview.animate (). Translationx (50f). Translationy (100f);

(8) Some properties that often need to be changed:

Translationx,translationy:view offset relative to the original position

Rotation,rotationx,rotationy: Swivel, rotation for 2D rotation angle, 3D for the latter two

Scalex,scaley: Zoom ratio

The final coordinate of the X,y:view is the left,top position of the view plus the Translationx,translationy

Alpha: Transparency

Iv. Finally, summarize the pros and cons of these three animations:

(1) frame Animation (frame animation) is mainly used to play a frame frame prepared pictures, similar to GIF pictures, the advantage is simple and convenient to use, the disadvantage is the need to prepare each frame of the picture beforehand;

(2) Tween Animation (motion Tween) only need to define the start and end keyframes, and the change in the middle frame by the system, the advantage is not to prepare each frame, the disadvantage is to change the object drawing only, without changing the view itself properties. So if you change the position of the button, you still need to click the original button location to be effective.

(3) Property Animation (attribute animation) is a 3.0 animation, the advantage is simple to use, reduce the complexity of the implementation, directly change the properties of the object, can be applied to almost any object but not the view class, The disadvantage is that more than 3.0 of API support is required, the limit is large! But currently there is an open source library, can provide low-version support!

Android three common animation sharing (GO)

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.