三种动画

来源:互联网 发布:天下无敌指标源码 编辑:程序博客网 时间:2024/05/16 06:32

动画分成三种:
1,属性动画(Property Animation

文件位置:
res/animator/filename.xml
资源参考:
In Java: R.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>

文件必须有一个根元素:  <set><objectAnimator>,<valueAnimator>之间的一种. 多种不同效果可以一起在<set> 元素中, 包括其它<set>元素。

元素:
<set>
包含其他动画元素的容器(<objectAnimator><valueAnimator>, or other <set> elements). 代表着一个动画集合.
你可以指定嵌套<set> 标签进一步动画分组. 每个<set>元素可以定义自己的排序属性

属性:
android:ordering
关键字. 特殊的晚点动画.
    在这组同时扮演动画。

ValueDescriptionsequentially

在这个集合中按顺序播放动画

together (default)
<objectAnimator>
一个特定对象的属性在一个特定的时间. 代表一个 ObjectAnimator.

属性:
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 objectAnimator element does not expose a target attribute, 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 callsetTarget() 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: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.
android:valueType
Keyword. Do not specify this attribute if the value is a color. The animation framework automatically handles color values
ValueDescriptionintTypeSpecifies that the animated values are integersfloatType (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: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.
android:valueType
Keyword. Do not specify this attribute if the value is a color. The animation framework automatically handles color values.
ValueDescriptionintTypeSpecifies that the animated values are integersfloatType (default)Specifies that the animated values are floatsEXAMPLE:
XML file saved at res/animator/property_animator.xml:

<setandroid: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:
AnimatorSetset=(AnimatorSet)AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);
set.setTarget(myObject);
set.start();
2,补间动画(Tween animation

FILE LOCATION:
res/anim/filename.xml
RESOURCE REFERENCE:
In Java: R.anim.filename
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<setxmlns: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 anAnimationSet.
attributes:
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. 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 the Animation class 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 pivotX andpivotY. 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 the Animation class 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 the Animation class 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 the Animation class reference (of which, all XML attributes are inherrited by this element).
EXAMPLE:
XML file saved at res/anim/hyperspace_jump.xml:

<setxmlns: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);
3,帧动画(Frame animation

FILE LOCATION:
res/drawable/filename.xml
RESOURCE REFERENCE:
In Java: R.drawable.filename
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>
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-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>
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();


0 0
原创粉丝点击