Wednesday, January 21, 2015

Android beginner tutorial Part 93 XML animation files

In this tutorial well learn about the structure of XML files that are used for defining animations in Android.

The XML files for animation are put into the res/anim/ directory of the Android project. The file has to have a single root node, which can be either of the possible elements: , , , or .

The node can act as a container for all of the elements, including other elements.

Even though the elements are put in a sequence, the animations play at the same time. But you can use the startOffset property to set delay before each animation start.

There are attributes that can be applied to all the listed elements, as well as attributes that can only be applied to a specific type of animation element.

First, lets take a look at the global ones - duration, startOffset, fillBefore, fillAfter, repeatCount, repeatMode, zAdjustment, interpolator.

The duration value is basically the duration time of the animation in milliseconds.

The startOffset value is the time delay before the animation starts.

The fillBefore is a boolean value, which, if set to true, makes sure that the animation transformation is applied before the beginning of the animation (during the startOffset period).

The fillAfter is a boolean value, which, if set to true, makes sure that the animation transformation is applied after the animation ends.

The repeatCount value determines how many times the animation repeats.

The repeatMode can be set to "restart" or "reverse" to make the animation start over or play in reverse when it ends.

The zAdjustment value sets the adjustment of the Z ordering (depth) of the content. Can be set to "normal" to be treated normally and kept in its current Z order, to "top" to put it on top of everything else and "bottom" to put it behind everything else.

The interpolator value holds a reference to an interpolator, which is an animation modifier that can affect the rate of change in your animation. There is a number of pre-defined interpolators that you can use, or you can create custom ones. Example: @android:anim/accelerate_interpolator.

Now lets take a look at the exclusive attributes.

The set container supports a shareInterpolator attribute, which can be used for setting one interpolator for use by all the children of the set.

The alpha element has 2 attributes - fromAlpha and toAlpha, which determine the initial and destination alpha values for the object. Values range from 0 to 1.

The scale element is used for scaling objects. It has 6 new attributes - fromXScale, toXScale, fromYScale, toYScale, pivotX and pivotY. The first four simply determine the initial and destination scaling values on both axes. The pivotX and pivotY determine the coordinates of the anchor position, relative to which the scaling will ocur. This is similar to registration point in DisplayObjects in Flash.

The translate element lets us move an object across the screen. Possible new attributes are fromXDelta, toXDelta, fromYDelta and toYDelta. Possible values can be in 3 formats - an absolute numeric value, a relative value in percentages (from -100% to 100%) and value in percentages relative to the size of the objects parent (-100%p to 100%p).

The rotate element is used for rotating objects and has 4 attributes - fromDegrees, toDegrees, pivotX, pivotY. The first two determine the initial and destination rotation degrees, the second pair detemine the coordinates of the anchor.

Thats all for today.

Thanks for reading!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.