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
.
- Tween animation: Creates an animation by performing a series of transformations on a single image with an
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
, orAnimatorSet
.
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:
- 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 an
AnimatorSet
object, 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 theAnimatorSet
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();
SEE ALSO:
-
- Property Animation
- API Demos for 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:
- 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 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);
SEE ALSO:
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 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 theandroid:interpolator
attribute. 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 the android:interpolator
attribute:
<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 the AnticipateInterpolator
, or adjust the number of cycles for the CycleInterpolator
. 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
Interpolator
implementation, when defined in XML, begins its name in lowercase.
EXAMPLE:
-
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).
-
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:
- EXAMPLE:
-
-
XML file saved at
-
<?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()
;
res/anim/rocket.xml
:
SEE ALSO:
-
-
动画资源可以定义两种类型的动画中的一者:
-
物业动画
-
通过在时间与一组时间修改对象的属性值创建一个动画的
动画师
。
观看动画
-
有两种类型的动画,你可以认为动画框架做的:
- 吐温动画:通过用一个单一的图像执行一系列转换创建动画
动画
- 帧动画:或通过示出,以便与一个图像的序列创建动画
AnimationDrawable
。
- 吐温动画:通过用一个单一的图像执行一系列转换创建动画
物业动画
在XML中定义的动画来修改目标对象的性质,例如背景颜色或阿尔法值,在设定的时间量。
-
文件位置:
-
RES /动画/ 文件名 的.xml
文件名 将被用作资源ID。
编译的资源数据类型:
-
资源指针
ValueAnimator
,ObjectAnimator
或AnimatorSet
。
资源引用:
-
在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>
。您可以将动漫元素结合在一起里面<设置>
元素,包括其他<设置>
元素。
内容:
- 例:
-
在保存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 ();
也可以看看:
观看动画
视图动画框架支持吐温和逐帧动画,可以在两个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>
该文件必须有一个根元素:要么是
<阿尔法>
,<规模>
,<翻译>
,<旋转>
或<设置>
元素持有其他动画元素的组(或组)(甚至嵌套的<设置>
元素)。
内容:
- 例:
-
在保存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 );
也可以看看:
插值
内插器是在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定义的时候,小写开头的名字。
例:
-
在保存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>
内容:
- 例:
-
-
在保存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 ()
;
RES /动画/ rocket.xml
:
也可以看看:
-
-
通过在时间与一组时间修改对象的属性值创建一个动画的