Animation特效——Tween Animation

来源:互联网 发布:linux查看当前用户身份 编辑:程序博客网 时间:2024/05/22 06:12

Animation特效——Tween Animation

Tween Animation 又被称为View Animation,译为补间动画,其作用就是在两个关键帧渐变过程中,通过一些算法补充渐变过程。既然又被称为View Animation,那么说明它只能作用于View对象,而且也仅能支持一部分的属性。我们下面来详细说明。


一、Tween Animation 支持的属性

Tween Animation 支持的属性有渐变、缩放、移动、旋转四种。它们可以以XML形式定义动画,当然也可以以代码实现。不过这里建议以XML实现。


二、XML定义动画属性

1、动画属性说明

1)alpha

渐变透明度动画效果

2)scale
渐变尺寸伸缩动画效果
3)translate
画面转换位置移动动画效果
4)rotate
画面转换旋转动画效果

2、属性语法说明

1)alpha语法
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="500"        android:fillAfter="true"        android:fromAlpha="1.0"        android:toAlpha="0.0" /></set>
duration
动画持续时间,单位毫秒,值越小动画速度越快
fillAfter
如果设置为true,控件动画结束时,将保持动画最后时的状态
fillBefore
如果设置为true,控件动画结束时,将还原到开始动画前的状态
fillEnable
同fillBefore
fromAlpha
开始时透明度
toAlpha
结束时透明度

2)scale语法
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <scale        android:duration="10000"        android:fromXScale="0.0"        android:fromYScale="0.0"        android:interpolator="@android:anim/decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:repeatCount="1"        android:repeatMode="reverse"        android:startOffset="0"        android:toXScale="1.5"        android:toYScale="1.5" /></set>
fromXScale,fromYScale
起始比例,0为不显示,1.0为正常
interpolator
指定动画插入器,取值部分后面将详细说明
pivotX,pivotY
动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始
repeatCount
动画重复的计数,动画将会执行该值+1次
repeatMode
动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。
startOffset
动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,
toXScale,toYScale
结束比例, 1.0为正常,大于1.0放大  

3)translate语法
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="10000"        android:fromXDelta="320"        android:fromYDelta="480"        android:toXDelta="0"        android:toYDelta="0" /></set>
fromXDelta,fromYDelta
起始X,Y座标 ,如果为100%的话,意思是本身百分比,如果为100%p的话,意思是父组件的百分比
toXDelta, toYDelta
结束X,Y的座标

4)rotate语法
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate        android:duration="10000"        android:fromDegrees="300"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="10%"        android:pivotY="100%"        android:toDegrees="-360" /></set>
fromDegrees
动画起始的角度   
toDegrees
动画结束的角度,正代表顺时针 

3、语法补充说明

1)set语法
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_decelerate_interpolator"    android:shareInterpolator="true" >   ...</set>
set
一个动画容器,管理多个动画的群组
interpolator
代表一个插值器资源,可以引用系统自带插值器资源,也可以用自定义插值器资源,默认值是匀速插值器
shareInterpolator
代表<set>里面的多个动画是否要共享插值器,默认值为true,即共享插值器,如果设置为false,那么<set>的插值器就不再起作用,我们要在每个动画中加入插值器。

2)interpolator插值器特效
系统特效说明:
Interpolator对象资源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快速到达终点并超出一小步最后回到终点

自定义插入器:
我们先来看一下几种常见的插值器可调整的属性:
<accelerateDecelerateInterpolator> 
none
<accelerateInterpolator> 
android:factor 浮点值,加速速率,默认为1
<anticipateInterploator> 
android:tension 浮点值,起始点后退的张力、拉力数,默认为2
<anticipateOvershootInterpolator> 
android:tension 拉力的倍数,默认为1.5(2  * 1.5)
<bounceInterpolator>  none
none
<cycleInterplolator> 
android:cycles 整数值,循环的个数,默认为1
<decelerateInterpolator> 
android:factor 浮点值,减速的速率,默认为1
<linearInterpolator> 
none
<overshootInterpolator> 
android:tension浮点值,超出终点后的张力、拉力,默认为2

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

4、调用XML动画效果

定义好动画的XML文件后,可以通过类似下面的代码对指定View应用动画。
ImageView spaceshipImage = (ImageView)findViewById(R.id.spaceshipImage);Animation hyperspaceJumpAnimation=AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);spaceshipImage.startAnimation(hyperspaceJumpAnimation);

三、JavaCode定义动画属性

1、动画属性说明

1)AlphaAnimation
渐变透明度动画效果
2)ScaleAnimation
渐变尺寸伸缩动画效果
3)TranslateAnimation
画面转换位置移动动画效果
4)RotateAnimation
画面转换旋转动画效果

2、语法及用法说明

举例说明如下:

AlphaAnimation animation = new AlphaAnimation(1, 0); animation.setDuration(2000);animation.setRepeatCount(2);animation.setFillAfter(true);animation.setStartOffset(10);image.setAnimation(animation);


3、通过AnimationSet应用多个动画

AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等。
举个例子说明一下:
先播放1动画,然后同时播放动画2,3,4,最后播放5
AnimatorSet bouncer = new AnimatorSet();bouncer.play(anim1).before(anim2);bouncer.play(anim2).with(anim3);bouncer.play(anim2).with(anim4)bouncer.play(anim5).after(amin2);animatorSet.start();






0 0
原创粉丝点击