Android Development Art Study notes (vii) _android

Source: Internet
Author: User
Tags repetition

Seventh chapter Android Animation in-depth analysis

Android animation is divided into three kinds: view animation, frame animation, property animation. The frame animation belongs to the view animation.

7.1 View Animation

The view animation has four animation effects: Pan (Translate), Zoom (Scale), rotate (Rotate), Transparency (Alpha).

The type of 7.1.1 view animation

Save path for view animation: Res/anim/filename.xml. The XML format syntax is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "Http://schemas.android.com/apk/res/android" Android:interpolator= "@anim/interpolator_resource" android:shareinterpolator= "True|false" > <alpha android: Fromalpha= "Float" <!--transparency starting value--> android:toalpha= "float"/><!--transparency End value--> <scale Android:fromxscale = "Float" <!--horizontal Scaling starting value--> android:toxscale= "float" <!--horizontal Scaling End value--> android:fromyscale= "float" <!-- Vertical scaling Starting value--> android:toyscale= "float" <!--vertical Scaling end--> android:pivotx= "float" <!--scaling axis point x coordinates--> Android :p ivoty= "float"/><!--zoom Axis point y coordinate--> <translate android:fromxdelta= "float" <!--x start position--> android: Fromydelta= the start position of "float" <!--y--> android:toxdelta= the end position of "float" <!--x--> android:toydelta= "float"/> <!--y End position--> <rotate android:fromdegrees= "float" <!--starting angle--> android:todegrees= "float" <!-- End angle--> android:pivotx= "float" <!--axis point x coordinate--> android:pivoty= "float"/><!--rotary Axis Point y-coordinate--> <set> ... </set> </set>  

The <set> label represents an animation set, corresponding to the Animationset class, and can also be nested inside other animation collections.

The android:interpolator interpolation sets the speed at which the animation has been run, and the default is the accelerated deceleration interpolation.

Whether the animation in the Android:shareinterpolator collection shares the same interpolation as the collection.

Android:duration Animation duration

Android:fillafter whether the view stays at the end after the animation finishes.

The axis point in <scale> <rotate> is the central point of view by default (it appears to be the upper left corner).

To load the animation defined in the XML in code:

Animation Animation = Animationutils.loadanimation (this, r.anim.view1);

Use the animation Setanimationlistener method to add a listener to the view animation.

7.1.2 Custom View Animation

Principle: Inherits animation this abstract class, then rewrites its initialize and the Applytransformation method, does the initialization in the initialize, carries on the corresponding matrix transformation in the applytransformation. Camera is usually used to simplify the process of matrix transformation. The process of customizing the view animation is primarily the process of matrix transformation. Examples can refer to the rotate3danimation effect in Apidemo.

7.1.3 Frame Animation

A frame animation is actually a set of predefined pictures that play sequentially. Use frame animation through animationdrawable. The XML format syntax is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <animation-list xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:oneshot=" True|false >
<item android:drawable= "@mipmap/ic_launcher" Android: Duration= "/>"
<item android:drawable= "@mipmap/ic_launcher" android:duration= ""/> <item
android:drawable= "@mipmap/ic_launcher" android:duration= "/>"

Use it directly as the background of the view and play it through drawable.

Button.setbackgroundresource (R.DRAWABLE.VIEW2);
Animationdrawable drawable= (animationdrawable) button.getbackground ();

Special use scenes for 7.2 view animations

7.2.1 Layoutanimation

Acts on ViewGroup, assigning an animation to the ViewGroup, so that when its child elements appear, it will have this animation effect.

Steps:

(1) Define Layoutanimation

<?xml version= "1.0" encoding= "Utf-8"?> <layoutanimation xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:delay=" 0.5 "
android:animationorder=" normal "

Android:delay the time delay when the child element starts the animation, such as the child element setting duration to 200ms, 0.5 means that each child element needs to be delayed 100ms to start the animation, that is, the first to play after 100ms, the second will have to go through 200ms to play, the third will have to go through 300ms to play, and so on.

Android:animationorder the animation order of the child elements, normal (sequential display), reverse (reverse display), random (random).

(2) Specifying animations for child elements

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android "
android:duration=" android:interpolator= "
@android: Anim/accelerate_interpolator"
Android:shareinterpolator= "true" >
<alpha
android:fromalpha= "0.0" android:toalpha=
"1.0"/>
<translate
android:fromxdelta= "0"
android:toydelta= "/>"

(3) Specify Android:layoutanimation property for ViewGroup

 
 

Or you can specify animations for ViewGroup by Layoutanimationcontroller.

Animation Animation = Animationutils.loadanimation (this, r.anim.anim_item);

Layoutanimationcontroller controller=new Layoutanimationcontroller (animation);
Controller.setdelay (0.5f);
Controller.setorder (Layoutanimationcontroller.order_normal);

The transition effect of 7.2.2 activity

Using the overridependingtransition (int enteranim, int exitanim) method, note that this method must be invoked after startactivity or finish to take effect.

Animation when Enteranim--activity is opened

Animation when Exitanim--activity exits

Start activity:

Intent Intent =new Intent (this,testactivity.class);
StartActivity (Intent);

Exit activity:

@Override public
void Finish () {
super.finish ();
Overridependingtransition (R.anim.enter_anim,r.anim.exit_anim);

7.3 Property Animation

API level must be greater than or equal to 11, storage path is res/animator/directory, property animation can animate any object, common Valueanimator, Objectanimator.

7.3.1 using attribute Animation

Property animation default time interval is 300ms, the default frame rate is 10ms/frame, you can achieve the object from one property value to another property value in a time interval. If you want the property animation to be compatible with the low version system, you need to use nineoldandroids this open source animation library.

Illustrates how to use attribute animation:

(1) Change the Translationy property of an object so that it moves up a distance along the Y axis: its height.
Objectanimator.offloat (MyObject, "Translationy",-myobject.getheight ());

(2) Change the background color of a view, let its background color in 3 seconds with 0xffff8080 to 0xff8080ff gradient, animation infinite loop and add reverse effect.

Valueanimator Coloranim=objectanimator.ofint (This, "BackgroundColor", 0xffff8080,0xff8080ff);
Coloranim.setduration (3000);
Coloranim.setevaluator (New Argbevaluator ());
Coloranim.setrepeatcount (valueanimator.infinite);
Coloranim.setrepeatmode (Valueanimator.reverse);

The XML format syntax for the property animation is as follows:

 <?xml version= "1.0" encoding= "Utf-8"?> <set "xmlns:android=" Schemas.android.com/apk/res/android "android:ordering=" Sequentially|together ><!--together: Child animations play at the same time. Sequentially: Automation plays--> <objectanimator android:duration= "int" <!--animation length--> android:propertyname= in sequence "String" <!--property name--> android:repeatcount= "int" <!--repeat number--> android:repeatmode= "Restart|reverse" <!--
Repeat mode--> android:startoffset= "int" <!--delay time--> android:valuefrom= "Float|int|color" <!--property Start value--> Android:valueto= "Float|int|color" <!--property End value--> android:valuetype= "Colortype|inttype|floattype|pathtype"/ ><!--Property Type--> <animator android:duration= "int" android:repeatcount= "int" android:repeatmode= "restart| Reverse "android:startoffset=" int "android:valuefrom=" Float|int|color "android:valueto=" Float|int|color "Android: Valuetype= "Colortype|inttype|floattype|pathtype"/> </set> 

Android:repeatcount animation of the number of cycles, the default is 0,-1 infinite loop;

Android:repeatmode repeat: continuous repetition; reverse: Reverse repetition (after the first playback, the second time played backwards, the third time in the beginning of the broadcast so repeated).

When you define a property animation in XML, you can use it in Java code.

Animatorset set= (Animatorset) animatorinflater.loadanimator (context,r.anim.property_animator);
Set.settarget (button);

In actual development, it is recommended that you use code to implement property animations, rather than using XML.

7.3.2 Understanding interpolation and Estimator

Interpolation: Calculates the percentage of the current property value change based on the percentage of elapsed time;

Estimator: Calculates the changed property value based on the percentage of the current property.

7.3.3 Property Animation Listener

Animatorlistener listening to the beginning, end, cancellation, repeat playback of the animation;

Animatorupdatelistener listens to the entire animation process.

7.3.4 to animate any property

Animate the attribute ABC of object, and if you want the animation to take effect, you need to meet two conditions:

(1) object must provide Setabc method, if the animation does not pass the initial value, then still have to provide getabc method, otherwise the program directly crash;

(2) The change of object SETABC to attribute ABC must be reflected in some way, such as UI changes.

How to animate a raw object See Method 2 of p285 page;

How the 7.3.5 Property animation works

A property animation requires an object that is animated to provide a set method for that property, and the property animation takes the set method off multiple times, depending on the initial and final values of the property that you pass, as well as the effect of the animation. The values passed to the Set method each time are different, and it is true that over time the values passed are getting closer to the final value. If the animation does not pass the initial value, then also provide the Get method, because the system is going to obtain the initial value of the property.

7.4 Considerations for using animations

Note that the view animation is an animation of the view's image, and does not really change the view's state, so sometimes the view cannot be hidden when the animation is done, namely Setvisibility (View.gone) is invalid, this time just call View.clearanimation () clear the View animation to solve this problem.

When view is moved (translated), a Click event cannot be triggered on a previous system on Android3.0, whether it is a view animation or a property animation, and the old location can still trigger a click event. Although the view is already visually absent, the click event in the original location continues to take effect after the view is moved back to its original location. Starting at 3.0, the Click event Trigger position of the property animation is the position after the move, but the view animation is still in its original position.

About the art of Android Exploration Learning notes (vii) to introduce so many, follow-up will continue to update the Android art exploration related knowledge, please continue to pay attention to this site, thank you.

Related Article

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.