Android Animation (develop art to explore reading notes)

Source: Internet
Author: User

Android animation can be divided into 3 categories: View animation, frame animation, property animation 1. The view animation can be created through XML, or it can be created through the Code 1.1 create a Filename.xml file under Res/anim with XML, as shown in the following syntax
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:interpolator= "@[package:]anim/interpolator_resource"    android:shareinterpolator=["true" | "false"] >    <alpha        android:fromalpha= "float"        android:toalpha= "float"/>    <scale        android:fromxscale= "float"        android:toxscale= "float"        android:fromyscale= "float"        android:toyscale= " Float "        android:pivotx=" float "        android:pivoty=" float "/>    <translate        Android:fromxdelta = "float"        android:toxdelta= "float"        android:fromydelta= "float"        android:toydelta= "float"/>    <rotate        android:fromdegrees= "float"        android:todegrees= "float"        android:pivotx= "float"        android:pivoty= "float"/>    <set> ...    </set></set>

  

The view animation can not only write individual animations, but can also use the animation collection .<set> to represent the collection of animations (corresponding to Animationset). 1.1.1:interpolator: The difference can affect the speed of the animation. 1.1.2:shareinterpolator whether a difference is shared, if the collection does not specify a difference, the sub-animation needs to specify the difference or use the default differential. 1.1.3:scale Pivot points: the Pivotx and Pivoty pivot points are different from the effects in the middle and on the left and right 2 sides. 1.1.4:duration1.1.5:fillafter whether the animation stops at the end of the ending position. Specific examples
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:fillafter= "false"    >    <translate        android:duration= "Android:fromxdelta="        0 "        android:toxdelta=" "        android:fromydelta=" 0 "        android:toydelta="        android:interpolator= "@ Android:anim/linear_interpolator "/>    <rotate        android:duration=" "        android:fromdegrees=" 0 "        android:todegrees= "/></set>"        
How to use
Animation Animation = animationutils.loadanimation (Animationactivity.this, r.anim.filename);                Mbutton.startanimation (animation);
1.2 Creating slightly 1.3View animations using the code a.viewgroup the appearance of the sub view
<?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 "></alpha> <translate Android:fromydelta= "android:toydelta=" 0 "></translate></set><?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:animation= "@anim/anim_item" ></layoutAnimation> animation Animat        Ion = Animationutils.loadanimation (this, r.anim.anim_item);        Layoutanimationcontroller controller = new Layoutanimationcontroller (animation);        Controller.setdelay (0.5f);        Controller.setorder (Layoutanimationcontroller.order_normal); V.setlayoutanimation (ControlleR)///or directly in XML using <listview android:id= "@+id/list" android:cachecolorhint= "#00000000" Android:di Vider= "#dddbdb" android:dividerheight= "1.0px" android:background= "#fff4f7f9" android:layoutanimation=        "@anim/anim_layout" android:listselector= "@android: Color/transparent" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" ></ListView>
Delay is the delay period (if the period is 300ms delay is 0.5*300=150, the first is 150ms after the start of the animation, the second is the delay 300ms after the beginning of change, etc.)The switch between b.activity is mainly used for overridependingtransition, but this method needs to be called before and after startactivity to be valid 2. Frame animation 3. Property Animation

Property animations can animate the properties of any object, not just the view, and the end of an object from one property value to another within a certain interval of time is worth changing, but property animation starts with Api11.

Several commonly used classes are: Valueanimator.objectanimator and Animatorset where Objectanimator inherits from Valueanimator,animatorset is an animated collection. You can define a set of animations. Implementing a single property and property collection through code
    private void Startobjectanimator (View object) {Objectanimator.offloat (object, "Translationy",-object.getheigh    T ()). Start (); } private void Startcolorproperty (view view) {Valueanimator Coloranim = objectanimator.ofint (view, "Backgroundc        Olor ", 0xffff8080, 0XFF8080FF);        Coloranim.setduration (3000);        Coloranim.setevaluator (New Argbevaluator ());        Coloranim.setrepeatcount (Valueanimator.infinite);        Coloranim.setrepeatmode (Valueanimator.reverse);    Coloranim.start ();        private void Startsetproperty (view view) {Animatorset set = new Animatorset (); Set.playtogether (View, "RotationX", 0, Objectanimator.offloat), Objectanimator.offloat ( View, "RotationY", 0, Objectanimator.offloat (view, "rotation", 0, -90), Objectanimator              . Offloat (View, "Translationx", 0, +), objectanimator.offloat (view, "Translationy", 0, 90),  Objectanimator.offloat (View, "ScaleX", 1, 1.5f), objectanimator.offloat (view, "ScaleY", 0, 0.5f),        Objectanimator.offloat (view, "Alpha", 0, 0.25f, 1));    Set.setduration (5 * +). Start (); }
With XML definitions, property animations are defined under res/animator/
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:ordering= "Together" >    <objectanimator        android:propertyname= "x"        android:duration= "Android:valueto="        android:valuetype= "Inttype"        ></objectAnimator>        < Objectanimator        android:propertyname= "y"        android:duration= "android:valueto="        android: Valuetype= "Inttype" ></objectAnimator>    </set>
Load the above animation to the view
        Animatorset Animatorset = (animatorset) animatorinflater.loadanimator (this, r.animator.property_animator);        Animatorset.settarget (view);        Animatorset.start ();
Understanding the difference between the Timeinterpolator and the estimator is translated into a time-difference device, and his role is to calculate the percentage of the current attribute value change based on the percentage of elapsed times. Typeevaluator's Chinese translation is a type valuation algorithm, which is the estimator that calculates the changed attribute value based on the percentage of the current attribute change.If you need to animate other types (such as non-int,float,color), then you need to customize the type estimation algorithm.
Property Animation Considerations:
property animations require that the properties of an object have set and get (optional) methods.The Listener property animation for property animations provides two interfaces for listening to animations Animatorupdatelistener and Animatorlistener
   public static interface Animatorlistener {        default void Onanimationstart (Animator animation, Boolean isreverse) {
   onanimationstart (animation);        }        default void Onanimationend (Animator animation, Boolean isreverse) {            onanimationend (animation);        }        void Onanimationstart (Animator animation);        void Onanimationend (Animator animation);        void Onanimationcancel (Animator animation);        void Onanimationrepeat (Animator animation);    }
Listen to the animation start, end, Cancel and repeat, and the system provides animatorlisteneradapter this class, it is convenient for us to inherit the above several methods arbitrary combination.
    public static interface Animatorupdatelistener {        void onanimationupdate (valueanimator animation);    }
The whole process of listening to the animation, the animation is composed of many frames, each play a frame, onanimatorupdate will be called once. Animating an animated property on any property is based on the initial and final values passed to the property by the outside world, and by calling the Set method continuously, The values that are passed to the set method are different each time, but are getting closer to the final value. In this way, animating the ABC property of object requires the animation to be valid for 2 conditions: (1): You need to provide a setabc () method, if you do not pass the initial value when animating, It is also necessary to provide the Getabc method, because the system is going to fetch the initial value of the ABC property (if this is not satisfied, the program will be Crash directly). (2): The Setabc () method of object changes to attribute ABC must be reflected in some way, such as UI changes (if this is not satisfied, the animation is invalid but not crash).for property animations that do not provide a get and set method, the official offers 3 solutions.3 Solutions for property Animations: 1. Add the Get and set methods to your object. 2. Use a class to wrap the original object and indirectly provide it with a get and set method. 3. Adopt Valueanimator, monitor the animation process, Implement your own property changes. 1. Add a Get and set method to your object this is usually not a permission for view in the SDK. 2. Use a class to wrap the original object, providing it with the get and set methods indirectly.
    private static class Viewwrapper {        private View mtarget;        Public Viewwrapper (View target) {            this.mtarget = target;        }        public void setwidth (int width) {            mtarget.getlayoutparams (). width = width;            Mtarget.requestlayout ();        }        public int getwidth () {            return Mtarget.getlayoutparams (). width;        }    }    Use the above wrapper class    Viewwrapper wrapper = new Viewwrapper (view);    Objectanimator.ofint (Wrapper, "width", and "Setduration"). Start ();
Originally wanted to change the width of the button, the result setwidth changed with what we want is not the same thing, the button wrapped as target, The effect of animation is achieved by changing the width of its layourparams. 3. Adopt Valueanimator, monitor the animation process, realize the change of the property. Valueanimator itself is not used for any object, that is, it has no effect on using it directly, it can be used to animate a value, we animate a value, and then listen to its animation process, modifying the property values of our animations in the process.
    private void Performanimator (final View target, final int start, final int end) {        Valueanimator valueanimator = Valuea Nimator.ofint (1, +);        Valueanimator.addupdatelistener (New Valueanimator.animatorupdatelistener () {            //Valuation object, use private when convenient valuation            Intevaluator mevaluator = new Intevaluator ();            @Override public            void Onanimationupdate (Valueanimator animation) {                //get current progress                int currentvalue = (int) Animation.getanimatedvalue ();                float fraction = animation.getanimatedfraction ();                Recalculate width, then set to Layoutparams to last redraw                target.getlayoutparams (). width = mevaluator.evaluate (fraction, start, end );                Target.requestlayout ();            }        });        Valueanimator.setduration (). Start ();    }
The above is the approximate content of the Android animation, the rest as the property animation works, not described.Problems that may occur:
1.View Animation is the image of the View to do animation, sometimes the animation is completed but the View can not be hidden phenomenon, is setvisibility (View.gone) failure, this time as long as the call View.clearanimation () Clear the View animation to resolve it.2. In the process of animating, use DP as much as possible, and using PX can result in different effects on different devices.

Android Animation (develop art to explore reading notes)

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.