Full Solution for Android animation

Source: Internet
Author: User

In the development of Android often encounter animation, see other applications have a cool application, always want to how to achieve, but each time is to find that feeling is to know how to do, actually do it or not to feel, the reason is still

Android Animation knowledge is not comprehensive, these days using free time to study the next Android animation knowledge, as a learning diary, we have some reference.

Android mainly divided into three types of animation: Tween Animation, Frame Animation, property Animation.

Which Tween Animation, frame Animation is in the Android is before the Android3.0 of the animation technology, and later due to the high demand for animation, Tween Animation, frame Animation has not satisfied the application for the animation effect of the demand, so after Android3.0, Google added a new animation property Animation. Here are three kinds of animations described individually.

First, Tween Animation

Also known as tweened Animation, which is animated by panning, rotating, zooming, and modifying transparency, the principle is to give two keyframes, with some algorithms to ramp a given attribute value between two keyframes within a given time. Here we only care about the use of animation, do not pay attention to its implementation of the code. Tween animation is based on the animation class extension and has the following Tween animation classes: Translateanimation (panning), alphaanimation (transparency), scaleanimation (shrinking Rotateanimation (rotation).

Translateanimation use the following methods:

Private translateanimation mtranslateanimation; // The four parameter meanings are the current view x start coordinates, x end coordinates, y start coordinates, y end coordinates, respectively. New Translateanimation (0, 0, 0); // Animation Duration mtranslateanimation.setduration (+); // number of repetitions mtranslateanimation.setrepeatcount (1); // animation execution Mode mtranslateanimation.setrepeatmode (animation.reverse);

The code above means starting from the start of the view, keeping the y-axis unchanged, panning 200 pixels along the X, panning for 2 seconds, repeating once, and the second execution in the exact opposite way of the first execution, such as:

These are the ways in which Java code is implemented and can be implemented using XML, as follows:

/res/anim/translate.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <TranslateAndroid:fromxdelta= "0"Android:toxdelta= "$"Android:fromydelta= "0"Android:toydelta= "0"android:duration= "$"Android:repeatcount= "1"Android:repeatmode= "Reverse"Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"/></Set>

Fromxdelta:view X-Axis start

Toxdelta:view x-Axis end point

Fromydelta:view y-Axis starting point

Toydelta:view y-Axis end point

Duration: Duration of animation

RepeatCount: Animation execution times

Repeatmode: Animation execution mode

Interpolator: Interpolator, which can be understood as the changing law of the View animation value

Called in Java:

Private= animationutils.loadanimation (this, r.anim.translate); Mtranslateiv.startanimation (mtranslateanimation);

There is no difference between using Java or XML to animate, which depends entirely on your own programming habits, but it is obvious that XML is more dominant in terms of extensibility alone.

Alphaanimation use the following methods:

Private alphaanimation malphaanimation; // opacity from 100% to 0 New Alphaanimation (1, 0); malphaanimation.setduration (+); Malphaanimation.setrepeatcount (1); Malphaanimation.setrepeatmode (animation.reverse);

The above code means that in 2 seconds, the view's non-transparent name from 1 to 0, a total of 2 times, the first and second execution of the animation process is the opposite, the effect such as:

XML Implementation method can refer to Translateanimation

Scaleanimation use the following methods:

New Scaleanimation (1, 2, 1, 2); Mscaleanimation.setduration (+); Mscaleanimation.setrepeatcount (1 ); Mscaleanimation.setrepeatmode (animation.reverse);

The four parameters in the above Scaleanimation constructor are:

FromX: start x Direction size

ToX: Final x-Direction size

FromY: start y coordinate

ToY: Final y-coordinate

The above code means that the view in 2 seconds to increase the vertical and horizontal direction of one time, the second is on the original basis to reduce one times, as follows:

XML implementation method with reference Translateanimation

Rotateanimation use the following methods:

New Rotateanimation (0,mrotateanimation.setduration);Mrotateanimation.setrepeatcount (1 ); Mrotateanimation.setrepeatmode (animation.reverse);

The above code means to rotate the view 360 degrees in 2 seconds, then rotate back, the effect is as follows:

Second, Frame Animation

The people who saw the movie may know that the film is made up of a picture, but the picture is fast and the continuous effect is seen in human vision. Frame animation is also based on this principle, to prepare several pictures in advance, in a specific order, and then use a specific animation class to play it, based on the implementation of this animation, frame Anmation also has a Chinese name: frame-wise animation.

The implementation of this animation requires the use of the Animationdrawable class, which inherits from Drawable and is used in the following way:

Privatenew  animationdrawable (); // add a picture and set the picture playback time Manimationdrawable.addframe (Getresources (). Getdrawable (R.drawable.australia),  Manimationdrawable.start (); 

The above code is to play three pictures in the specified order, at the specified time, to achieve an animated effect, such as:

It can also be implemented in XML, with the following code:

/anim/frame.xml

<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "false">    <Itemandroid:drawable= "@drawable/australia"android:duration= "$"/>    <Itemandroid:drawable= "@drawable/austria"android:duration= "$"/>    <Itemandroid:drawable= "@drawable/china"android:duration= "$"/></animation-list>

Called in XML:

< ImageView         Android:id = "@+id/frame_iv"         android:layout_width= "Wrap_content"        android:layout_height= "Wrap _content "        android:background=" @anim/frame "/>

Called in Java:

Private ImageView Mframeiv; Private  == (animationdrawable) mframeiv.getbackground (); Manimationdrawable.start ();

About frame tween knowledge is relatively simple, familiar with animationdrawable, the following describes the most important property Animation this chapter.

Third, Property Animation

Chinese also called property animation, such as its name, can change the properties of the object, notice is the object, not the view, and is an arbitrary object, as long as the object meets certain conditions, the above tween animation only can operate the view, for the object outside the view is powerless, And it does not change the properties of the view, only through the parent view of its repainting to achieve animation effect, and frame animation animation is too simple. With the development of our Android application, can find tween Animation, Frame Animation sometimes do not achieve the desired effect, Google is aware of this, so after the Android3.0 added a powerful property animation.

Property animation has two classes to use: Valueanimator, Objectanimator. These two classes are directly or indirectly inherited from the animator

Objectanimator is used as follows:

Privateobjectanimator Mobjectanimator;//setting opacity varies from 1 to 0Mobjectanimator =objectanimator. Offloat (Mobjectanimatoriv,"Alpha", 1, 0). Setduration (1000);//set the interpolator to accelerate and decelerate firstMobjectanimator.setinterpolator (Newacceleratedecelerateinterpolator ());//Animation repeats onceMobjectanimator.setrepeatcount (1);//Set Execution ModeMobjectanimator.setrepeatmode (Valueanimator.reverse); Mobjectanimator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {//Gets the current animation value        floatCval =(Float) animation.getanimatedvalue (); //Scales the current view based on the animated valueMobjectanimatoriv.setscalex (Cval);    Mobjectanimatoriv.setscaley (Cval); }});

The above code means to change the current view opacity from 1 to 0, and the alpha in the first code, Offloat (Mobjectanimatoriv, "Alpha", 1, 0), represents opacity, and someone might say I can write something else? Of course, such as written offloat (Mobjectanimatoriv, "TMG", 1, 0), but you will find that the animation will not be effective after execution, because the premise of writing this string is the current view or the current view of the parent class implemented the string get, set method , Getalpha, Setalpha, when the animation is executed, the animation class will change the view's Alpha property by calling Setalpha to achieve the desired animation effect. So how does this alpha go from 1 to 0? There is a typeevaluator in property animation, which means that the property value of the current time is calculated based on the start of the property, the end value, and the factor calculated by timeinterpolation, which is how the alpha in the previous example changed from 1 to 0. But it doesn't set typeevaluator? The reason is that property Animation

Will configure a default typeevaluator, if you want to explicitly set, there are several written typeevaluator to choose from:

Intevaluator: The type of the property value is int

Floatevaluator: Property value is of type float

Argbevaluator: The value type of the property is a hexadecimal color value

If you do not have the typeevaluator you need, you can also choose to inherit typeevaluator to customize a similar typeevaluator

From the above code we can also see:

float cval = (float) animation.getanimatedvalue (); Mobjectanimatoriv.setscalex (Cval); Mobjectanimatoriv.setscaley (cval);

where Animation.getanimatedvalue (); Is the value of the current property of the view, of course, according to the previous setting, the value of this property is actually 1 to 0, the following two lines of code is each moment different attribute values to zoom to the view, to change the transparency at the same time ,

The effect of changing the view size is as follows:

Similarly, property animation can also be implemented through XML, similar to the above two, no longer introduced, readers can be shared from the source to see

Valueanimator is used in a similar way to Objectanimator, the only difference being that Valueanimator cannot set properties directly, that is, similar

Mobjectanimator = objectanimator                "Alpha", 1, 0)                . Setduration (1000);

It can only do this:

Mvalueanimator = valueanimator.offloat (1, 0); mvalueanimator.settarget (MVALUEANIMATORIV);

Animation effects can be implemented in listener events, such as:

Mvalueanimator.addupdatelistener (new  Valueanimator.animatorupdatelistener () {    @Override     publicvoid  onanimationupdate (valueanimator animation) {        float Animatedvalue = (float) animation.getanimatedvalue ();        Mvalueanimatoriv.setalpha (animatedvalue);        Mvalueanimatoriv.setscalex (animatedvalue);        Mvalueanimatoriv.setscaley (Animatedvalue);    }});

Talk about Keyframe,keyframe is a time/value pair, through which you can define a specific state at a specific time,

That is, keyframes, and you can define different interpolator between the two keyframe, just like the stitching of multiple animations, the end point of the first animation is the beginning of the second animation. Keyframe is an abstract class, to get the proper keyframe through Ofint (), Offloat (), and so on Propertyvaluesholder.ofkeyframe

Get the Propertyvaluesholder object, as in the following example:

Keyframe kf0 = keyframe.ofint (0, 400); Keyframe Kf1= Keyframe.ofint (0.25f, 200); Keyframe Kf2= Keyframe.ofint (0.5f, 400); Keyframe Kf3= Keyframe.ofint (0.75f, 100); Keyframe Kf4= Keyframe.ofint (1f, 500); Propertyvaluesholder Propertyvaluesholder= Propertyvaluesholder.ofkeyframe ("width", kf0, Kf1, Kf2, KF3, Kf4); Mkeyframesobjectanimator=Objectanimator.ofpropertyvaluesholder (MKEYFRAMESOBJECTANIMATIONBTN, Propertyvaluesholder); Mkeyframesobjectanimator.setduration (2000); Mkeyframesobjectanimator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {ANDROID.UTIL.LOG.D ("Update", (Animation.getanimatedvalue ()). ToString ()); }});

The animation effect is also added in Onanimationupdate, which is just a log

If you want to apply multiple animations, Animationset can help you, Animationset provides a mechanism to combine multiple animations into a combination, and to set the timing relationship of the animation in the group, such as simultaneous playback, sequential playback, etc.

The following code:

New Animatorset (); Manimatorset.play (Mobjectanimator). before (Mvalueanimator); Manimatorset.play (MValueAnimator). Before (mkeyframesobjectanimator);

Play is played, before is played before the current animation, there are 2 methods with, after, respectively, and the current animation play at the same time, after the current animation, the effect is as follows:

There are three animations playing in a certain order, and sometimes this class is important when we need to compare complex animation effects.

Iv. Introduction of expansion

Interpolator: Interpolator that represents the speed at which animation values change

RepeatCount: Number of times the animation repeats

Repeatmode: Animation execution mode, each time the animation is executed in the same way or in the opposite direction

Duration: Animation Execution Time

Evaluator: Calculates the current time property value based on the attribute's start, end value, and timeinterpolation calculated factor

Reference article:

Http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html

http://blog.csdn.net/lmj623565791/article/details/38092093

Source:

Https://github.com/taothreeyears/animation

Full Solution for Android animation

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.