Home>

Chapter 7 In-Depth Analysis of Android Animation

Android animation is divided into three types:view animation, frame animation, attribute animation.Frame animation belongs to view animation.

7.1 view animation

The role of view animation is view. There are four kinds of animation effects:translate, scale, rotate, and alpha.

7.1.1 types of view animation

View animation save path: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 start value->
android:toalpha="float" /><!-Transparency end value->
<scale
android:fromxscale="float"<!-horizontal scaling start value->
android:toxscale="float"<!-horizontal scale end value->
android:fromyscale="float"<!-Starting value of vertical scaling->
android:toyscale="float"<!-vertical scale end value->
android:pivotx="float"< ;! --- scale axis point x coordinate->
android:pivoty="float" /><!-Zoom axis point y coordinate->
<translate
android:fromxdelta="float"<!-the starting position of x->
android:fromydelta="float"<!-y's starting position->
android:toxdelta="float"<!-end of x->
android:toydelta="float" /><!-y's end position->
<rotate
android:fromdegrees="float"<!-starting angle->
android:todegrees="float"<!-end angle->
android:pivotx="float"<!-x-coordinate of rotation axis point->
android:pivoty="float" /><!-Rotation axis point y coordinate->
<set>
...
</set>
</set>

<set>tags represent animation collections,Corresponding to the animationset class, other animation collections can be nested inside.

android:interpolator interpolator specifies how fast the animation will run,The default is the acceleration/deceleration interpolator.

Whether the animation in the android:shareinterpolator collection shares the same interpolator as the collection.

android:duration animation duration

android:fillafter Whether the view stays at the end position after the animation ends.

<scale> ;,<rotate>By default, the pivot point is the center point of the view (to be verified looks like the upper left corner).

Load the animation defined in the xml in the code:

animation animation=animationutils.loadanimation (this, r.anim.view1);
button.startanimation (animation);

Use animation's setanimationlistener method to add a listener to the view animation.

7.1.2 Custom view animation

Principle:inherit the abstract class animation,Then override its initialize and applytransformation methods, and do initialization in initialize.The corresponding matrix transformation can be performed in applytransformation.Camera is usually used to simplify the process of matrix transformation.The process of custom view animation is mainly the process of matrix transformation.For an example, refer to the rotate3danimation effect in Apidemo.

7.1.3 Frame Animation

Frame animation is actually a sequence of predefined pictures.Use animationdrawable to use frame animation.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="500" />
<item android:drawable="@ mipmap/ic_launcher" android:duration="500" />
<item android:drawable="@ mipmap/ic_launcher" android:duration="500" />
</animation-list>

When used directly as the background of the view and play through drawable.

button.setbackgroundresource (r.drawable.view2);
animationdrawable drawable=(animationdrawable) button.getbackground ();
drawable.start ();

7.2 Special usage scenarios for view animation

7.2.1 layoutanimation

Act on viewgroup, specify an animation for viewgroup,This will have this animation effect when its child elements come out.

step:

(1) Definition of 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:animation="@ anim/anim_item" />

android:delay child element delay time to start animation,For example, if the duration of the child element is set to 200ms, 0.5 means that each child element needs to be delayed by 100ms to start playing the animation.That is, the first one starts to play after 100ms,The second one has to start playing after 200ms,The third one has to start playing after 300ms,And so on.

android:animationorder child element animation order,normal (sequential display), reverse (reverse display), random (random).

(2) Specify animation for child elements

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

(3) Specify the android:layoutanimation attribute for the viewgroup

android:layoutanimation="@ anim/anim_layout"

Or use the layoutanimationcontroller to specify the animation for the viewgroup.

animation animation=animationutils.loadanimation (this, r.anim.anim_item);
layoutanimationcontroller controller=new layoutanimationcontroller (animation);
controller.setdelay (0.5f);
controller.setorder (layoutanimationcontroller.order_normal);
linearlayout.setlayoutanimation (controller);

7.2.2 Activity switching effect

Use the overridepending transition (int enteranim, int exitanim) method. Note that this method must be called after startactivity or finish to take effect.

enteranim-the animation when the activity is opened

exitanim-Animation when the activity exits

Start the activity:

intent intent=new intent (this, testactivity.class);
startactivity (intent);
overridependingtransition (r.anim.enter_anim, r.anim.exit_anim);

Exit activity:

@override
public void finish () {
super.finish ();
overridependingtransition (r.anim.enter_anim, r.anim.exit_anim);
}

7.3 Property Animation

The api level must be greater than or equal to 11, and the storage path is under the res/animator/directory. Property animation can animate any object.Common are valueanimator and objectanimator.

7.3.1 Using attribute animation

The default time interval of the property animation is 300ms, and the default frame rate is 10ms/frame, which can achieve the change of the object from one property value to another in a time interval.If i want attribute animation to be compatible with earlier versions of the system,Need to use nineoldandroids open source animation library.

An example of how to use property animation:

(1) Change the translationy property of an object so that it translates 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 achieve a gradient of 0xffff8080 to 0xff8080ff in 3 seconds, the animation loops indefinitely, and the reverse effect is added.

valueanimator coloranim=objectanimator.ofint (this, "backgroundcolor", 0xffff8080,0xff8080ff);
coloranim.setduration (3000);
coloranim.setevaluator (new argbevaluator ());
coloranim.setrepeatcount (valueanimator.infinite);
coloranim.setrepeatmode (valueanimator.reverse);
coloranim.start ();

The xml format syntax of the property animation is as follows:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially | together"><!-together:Sub-animations play simultaneously.
sequential:automation plays in order->
<objectanimator
android:duration="int"<!-animation duration->
android:propertyname="string"<!-property name->
android:repeatcount="int"<!-Repeats->
android:repeatmode="restart | reverse"<!-repeat mode->
android:startoffset="int"<!-delay time->
android:valuefrom="float | int | color"<!-property starting value->
android:valueto="float | int | color"<!-end of attribute value->
android:valuetype="colortype | inttype | floattype | pathtype" /><!-attribute 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 number of loops for the animation,The default is 0, -1 is an infinite loop;

android:repeatmode repeat:Repeat continuously;reverse:Repeat in reverse (after the first play,Played upside down for the second time,The third time I started the broadcast again and again).

After defining the property animation in xml, it can be used in java code

animatorset set=(animatorset) animatorinflater.loadanimator (context, r.anim.property_animator);
set.settarget (button);
set.start ();

It is recommended to use code to implement property animation in actual development.Don't use xml.

7.3.2 Understanding Interpolators and Estimators

Interpolator:calculates the percentage change in the current attribute value based on the percentage of time elapsed;

Estimator:calculates the changed attribute value based on the percentage of the current attribute.

7.3.3 Property Animation Listener

animatorlistener listens to the start, end, cancel, and repeat playback of the animation;

animatorupdatelistener listens to the entire animation process.

7.3.4 Animating arbitrary attributes

Animate the object's attribute abc. If i want the animation to take effect, you need to meet two conditions:

(1) The object must provide the setabc method. If the initial value is not passed during the animation,Then you have to provide the getabc method, otherwise the program crashes directly;

(2) The change made by object's setabc to the attribute abc must be able to be reflected by some method,Such as bringing changes in UI.

For how to animate an original object, see Method 2 on page 285.

7.3.5 How Attribute Animation Works

Property animation requires that the object that the animation acts on provides the set method of the property. Property animation is based on the initial and final values ​​of the property that you pass,And the effect of animation has been removed many times with the set method. Each time the value passed to the set method is different,Exactly over time,The value passed is getting closer and closer to the final value.If no initial value is passed during the animation,Then you also need to provide the get method, because the system needs to get the initial value of the property.

7.4 Notes on using animation

Note that the view animation is to animate the image of the view.It does not really change the state of the view, so sometimes the view cannot be hidden after the animation is completed.That is, setvisibility (view.gone) is invalid. At this time, simply call view.clearanimation () to clear the view animation to solve this problem.

After moving (panning) the view, on systems before Android 3.0,Whether it is view animation or attribute animation,Click events cannot be triggered at the new location.At the same time, the old position can still trigger a click event.Although the view no longer exists visually,After moving the view back to its original position,The click event at the original position continues to take effect.Starting from 3.0, the click event trigger position of the property animation is the position after the movement.But the view animation is still in its original position.

  • Previous PHP image cropping and cropping source code and usage method
  • Next Analysis of the realization method of javascript uniform motion