Android Application Resources-Animation Resources (III)

Source: Internet
Author: User

Plug-in

The plug-in is an editor defined in XML that can affect the frequency of animation changes. It will affect the acceleration, deceleration, replay, rebound, and other effects of existing animations.

Use the android: interpolator attribute to apply the plug-in to an animation element. Its value is a reference of a plug-in resource.

In Android, all available plug-ins are subclasses of the Interpolator class. For each plug-in makeup class, Android contains a public resource that can be referenced to apply this plug-in to the android: interpolator attribute used by animation. The following table specifies the resources available for each plug-in.

Plug-in
Resource ID
 
AccelerateDecelerateInterpolator
@ Android: anim/accelerate_decelerate_interpolator
 
AccelerateInterpolator
@ Android: anim/accelerate_interpolator
 
AnticipateInterpolator
@ Android: anim/anticipate_interpolator
 
AnticipateOvershootInterpolator
@ Android: anim/anticipate_overshoot_interpolator
 
BounceInterpolator
@ Android: anim/bounce_interpolator
 
CycleInterpolator
@ Android: anim/cycle_interpolator
 
DecelerateInterpolator
@ Android: anim/decelerate_interpolator
 
LinearInterpolator
@ Android: anim/linear_interpolator
 
OvershootInterpolator
@ Android: anim/overshoot_interpolator
 

 

You can set the android: interpolator attribute as follows:

<Setandroid: interpolator = "@ android: anim/accelerate_interpolator">
...
</Set>

 

Custom plug-ins

If you are not satisfied with the plug-in Plug-In provided by the platform (the plug-in Plug-in listed in the above table), you can also edit the attribute method to create a custom plug-in resource. For example, you can adjust the acceleration frequency of the AnticipateInterpolator plug-in, or adjust the cycle quantity of the CycleInterpolator plug-in. To achieve this goal, you need to create your own plug-in resource in an XML file.

File location ):

Res/anim/filename. xml. The file name is used as the resource ID.

Compiled resource type (compiled resource datatype)

Resource pointing to the corresponding plug-in object

Resource reference ):

In XML: @ [package:] anim/filename

SYNTAX (SYNTAX ):

<? Xml version = "1.0" encoding = "UTF-8"?>
<InterpolatorNamexmlns: android = "http://schemas.android.com/apk/res/android"
Android: attribute_name = "value"
/>

If no attributes are applied, the customized plug-in will have the same functions as the plug-in provided by the platform.

Element (ELEMENTS ):

Note that the implementation of each plug-in is defined in XML and starts with a lowercase letter.

<AccelerateDecelerateInterpolator>

Reduces the frequency of changes at the animation start and end, but accelerates at the center of the animation.

No attribute

<AccelerateInterpolator>

Reduces the frequency of changes when the animation starts and starts acceleration.

ATTRIBUTES (ATTRIBUTES ):

Android: factor

Floating point value, specifying the acceleration frequency (1 by default)

<AnticipateInterpolator>

When the animation starts, it first changes backward and then forward (rubber band effect ).

ATTRIBUTES (ATTRIBUTES ):

Android: tension

Floating point value, specifying the number of tension (2 by default)

<AnticipateOvershootInterpolator>

When the animation is started, it first goes backward, then goes forward, and flies over the target value, and then ends stably.

ATTRIBUTES (ATTRIBUTES ):

Android: tension

Floating point value, specifying the number of tension (2 by default)

Android: extraTension

Floating point value, specifying a multiple of tension (default value: 1.5)

<BounceInterpolator>

The bounce effect at the end of the animation.

No attribute

<CycleInterpolator>

Repeat the animation with the specified number of loops. The variation frequency is in the sine pattern.

ATTRIBUTES (ATTRIBUTES ):

Android: cycles

An integer that specifies the number of cycles (1 by default ).

<DecelerateInterpolator>

When the animation starts, it quickly jumps out and then slows down.

ATTRIBUTES (ATTRIBUTES ):

Android: factor

Floating point value, specifying the deceleration ratio (default value: 1)

<LinearInterpolator>

The animation change frequency is fixed.

No attribute.

<OvershootInterpolator>

Throw forward, fly over the terminal, and return back.

ATTRIBUTES (ATTRIBUTES ):

Android: tension

Floating point value, set the number of tension (2 by default)

Example:

The XML file is saved in res/anim/my_overshoot_interpolator.xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<OvershootInterpolatorxmlns: android = "http://schemas.android.com/apk/res/android"
Android: tension= "7.0"
/>

The following is the plug-in used by the animation XML:

<Scalexmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ anim/my_overshoot_interpolator"
Android: fromXScale = "1.0"
Android: toXScale = "3.0"
Android: fromYScale = "1.0"
Android: toYScale = "3.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration = "700"/>

Frame Animation

An animation is defined in XML. It plays an image in sequence like a movie.

File location ):

Res/drawable/filename. xml

The file name is used as the resource ID.

Compiled resource type (compiled resource datatype ):

The resource points to an AnimationDrawable object.

RESOURCE REFERENCE)

In Java code: R. drawable. filename

In XML: @ [package:] drawable. filename

SYNTAX (SYNTAX ):

<? Xml version = "1.0" encoding = "UTF-8"?>
<Animation-listxmlns: android = "http://schemas.android.com/apk/res/android"
Android: oneshot = ["true" | "false"]>
<Item
Android: drawable = "@ [package:] drawable/drawable_resource_name"
Android: duration = "integer"/>
</Animation-list>

 

Element (ELEMENTS ):

<Animation-list>

Required. It must be a root element. It must contain one or more <item> elements.

ATTRIBUTES (ATTRIBUTES ):

Android: oneshot

Boolean value. If you only want to run the animation once, set it to "true". Otherwise, set it to "false" to play the animation cyclically.

<Item>

An animation frame must be a child element of the <animation-list> element.

ATTRIBUTES (ATTRIBUTES ):

Android: drawable

Specifies the plotting resource used for the current animation.

Android: duration

Integer, in milliseconds, shows the animation duration.

Example:

The XML file is saved in res/anim/rocket. xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Animation-listxmlns: android = "http://schemas.android.com/apk/res/android"
Android: oneshot = "false">
<Itemandroid: drawable = "@ drawable/rocket_thrust1" android: duration = "200"/>
<Itemandroid: drawable = "@ drawable/rocket_thrust2" android: duration = "200"/>
<Itemandroid: drawable = "@ drawable/rocket_thrust3" android: duration = "200"/>
</Animation-list>

The application code sets this animation as the background of a View object and then plays the animation:

ImageView rocketImage = (ImageView) findViewById (R. id. rocket_image );
RocketImage. setBackgroundResource (R. drawable. rocket_thrust );

RocketAnimation = (AnimationDrawable) rocketImage. getBackground ();
RocketAnimation. start ();

 

From FireOfStar's column

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.