Animation Resources

来源:互联网 发布:windows碎片整理 编辑:程序博客网 时间:2024/05/28 17:05

Animation Resources

@(Android)

  • Animation Resources
    • Property Animation
      • 语法
        • set 标签
        • objectAnimator 标签
        • animator标签
      • 示例
    • View Animation
      • Tween animation
        • 语法
          • set 标签
          • alpha 标签
          • rotate 标签
          • scale 标签
          • translate 标签
        • 示例
        • 插值器
          • Custom interpolators自定义插值器
            • 语法
            • 示例
      • Frame animation
        • 语法
        • 示例

API Guides

An animation resource can define one of two types of animations:

  • Property Animation
    Creates an animation by modifying an object’s property values over a set period of time with an Animator.

  • View Animation
    There are two types of animations that you can do with the view animation framework:

    • Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation
    • Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable.


Property Animation

文件位置:

res/animator/filename.xml

数据类型:

Resource pointer to a ValueAnimator, ObjectAnimator, or AnimatorSet.

使用方式:

In Java: R.animator.filenameIn XML: @[package:]animator/filename

语法

<set  android:ordering=["together" | "sequentially"]>    <objectAnimator        android:propertyName="string"        android:duration="int"        android:valueFrom="float | int | color"        android:valueTo="float | int | color"        android:startOffset="int"        android:repeatCount="int"        android:repeatMode=["repeat" | "reverse"]        android:valueType=["intType" | "floatType"]/>    <animator        android:duration="int"        android:valueFrom="float | int | color"        android:valueTo="float | int | color"        android:startOffset="int"        android:repeatCount="int"        android:repeatMode=["repeat" | "reverse"]        android:valueType=["intType" | "floatType"]/>    <set>        ...    </set></set>

The file must have a single root element: either <set>, <objectAnimator>, or <valueAnimator>. You can group animation elements together inside the <set> element, including other <set>elements.

set 标签

A container that holds other animation elements (<objectAnimator>, <valueAnimator>, or other <set> elements). Represents an AnimatorSet.

You can specify nested <set> tags to further group animations together. Each <set> can define its own ordering attribute.

android:ordering

Keyword. Specifies the play ordering of animations in this set.

Value Description sequentially Play animations in this set sequentially together Play animations in this set at the same time (default)

objectAnimator 标签

Animates a specific property of an object over a specific amount of time. Represents an ObjectAnimator.

android:propertyName:属性名称

String. Required. The object’s property to animate, referenced by its name.

android:duration:动画持续时间

int. The time in milliseconds of the animation. 300 milliseconds is the default.

android:valueType:数值类型

Keyword. Do not specify this attribute if the value is a color. The animation framework automatically handles color values

Value Description intType Specifies that the animated values are integers floatType Specifies that the animated values are floats (default)

android:valueFrom:开始值

float, int, or color. The value where the animated property starts. If not specified, the animation starts at the value obtained by the property’s get method. Colors are represented as six digit hexadecimal numbers (for example, #333333).

android:valueTo:结束值

float, int, or color. Required. The value where the animated property ends. Colors are represented as six digit hexadecimal numbers.

android:startOffset:延迟时间

int. The amount of milliseconds the animation delays after start() is called.

android:repeatCount:重复次数

int. How many times to repeat an animation. Set to “-1” to infinitely repeat or to a positive integer. For example, a value of “1” means that the animation is repeated once after the initial run of the animation, so the animation plays a total of two times. The default value is “0”, which means no repetition.

android:repeatMode:重复模式

int. How an animation behaves when it reaches the end of the animation. android:repeatCount must be set to a positive integer or “-1” for this attribute to have an effect. Set to “reverse” to have the animation reverse direction with each iteration or “repeat” to have the animation loop from the beginning each time.

animator标签

Performs an animation over a specified amount of time. Represents a ValueAnimator.

android:duration:动画持续时间
android:valueType:数值类型
android:valueFrom:开始值
android:valueTo:结束值
android:startOffset:延迟时间
android:repeatCount:重复次数
android:repeatMode:重复模式

示例

XML file saved at res/animator/property_animator.xml:

<set android:ordering="sequentially">    <set>        <objectAnimator            android:propertyName="x"            android:duration="500"            android:valueTo="400"            android:valueType="intType"/>        <objectAnimator            android:propertyName="y"            android:duration="500"            android:valueTo="300"            android:valueType="intType"/>    </set>    <objectAnimator        android:propertyName="alpha"        android:duration="500"        android:valueTo="1f"/></set>

In order to run this animation, you must inflate the XML resources in your code to an AnimatorSet object, and then set the target objects for all of the animations before starting the animation set. Calling setTarget() sets a single target object for all children of the AnimatorSet as a convenience. The following code shows how to do this:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(    myContext,R.anim.property_animator);set.setTarget(myObject);set.start();

View Animation

The view animation framework supports both tween and frame by frame animations, which can both be declared in XML.

Tween animation

An animation defined in XML that performs transitions such as rotating, fading, moving, and stretching on a graphic.

文件位置:

res/anim/filename.xml

数据类型:

Resource pointer to an Animation.

使用方式:

In Java: R.anim.filenameIn XML: @[package:]anim/filename

语法

<?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"        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 file must have a single root element: either an <alpha>, <scale>, <translate>, <rotate>, or <set> element that holds a group (or groups) of other animation elements (even nested <set> elements).

set 标签

A container that holds other animation elements (<alpha>, <scale>, <translate>, <rotate>) or other <set> elements. Represents an AnimationSet.

set 标签标识动画集合,对应 AnimationSet 。

android:interpolator:插值器

Interpolator resource. An Interpolator to apply on the animation. The value must be a reference to a resource that specifies an interpolator (not an interpolator class name). There are default interpolator resources available from the platform or you can create your own interpolator resource.

表示动画集合所采用的插值器,插值器影响动画的速度,比如非匀速动画就需要通过插值器来控制动画的播放过程。这个属性可以不指定,默认为@android:anim/accelerate_decelerate_interpolator(即:加速减速插值器)

android:shareInterpolator:是否共享同一个插值器

Boolean. “true” if you want to share the same interpolator among all child elements.

表示集合中的动画是否和集合共享同一个插值器。如果集合不指定插值器,那么子动画就需要单独指定所需的插值器或者采用默认值。

alpha 标签

A fade-in or fade-out animation. Represents an AlphaAnimation.

alpha 标签表示透明度动画,对应 AlphaAnimation。

android:fromAlpha:透明度的起始值

Float. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque.

android:toAlpha:透明度的结束值

Float. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.

rotate 标签

A rotation animation. Represents a RotateAnimation.

rotate 标签表示旋转动画,对应 RotateAnimation。

android:fromDegrees:旋转开始的角度

Float. Starting angular position, in degrees.

android:toDegrees:旋转结束的角度

Float. Ending angular position, in degrees.

android:pivotX:旋转的轴点的 x 坐标

android:pivotY:旋转的轴点的 y 坐标

Float or percentage. The Y coordinate of the center of rotation. Expressed either: in pixels relative to the object’s top edge (such as “5”), in percentage relative to the object’s top edge (such as “5%”), or in percentage relative to the parent container’s top edge (such as “5%p”).

scale 标签:

A resizing animation. You can specify the center point of the image from which it grows outward (or inward) by specifying pivotX and pivotY. For example, if these values are 0, 0 (top-left corner), all growth will be down and to the right. Represents a ScaleAnimation.

scale 标签表示缩放动画,对应 ScaleAnimation。

android:fromXScale:水平方向缩放的起始值

Float. Starting X size offset, where 1.0 is no change.

android:toXScale:水平方向缩放的结束值

Float. Ending X size offset, where 1.0 is no change.

android:fromYScale:竖直方向缩放的起始值

Float. Starting Y size offset, where 1.0 is no change.

android:toYScale:竖直方向缩放的结束值

Float. Ending Y size offset, where 1.0 is no change.

android:pivotX:缩放的轴点的 x 坐标

Float. The X coordinate to remain fixed when the object is scaled.

android:pivotY:缩放的轴点的 y 坐标

Float. The Y coordinate to remain fixed when the object is scaled.

translate 标签

A vertical and/or horizontal motion. Supports the following attributes in any of the following three formats: values from -100 to 100 ending with “%”, indicating a percentage relative to itself; values from -100 to 100 ending in “%p”, indicating a percentage relative to its parent; a float value with no suffix, indicating an absolute value. Represents a TranslateAnimation.

translate 标签表示平移动画,对应 TranslateAnimation。

android:fromXDelta:表示 x 的起始值

android:toXDelta:表示 x 的结束值

android:fromYDelta:表示 y 的起始值

android:toYDelta:表示 y 的结束值

Float or percentage. Ending Y offset. Expressed either: in pixels relative to the normal position (such as “5”), in percentage relative to the element height (such as “5%”), or in percentage relative to the parent height (such as “5%p”).

示例

XML file saved at res/anim/hyperspace_jump.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false">    <scale        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromXScale="1.0"        android:toXScale="1.4"        android:fromYScale="1.0"        android:toYScale="0.6"        android:pivotX="50%"        android:pivotY="50%"        android:fillAfter="false"        android:duration="700" />    <set        android:interpolator="@android:anim/accelerate_interpolator"        android:startOffset="700">        <scale            android:fromXScale="1.4"            android:toXScale="0.0"            android:fromYScale="0.6"            android:toYScale="0.0"            android:pivotX="50%"            android:pivotY="50%"            android:duration="400" />        <rotate            android:fromDegrees="0"            android:toDegrees="-45"            android:toYScale="0.0"            android:pivotX="50%"            android:pivotY="50%"            android:duration="400" />    </set></set>

This application code will apply the animation to an ImageView and start the animation:

ImageView image = (ImageView) findViewById(R.id.image);Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);image.startAnimation(hyperspaceJump);

插值器

An interpolator is an animation modifier defined in XML that affects the rate of change in an animation. This allows your existing animation effects to be accelerated, decelerated, repeated, bounced, etc.

An interpolator is applied to an animation element with the android:interpolator attribute, the value of which is a reference to an interpolator resource.

All interpolators available in Android are subclasses of the Interpolator class. For each interpolator class, Android includes a public resource you can reference in order to apply the interpolator to an animation using the android:interpolator attribute.

插值器是一个定义在 XML 中能够影响动画变换速率的调节器。

The following table specifies the resource to use for each interpolator:

Interpolator class Resource ID LinearInterpolator @android:anim/linear_interpolator AccelerateInterpolator @android:anim/accelerate_interpolator DecelerateInterpolator @android:anim/decelerate_interpolator AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator BounceInterpolator @android:anim/bounce_interpolator CycleInterpolator @android:anim/cycle_interpolator AnticipateInterpolator @android:anim/anticipate_interpolator OvershootInterpolator @android:anim/overshoot_interpolator AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator

Here’s how you can apply one of these with the android:interpolator attribute:

<set android:interpolator="@android:anim/accelerate_interpolator">    ...</set>
Custom interpolators(自定义插值器)

文件位置:

res/anim/filename.xml

数据类型:

Resource pointer to the corresponding interpolator object.

使用方式:

In XML: @[package:]anim/filename

语法
<?xml version="1.0" encoding="utf-8"?><InterpolatorName xmlns:android="http://schemas.android.com/apk/res/android"    android:attribute_name="value"    />

If you don’t apply any attributes, then your interpolator will function exactly the same as those provided by the platform (listed in the table above).

<linearInterpolator>:线性插值器
The rate of change is constant.

<accelerateInterpolator>:加速插值器
The rate of change starts out slowly, then accelerates.
+ android:factor:加速度
Float. The acceleration rate (default is 1).

<decelerateInterpolator>:减速插值器
The rate of change starts out quickly, then decelerates.
+ android:factor:减速度
Float. The deceleration rate (default is 1).

<accelerateDecelerateInterpolator>:先加速后减速插值器
The rate of change starts and ends slowly but accelerates through the middle. No attributes.

<bounceInterpolator>:弹跳插值器
The change bounces at the end. No attributes

<cycleInterpolator>:循环插值器
Repeats the animation for a specified number of cycles. The rate of change follows a sinusoidal pattern.

  • android:cycles:循环次数
    Integer. The number of cycles (default is 1).

<anticipateInterpolator>
The change starts backward then flings forward.

  • android:tension:张力
    Float. The amount of tension to apply (default is 2).

<overshootInterpolator>
The change flings forward and overshoots the last value, then comes back.
向前甩一定值后再回到原来位置

  • android:tension
    Float. The amount of tension to apply (default is 2).

<anticipateOvershootInterpolator>
The change starts backward, flings forward and overshoots the target value, then settles at the final value.
开始的时候向后然后向前甩一定值后返回最后的值

  • android:tension
    Float. The amount of tension to apply (default is 2).
  • android:extraTension
    Float. The amount by which to multiply the tension (default is 1.5).
示例

XML file saved at res/anim/my_overshoot_interpolator.xml:

<?xml version="1.0" encoding="utf-8"?><overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"    android:tension="7.0"    />

This animation XML will apply the interpolator:

<scale xmlns: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:pivotX="50%"    android:pivotY="50%"    android:duration="700" />

Frame animation

An animation defined in XML that shows a sequence of images in order (like a film).

文件位置:
res/drawable/filename.xml

数据类型:
Resource pointer to an AnimationDrawable.

使用方式:

In Java: R.drawable.filenameIn XML: @[package:]drawable.filename

语法

<?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="@[package:]drawable/drawable_resource_name"        android:duration="integer" /></animation-list>

<animation-list>标签
Required. This must be the root element. Contains one or more elements.
+ android:oneshot
Boolean. “true” if you want to perform the animation once; “false” to loop the animation.

<item>标签
A single frame of animation. Must be a child of a <animation-list>element.
+ android:drawable
Drawable resource. The drawable to use for this frame.
+ android:duration
Integer. The duration to show this frame, in milliseconds.

示例

XML file saved at res/anim/rocket.xml:<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /></animation-list>

This application code will set the animation as the background for a View, then play the animation:

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);rocketImage.setBackgroundResource(R.drawable.rocket_thrust);rocketAnimation = (AnimationDrawable) rocketImage.getBackground();rocketAnimation.start();
1 0
原创粉丝点击