网友通过本文主要向大家介绍了android animation,android中animation,android setanimation,view animation,viewanimationutils等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android动画三部曲之一 View Animation & LayoutAnimation
Tween动画
Tween动画又称补间动画。通过对view的位置、大小、透明度、角度的改变来实现动画效果。
补间动画的基类是Animation。我们通常使用它的直接子类RotateAnimation、TranslateAnimation、ScaleAnimation、AlphaAnimation。
补间动画可以通过xml进行定义(res/anim/xxx),然后通过AnimationUtils类进行加载;也可以通过完全代码进行设置。
XML语法介绍
<code class="hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <set android:interpolator="@[package:]anim/interpolator_resource" android:shareinterpolator="[" xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromalpha="float" android:toalpha="float"> <scale android:fromxscale="float" android:fromyscale="float" android:pivotx="float" android:pivoty="float" android:toxscale="float" android:toyscale="float"> <translate android:fromxdelta="float" android:fromydelta="float" android:toxdelta="float" android:toydelta="float"> <rotate android:fromdegrees="float" android:pivotx="float" android:pivoty="float" android:todegrees="float"> <set> ... </set> </rotate></translate></scale></alpha></set></code>
Animation类定义了很多常量和变量的初始值,比如:
public static final int INFINITE = -1;
public static final int RESTART = 1;
public static final int REVERSE = 2;
主要用到它的子类以及AnimationListener :
public static interface AnimationListener {
/**
* 动画开始的时候回调
*
* @param animation The started animation.
*/
void onAnimationStart(Animation animation);
/**
* 动画结束的时候回调。但是当设置动画重复次数为INFINITE的时候,该方法不会回调。
*
* @param animation The animation which reached its end.
*/
void onAnimationEnd(Animation animation);
/**
* 动画重复播放的时候回调
*
* @param animation The animation which was repeated.
*/
void onAnimationRepeat(Animation animation);
}
添加此监听器可以对动画做更多的操作。
插值器 – Interpolator
介绍动画之前,得先说说”插值器”。插值器的意思就是在播放动画的时候,改变播放的速率,可以使动画越来越快,或者越来越慢等。
常用的是一下九个插值器:
Baseinterpolator子类 | Resource ID | 描述 |
---|---|---|
AccelerateInterpolator | @android:anim/accelerate_interpolator | 加速变化(开始慢,越来越快) |
DecelerateInterpolator | @android:anim/decelerate_interpolator | 减速变化(开始快,越来越慢) |
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 先加速后减速(中间速度最快) |
LinearInterpolator | @android:anim/linear_interpolator | 线性均匀变化 |
OvershootInterpolator | @android:anim/overshoot_interpolator | 超出结尾的临界值,然后在缓慢回到结束值 |
AnticipateInterpolator | @android:anim/anticipate_interpolator | 先向相反的方向改变一点,然后在加速播放 |
AnticipateOvershootInterpolator | @android:anim/anitcipate_overshoot_interpolator | 先向相反的方向改变一点,然后在加速播放至超出结束值一点,然后在缓慢回到结束值 |
BounceInterpolator | @android:anim/bounce_interpolator | 动画快结束的时候,模拟球落地的回弹效果 |
CycleInterpolator | @android:anim/cycle_interpolator | 动画循环播放指定的次数 |
自定义Interpolator
一般来说,官方API给的这几个插值器就够实用了。不过还可以自定义Interpolator。可以简单的对系统的插值器进行一些参数值的修改:
公共XML属性及对应的方法
属性名称 | 对应的方法 | 描述 |
---|---|---|
android:duration | setDuration(long) | 动画持续的时间长度(单位是miliseconds) |
android:interpolator | setInterpolator(Interpolator) | 设置动画播放时的插值器 |
android:repeatCount | setRepeatCount(int) | 设置动画播放重复次数 |
android:repeatMode | setRepeatMode(int) | 设置动画重复的方式(当repeat count>0时才有效) “reverse“(2) or “restart“(1) |
android:startOffset | setStartOffset(long) | 设置动画开始播放的延迟时间 |
android:fillAfter | setFillAfter(boolean) | 设置为true时,视图会停留在动画结束的状态。 |
android:fillBefore | setFillBefore(boolean) | 默认值是true,视图会停留在动画开始的状态 |
android:fillEnable | setFillEnable(boolean) | 默认值是false。如果是true,动画将会应用fillBefore值;否则,fillBefore的值会被忽略,transformation会在动画结束的时候被应用。 |
android:detachWallpaper | setDetachWallpaper(boolean) | 默认值是false。如果为true,并且动画窗体有一个壁纸的话,那么动画只会应用给window,墙纸是静态不动的 |
android:zAdjustment | setZAdjustment(int) | 允许在动画播放期间,调整播放内容在Z轴方向的顺序。”top“(1) or “normal“(0) or “bottom“(-1) |
android:zAdjustment:允许在动画播放期间,调整播放内容在Z轴方向的顺序:
normal(0):正在播放的动画内容保持当前的Z轴顺序, top(1):在动画播放期间,强制把当前播放的内容放到其他内容的上面; bottom(-1):在动画播放期间,强制把当前播放的内容放到其他内容之下ScaleAnimation – 缩放动画
XML属性名称 | 描述 |
---|---|
android:fromXScale | 动画起始时,X轴坐标的伸缩尺寸。0.0表示收缩到没有。1.0表示正常没伸缩。>1.0表示放大。<1.0表示收缩。 |
android:toXScale | 动画结束时X轴坐标的伸缩尺寸 |
android:fromYScale | 动画起始时Y轴坐标的伸缩尺寸 |
android:toYScale | 动画结束时Y轴坐标的伸缩尺寸 |
android:pivotX | 缩放动画作用点在X轴方向上的位置。android:pivotX=”50”表示绝对定位,相对于零点偏移50 –> Animation.ABSOLUTE android:pivotX=”50%”表示相对控件本身 –> Animation.RELATE_TO_SELF android:pivotX=”50%p”表示相对控件的父控件 –> Animation.RELATE_TO_PARENT |
android:pivotY | 缩放动画作用点在Y轴方向上的位置 |
xml定义缩放动画