动画专题一

动画的分类


View动画(View Animation)


帧动画(Drawable Animation / Frame Animation)

属性动画(Property Animation)

Animation Resources



 

Property Animation


位置: res/animator/ filename .xml

<set
  adroid: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> android:ordering
Value Description
sequentially Play animations in this set sequentially
together (default) Play animations in this set at the same time.

<objectAnimator>
android:propertyName    表示属性值 alpha、scaleX、scaleY、rotation、 rotationX、rotationY、 translationX、 translationY、x、y String .   Required . The object's property to animate, referenced by its name. For example you canspecify   "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 resourcebycalling   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 hexadecimalnumbers(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 theproperty'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   动画多少ms开始 int . The amount of milliseconds the animation delays after   start()   is called. android:repeatCount   重复次数,-1表示无限循环 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" meansthat the animation is repeated once after the initial run of the animation, so the animation plays a total of two times.The defaultvalue is   "0" , which means no repetition. android:repeatMode   重复类型 nt . 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"   tohave the animation loop from the beginning each time. android:valueType    值的类型,float还是int 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>
android:valueTo  动画结束的值 float, int, or color .   Required . The value where the animation ends. Colors are represented as six digit hexadecimal numbers (forexample, #333333). android:valueFrom  动画开始的值 float, int, or color .   Required . The value where the animation starts. Colors are represented as six digit hexadecimal numbers (forexample, #333333). android:duration   动画持续时间 int . The time in milliseconds of the animation. 300ms is the default. android:startOffset  动画多少ms开始 int . The amount of milliseconds the animation delays after   start()   is called. android:repeatCount  重复次数,-1表示无限循环 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" meansthat the animation is repeated once after the initial run of the animation, so the animation plays a total of two times. The defaultvalue 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"   tohave the animation loop from the beginning each time. android:valueType   值的类型,float还是int 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:
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>

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


View Animation





Tween Animation



android:detachWallpaper  设置是否在壁纸上运行,只对设置了壁纸背景的窗口动画(window animation)有效。设为true,则动画只在窗口运行,壁纸背景保持不变

android:duration     动画持续的时间

android:fillAfter    true:动画结束保持状态   

android:fillBefore    true: 动画结束还原开始状态

android:fillEnabled     与 fillBefore  效果一致

android:interpolator     设定插值器

android:repeatCount     重复次数,-1表示无限循环

android:repeatMode     重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍

android:startOffset     表示动画在多少毫秒开始

android:zAdjustment     播放调整 Z 轴的位置,normal:正常的Z轴顺序  bottom:强制把当前播放的内容发在其他内容的下面  top:强制把当前播放的内容发在其他内容的上面

位置:    res/anim/ filename .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>


<scale>

android:fromXScale  起始X方向相对自身的缩放比例

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

android:toXScale  结尾X方向相对自身的缩放比例

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

android:fromYScale 起始Y方向相对自身的缩放比例

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

android:toYScale  结尾Y方向相对自身的缩放比例

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.



<alpha>

android:fromAlpha   动画开始的透明度 0---1   从透明到不透明

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

android:toAlpha     动画结束的透明度 0---1 从透明到不透明

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

<translate>

android:fromXDelta  起始点X坐标

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  结束点X坐标

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   起始点Y坐标

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  结束点X坐标

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

<rotate>

android:fromDegrees  开始旋转的角度位置,正数是顺时针,负数是逆时针

Float. Starting angular position, in degrees.

android:toDegrees  结束旋转到的角度

Float. Ending angular position, in degrees.

android:pivotX    旋转的X坐标

Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object's left edge(suchas "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  旋转的Y坐标

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

官方EXAMPLE:

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>

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

Custom interpolators


位置:res/anim/filename.xml


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


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






Frame animation


位置: res/drawable/ filename .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>

<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

res/drawable/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>

模仿JD下拉加载动画

资源可以下载京东的App解压在  r\j 下面


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="false">
    <item
        android:drawable="@mipmap/wj"
        android:duration="100"/>
    <item
        android:drawable="@mipmap/wk"
        android:duration="100"/>

    <item
        android:drawable="@mipmap/wl"
        android:duration="100"/>
</animation-list>


ImageView ivLoading = (ImageView) findViewById(R.id.loading);
 ivLoading.setBackgroundResource(R.drawable.loading);
 AnimationDrawable loadingDrawable = (AnimationDrawable) ivLoading.getBackground();
 loadingDrawable.start();







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值