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 anAnimation
  • Frame animation: or creates an animation by showing a sequence of images in order with anAnimationDrawable.

Property Animation

An animation defined in XML that modifies properties of the target object, such as background color or alpha value, over a set amount of time.

FILE LOCATION:
res/animator/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to a ValueAnimator, ObjectAnimator, or AnimatorSet.
RESOURCE REFERENCE:
In Java: R.animator.filename
In XML: @[package:]animator/filename
SYNTAX:
<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.

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 ownorderingattribute.

attributes:

android:ordering
Keyword. Specifies the play ordering of animations in this set.
Value Description
sequentially Play animations in this set sequentially
together(default) Play animations in this set at the same time.
<objectAnimator>
Animates a specific property of an object over a specific amount of time. Represents an ObjectAnimator.

attributes:

android:propertyName
String. Required. The object's property to animate, referenced by its name. For example you can specify "alpha"or "backgroundColor"for a View object. The objectAnimatorelement does not expose a targetattribute, however, so you cannot set the object to animate in the XML declaration. You have to inflate your animation XML resource by calling loadAnimator()and call setTarget()to set the target object that contains this property.
android:valueTo
float, int, or color. Required. The value where the animated property ends. Colors are represented as six digit hexadecimal numbers (for example, #333333).
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:duration
int. The time in milliseconds of the animation. 300 milliseconds is the default.
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:repeatCountmust 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.
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(default) Specifies that the animated values are floats
<animator>
Performs an animation over a specified amount of time. Represents a ValueAnimator.

attributes:

android:valueTo
float, int, or color. Required. The value where the animation ends. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:valueFrom
float, int, or color. Required. The value where the animation starts. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:duration
int. The time in milliseconds of the animation. 300ms is the default.
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:repeatCountmust 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.
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(default) Specifies that the animated values are floats
EXAMPLE:
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 anAnimatorSetobject, and then set the target objects for all of the animations before starting the animation set. CallingsetTarget()sets a single target object for all children of theAnimatorSetas 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();
SEE ALSO:
  • Property Animation
  • API Demosfor examples on how to use the property animation system.

View Animation

The view animation framework supports both tween and frame by frame animations, which can both be declared in XML. The following sections describe how to use both methods.

Tween animation

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

FILE LOCATION:
res/anim/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to an Animation.
RESOURCE REFERENCE:
In Java: R.anim.filename
In XML: @[package:]anim/filename
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 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).

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

attributes:

android:interpolator
Interpolator resource. An Interpolatorto 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. See the discussion below for more about Interpolators.
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.

attributes:

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.

For more attributes supported by<alpha>, see theAnimationclass reference (of which, all XML attributes are inherrited by this element).

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

attributes:

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
Float. The X coordinate to remain fixed when the object is scaled.
android:pivotY
Float. The Y coordinate to remain fixed when the object is scaled.

For more attributes supported by<scale>, see theAnimationclass reference (of which, all XML attributes are inherrited by this element).

<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.

attributes:

android:fromXDelta
Float or percentage. Starting X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
android:toXDelta
Float or percentage. Ending X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
android:fromYDelta
Float or percentage. Starting 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").
android:toYDelta
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").

For more attributes supported by<translate>, see theAnimationclass reference (of which, all XML attributes are inherrited by this element).

<rotate>
A rotation animation. Represents a RotateAnimation.

attributes:

android:fromDegrees
Float. Starting angular position, in degrees.
android:toDegrees
Float. Ending angular position, in degrees.
android:pivotX
Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object's left edge (such as "5"), in percentage relative to the object's left edge (such as "5%"), or in percentage relative to the parent container's left edge (such as "5%p").
android:pivotY
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").

For more attributes supported by<rotate>, see theAnimationclass reference (of which, all XML attributes are inherrited by this element).

EXAMPLE:
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 anImageViewand start the animation:

ImageView image = (ImageView) findViewById(R.id.image);Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);image.startAnimation(hyperspaceJump);
SEE ALSO:
  • 2D Graphics: Tween Animation

Interpolators

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 theandroid:interpolatorattribute, the value of which is a reference to an interpolator resource.

All interpolators available in Android are subclasses of theInterpolatorclass. For each interpolator class, Android includes a public resource you can reference in order to apply the interpolator to an animation using theandroid:interpolatorattribute. The following table specifies the resource to use for each interpolator:

Interpolator class 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

Here's how you can apply one of these with theandroid:interpolatorattribute:

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

Custom interpolators

If you're not satisfied with the interpolators provided by the platform (listed in the table above), you can create a custom interpolator resource with modified attributes. For example, you can adjust the rate of acceleration for theAnticipateInterpolator, or adjust the number of cycles for theCycleInterpolator. In order to do so, you need to create your own interpolator resource in an XML file.

FILE LOCATION:
res/anim/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to the corresponding interpolator object.
RESOURCE REFERENCE:
In XML: @[package:]anim/filename
SYNTAX:
<?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).

ELEMENTS:
Notice that each Interpolatorimplementation, when defined in XML, begins its name in lowercase.

<accelerateDecelerateInterpolator>
The rate of change starts and ends slowly but accelerates through the middle.

No attributes.

<accelerateInterpolator>
The rate of change starts out slowly, then accelerates.

attributes:

android:factor
Float. The acceleration rate (default is 1).
<anticipateInterpolator>
The change starts backward then flings forward.

attributes:

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.

attributes:

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).
<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.

attributes:

android:cycles
Integer. The number of cycles (default is 1).
<decelerateInterpolator>
The rate of change starts out quickly, then decelerates.

attributes:

android:factor
Float. The deceleration rate (default is 1).
<linearInterpolator>
The rate of change is constant.

No attributes.

<overshootInterpolator>
The change flings forward and overshoots the last value, then comes back.

attributes:

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

XML file saved atres/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).

FILE LOCATION:
res/drawable/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to an AnimationDrawable.
RESOURCE REFERENCE:
In Java: R.drawable.filename
In XML: @[package:]drawable.filename
SYNTAX:
<?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>
ELEMENTS:
<animation-list>
Required. This must be the root element. Contains one or more <item>elements.

attributes:

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.

attributes:

android:drawable
Drawable resource. The drawable to use for this frame.
android:duration
Integer. The duration to show this frame, in milliseconds.
EXAMPLE:
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();
SEE ALSO:

  • 2D Graphics: Frame Animation

动画资源可以定义两种类型的动画中的一者:

物业动画
通过在时间与一组时间修改对象的属性值创建一个动画的 动画师
观看动画

有两种类型的动画,你可以认为动画框架做的:

  • 吐温动画:通过用一个单一的图像执行一系列转换创建动画动画
  • 帧动画:或通过示出,以便与一个图像的序列创建动画AnimationDrawable

物业动画

在XML中定义的动画来修改目标对象的性质,例如背景颜色或阿尔法值,在设定的时间量。

文件位置:
RES /动画/文件名​​的.xml
文件名 ​​将被用作资源ID。
编译的资源数据类型:
资源指针 ValueAnimatorObjectAnimatorAnimatorSet
资源引用:
在Java: 。R.animator
在XML: @ [:]动画/文件名
句法:
< 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>

该文件必须有一个根元素:要么<SET><objectAnimator><valueAnimator>。您可以将动漫元素结合在一起里面<设置>元素,包括其他<设置>元素。

内容:
<设置>
持有其他动画元素的容器( <objectAnimator><valueAnimator>或其他 <设置>元素)。表示一个 AnimatorSet

您可以指定嵌套的<设置>标签来进一步群的动画在一起。每个<SET>可以定义自己的排序属性。

属性:

机器人:订货
关键字。指定动画在此设置播放顺序。
描述
顺序 播放动画在这组顺序
在一起(默认) 在此设置在同一时间播放动画。
<objectAnimator>
动画对象的结束时间的特定量的特定属性。表示一个 ObjectAnimator

属性:

机器人:propertyName的
字符串必需。该对象的属性进行动画,它的名字引用。例如,您可以指定 “阿尔法”“的backgroundColor”的视图对象。该 objectAnimator元素不暴露 目标的属性,但是,这样你就可以不设置在XML声明动画的对象。你必须通过调用充气动画XML资源 loadAnimator()并调用 setTarget()来设置包含此属性的目标对象。
机器人:valueTo
浮动,int或颜色必需。其中动画属性的结束值。颜色被表示为六位十六进制数(例如,#333333)。
机器人:valueFrom
浮动,int或颜色。其中动画属性开始的值。如果没有指定,动画开始于由物业的get方法得到的值。颜色被表示为六位十六进制数(例如,#333333)。
机器人:时间
INT。在动画毫秒的时间。300毫秒是默认的。
机器人:startOffset
INT。毫秒动画后延迟量 的start()被调用。
安卓的repeatCount
INT。多少次重复的动画。集到 “-1”,以无限重复,或者一个正整数。例如,值 “1”,意味着该动画是动画的初始运行后重复一次,所以在动画中起着共两次。默认值是 “0”,这意味着没有重复。
机器人:REPEATMODE
INT。如何当到达动画结尾动画表现 安卓的repeatCount必须被设置为正整数或 “-1”这个属性有效果。设置为 “反向”每次迭代具有动画相反方向或 “重复”具有从每次开始动画循环。
机器人:VALUETYPE
关键字。如果值是颜色不指定此属性。动画框架自动处理颜色值
描述
IntType上 指定动画的值是整数
floatType(默认) 指定动画的值是浮点数
<动画>
进行过指定的时间量的动画。代表 ValueAnimator

属性:

机器人:valueTo
浮动,int或颜色必需。其中,动画结束的值。颜色被表示为六位十六进制数(例如,#333333)。
机器人:valueFrom
浮动,int或颜色必需。其中动画的开始值。颜色被表示为六位十六进制数(例如,#333333)。
机器人:时间
INT。在动画毫秒的时间。300ms的是默认的。
机器人:startOffset
INT。毫秒动画后延迟量 的start()被调用。
安卓的repeatCount
INT。多少次重复的动画。集到 “-1”,以无限重复,或者一个正整数。例如,值 “1”,意味着该动画是动画的初始运行后重复一次,所以在动画中起着共两次。默认值是 “0”,这意味着没有重复。
机器人:REPEATMODE
INT。如何当到达动画结尾动画表现 安卓的repeatCount必须被设置为正整数或 “-1”这个属性有效果。设置为 “反向”每次迭代具有动画相反方向或 “重复”具有从每次开始动画循环。
机器人:VALUETYPE
关键字。如果值是颜色不指定此属性。动画框架自动处理颜色值。
描述
IntType上 指定动画的值是整数
floatType(默认) 指定动画的值是浮点数
例:
在保存XML文件 RES /动画/ 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>

为了运行这个动画,必须在代码中的充气XML资源AnimatorSet对象,然后开始动画集之前为所有动画的目标对象。调用setTarget()设置的所有孩子一个目标对象AnimatorSet的方便。下面的代码演示如何做到这一点:

AnimatorSet  set  =  ( AnimatorSet )  AnimatorInflater . loadAnimator ( myContext ,   R . anim . property_animator ); set . setTarget ( myObject ); set . start ();
也可以看看:
  • 物业动画
  • API演示了如何使用属性动画系统的例子。

观看动画

视图动画框架支持吐温和逐帧动画,可以在两个XML声明。下面的章节描述了如何使用这两种方法。

吐温动画

在XML中定义的动画执行过渡,例如旋转,衰落,移动和拉伸上的图形。

文件位置:
RES /阿尼姆/文件名​​的.xml
文件名 ​​将被用作资源ID。
编译的资源数据类型:
资源指向一个 动画
资源引用:
在Java: 。R.anim
在XML: @ [:]动画/文件名
句法:
<?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>

该文件必须有一个根元素:要么是<阿尔法><规模><翻译><旋转><设置>元素持有其他动画元素的组(或组)(甚至嵌套的<设置>元素)。

内容:
<设置>
持有其他动画元素的容器( <阿尔法><规模><翻译><旋转>)或其他 <设置>元素。表示一个 AnimationSet

属性:

机器人:插值
插补资源。一个 插补应用的动画。该值必须是一个指定的内插器(未内插器类名称)的资源的参考。有可从平台默认的内插资源,也可以创建自己的内插器的资源。请参见下面的讨论,更多的 内插器。
机器人:shareInterpolator
布尔。“真”,如果你想分享所有的子元素中相同的插补器。
<阿尔法>
淡入或淡出动画。表示一个 AlphaAnimation

属性:

机器人:fromAlpha
浮动。起始的不透明度偏置,其中0.0是透明,1.0是不透明的。
机器人:toAlpha
浮动。结束不透明度偏置,其中0.0是透明,1.0是不透明的。

通过支持多个属性<阿尔法>,看动画类引用(其中,所有XML属性由该元素inherrited)。

<规模>
一个伸缩动画。可以通过指定指定从它向外(或向内)生长的图像的中心点 pivotXpivotY。例如,如果这些值是0,0(左上角),所有增长将向下和向右。代表 ScaleAnimation

属性:

机器人:fromXScale
浮动。启动X尺寸偏差,其中1.0是没有任何变化。
机器人:toXScale
浮动。结束x尺寸偏差,其中1.0是没有任何变化。
机器人:fromYScale
浮动。起始Y尺寸偏差,其中1.0是没有任何变化。
机器人:toYScale
浮动。结束Y规格偏移,其中1.0是没有任何变化。
机器人:pivotX
浮动。的X坐标,以保持固定对象时缩放。
机器人:pivotY
浮动。的Y坐标,以保持固定对象时缩放。

通过支持多个属性<规模>,看动画类引用(其中,所有XML属性由该元素inherrited)。

<翻译>
垂直和/或水平运动。支持在以下任何三种格式的以下属性:值从-100到100以“%”结尾,指示相对于本身的百分比;值从-100到100“%P”结尾,这表明相对于其父的百分比;一个浮点值,没有后缀,表示绝对值。代表 TranslateAnimation

属性:

机器人:fromXDelta
浮动或百分比。开始的X轴偏移。表示任一:在相对 ​​于正常位置的像素(例如 “5”),在相对 ​​于该元件宽度百分比(如 “5%”),或在相对 ​​百分比到母体宽度(如 “5%对”)。
机器人:toXDelta
浮动或百分比。结束X轴偏移。表示任一:在相对 ​​于正常位置的像素(例如 “5”),在相对 ​​于该元件宽度百分比(如 “5%”),或在相对 ​​百分比到母体宽度(如 “5%对”)。
机器人:fromYDelta
浮动或百分比。开始的Y轴偏移。表示任一:在相对 ​​于正常位置的像素(例如 “5”),在相对 ​​于所述元件的高度的百分比(如 “5%”),或在相对 ​​百分比到母体高度(如 “5%对”)。
机器人:toYDelta
浮动或百分比。结束Y轴偏移。表示任一:在相对 ​​于正常位置的像素(例如 “5”),在相对 ​​于所述元件的高度的百分比(如 “5%”),或在相对 ​​百分比到母体高度(如 “5%对”)。

通过支持多个属性<翻译>,看动画类引用(其中,所有XML属性由该元素inherrited)。

<旋转>
一个旋转动画。代表 RotateAnimation

属性:

机器人:fromDegrees
浮动。开始角度位置,以度。
机器人:toDegrees
浮动。结束角度位置,以度。
机器人:pivotX
浮动或百分比。旋转中心的X坐标。表示任一:在相对 ​​于对象的左边缘像素(例如 “5”),在相对 ​​于对象的左边缘百分比(如 “5%”),或在相对 ​​百分比到父容器的左边缘(如 “ 5%p“)。
机器人:pivotY
浮动或百分比。旋转中心的Y坐标。表示任一:在相对 ​​于对象的顶部边缘像素(例如 “5”),在相对 ​​于物体的顶部边缘百分比(如 “5%”),或在相对 ​​百分比到父容器的顶部边缘(如 “ 5%p“)。

通过支持多个属性<旋转>,看动画类引用(其中,所有XML属性由该元素inherrited)。

例:
在保存XML文件 RES /动画/ 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>

此应用程序代码将动画应用到的ImageView和启动动画:

ImageView image =  ( ImageView ) findViewById ( R . id . image ); Animation hyperspaceJump =  AnimationUtils . loadAnimation ( this , R . anim . hyperspace_jump ); image . startAnimation ( hyperspaceJump );
也可以看看:
  • 2D图形:吐温动画

插值

内插器是在XML中,影响变化的动画中的速率限定的动画改性剂。这使得现有的动画效果加速,减速,反复,反弹等。

内插器被施加到与一个动画元素机器人:内插属性,它的值是内插器的资源的参考。

在Android的所有可用插值是的子类插补类。对于每个插值类,Android包含可以为了利用内插器应用到动画引用公共资源的android:插补属性。下表指定要用于每个插补器资源:

插补类 资源ID
AccelerateDecelerateInterpolator @android:动画/ accelerate_decelerate_interpolator
AccelerateInterpolator @android:动画/ accelerate_interpolator
AnticipateInterpolator @android:动画/ anticipate_interpolator
AnticipateOvershootInterpolator @android:动画/ anticipate_overshoot_interpolator
BounceInterpolator @android:动画/ bounce_interpolator
CycleInterpolator @android:动画/ cycle_interpolator
DecelerateInterpolator @android:动画/ decelerate_interpolator
LinearInterpolator @android:动画/ linear_interpolator
OvershootInterpolator @android:动画/ overshoot_interpolator

这里是你如何运用这些与一个机器人:插值属性:

<设置 机器人:插补器= “@android:动画/ accelerate_interpolator” >   ... </设置>

自定义插值

如果你不满意的平台(上表中列出)提供的内插器,您可以创建修改的属性自定义资源的内插器。例如,可以调整加速度率为AnticipateInterpolator,或调整为周期数CycleInterpolator。为了做到这一点,你需要在一个XML文件来创建自己的内插器的资源。

文件位置:
RES /阿尼姆/文件名​​的.xml
文件名 ​​将被用作资源ID。
编译的资源数据类型:
资源指针到相应的内插的对象。
资源引用:
在XML: @ [:]动画/文件名
句法:
<?xml的version = "1.0" encoding = "utf-8" ?> < InterpolatorName  xmlns:android = "http://schemas.android.com/apk/res/android"   android: attribute_name = " value "   />

如果不应用任何属性,那么你就会插功能完全一样的平台(表上面列出)提供的。

内容:
请注意,每个 插补实现,XML定义的时候,小写开头的名字。

<accelerateDecelerateInterpolator>
变化率开始,并结束缓慢但通过中间加速。

无属性。

<accelerateInterpolator>
变化率开始慢慢出来,再加速。

属性:

安卓因素
浮动。加速度(默认为1)。
<anticipateInterpolator>
这种变化开始向后甩,然后向前。

属性:

机器人:紧张
浮动。张力的量应用(默认为2)。
<anticipateOvershootInterpolator>
改变落后开始,甩向前和过冲的目标值,然后稳定在最终值。

属性:

机器人:紧张
浮动。张力的量应用(默认为2)。
机器人:extraTension
浮动。由量乘以张力(默认为1.5)。
<bounceInterpolator>
变化反弹在末端。

无属性

<cycleInterpolator>
将重复周期的指定数目的动画。变化率遵循正弦模式。

属性:

机器人:周期
。周期(默认为1)的数目。
<decelerateInterpolator>
变化率迅速开出,然后减速。

属性:

安卓因素
浮动。减速度(默认为1)。
<linearInterpolator>
变化率是恒定的。

无属性。

<overshootInterpolator>
这种变化甩向前冲过最后一个值,然后回来。

属性:

机器人:紧张
浮动。张力的量应用(默认为2)。
例:

在保存XML文件RES /动画/ 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"   />

这个动画XML将运用插值:

<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"  />

帧动画

在XML中定义的动画,显示图像在顺序的序列(如薄膜)。

文件位置:
水库/抽拉/文件名​​的.xml
文件名 ​​将被用作资源ID。
编译的资源数据类型:
资源指向一个 AnimationDrawable
资源引用:
在Java: 。R.drawable
在XML: @ [:]绘文件名
句法:
<?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>
内容:
<动画列表>
必需的。这必须是根元素。包含一个或多个 的<item>元素。

属性:

机器人:单稳
布尔。“真”,如果你想执行一次动画;“假”循环动画。
<项目>
动画的单帧。必须是一个孩子 <动画列表>元素。

属性:

机器人:可绘制
绘制资源。可绘制用于 ​​此框架。
机器人:时间
。持续时间显示此框架,以毫秒为单位。
例:
在保存XML文件 RES /动画/ 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>
此应用程序代码将设置动画为背景的视图,然后播放动画:
ImageView rocketImage =  ( ImageView ) findViewById ( R . id . rocket_image ); rocketImage . setBackgroundResource ( R . drawable . rocket_thrust ); rocketAnimation =  ( AnimationDrawable ) rocketImage . getBackground () ; rocketAnimation . start () ;
也可以看看:
  • 2D图形:帧动画

更多相关文章

  1. Android_自定义底部动画弹出pupopwindow
  2. Android可控图片旋转
  3. Android(安卓)TextView 一些字体颜色、大小设置属性
  4. Android(安卓)滑动关闭Activity实现
  5. Android(安卓)补间动画
  6. android 动画一 (帧动画FrameAnimation)
  7. Qt for Android程序沉浸式启动页面(去除标题栏、去除黑屏、保留后
  8. 详解 Android(安卓)Views 元素的 layout_weight 属性
  9. Android中自定义样式(style)与主题(theme)

随机推荐

  1. Android中调用其它应用
  2. android keytool 不是内部命令或外部命令
  3. Android Activity启动模式分析
  4. android开发仿IOS滑动开关
  5. Android(安卓)开发环境下载地址 -- 百度
  6. android_防止被反编译 混淆
  7. Android Studio 如何引用aar包
  8. Google android初级开发之 : android 编
  9. Android 设定横屏,禁止屏幕旋转,Activity重
  10. 关于Android Studio 模拟器“ANDROID SDK