Android的两种Animation模式:
- Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;
- Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。
Android的四种Animation类型:
- alpha 渐变透明度动画效果
- scale 渐变尺寸伸缩动画效果
- translate 画面转换位置移动动画效果
- rotate 画面转移旋转动画效果
公共属性
1. interpolator:插入器,用来定义动画的加速曲线
对应设置Interpolator的源码:
/**
* Sets the acceleration curve for this animation. The interpolator is loaded as
* a resource from the specified context.
*
* @param context The application environment
* @param resID The resource identifier of the interpolator to load
* @attr ref android.R.styleable#Animation_interpolator
*/
public void setInterpolator(Context context, int resID) {
setInterpolator(AnimationUtils.loadInterpolator(context, resID));
}
/**
* Sets the acceleration curve for this animation. Defaults to a linear
* interpolation.
*
* @param i The interpolator which defines the acceleration curve
* @attr ref android.R.styleable#Animation_interpolator
*/
public void setInterpolator(Interpolator i) {
mInterpolator = i;
}
Interpolator定义了动画的变化率,支持基本动画效果(alpha、rotate、scale、translate)的加速、减速、重复。interface Interpolator extends TimeInterpolator:
public interface Interpolator extends TimeInterpolator {
// A new interface, TimeInterpolator, was introduced for the new android.animation
// package. This older Interpolator interface extends TimeInterpolator so that users of
// the new Animator-based animations can use either the old Interpolator implementations or
// new classes that implement TimeInterpolator directly.
}
TimeInterpolator接口:
public interface TimeInterpolator {
/**
* Maps a value representing the elapsed fraction of an animation to a value that represents
* the interpolated fraction. This interpolated value is then multiplied by the change in
* value of an animation to derive the animated value at the current elapsed animation time.
*
* @param input A value between 0 and 1.0 indicating our current point
* in the animation where 0 represents the start and 1.0 represents
* the end
* @return The interpolation value. This value can be more than 1.0 for
* interpolators which overshoot their targets, or less than 0 for
* interpolators that undershoot their targets.
*/
float getInterpolation(float input);
}
2.fillEnabled、fillBefore、fillAfter
fillBefore:是指动画结束时画面停留在此动画的第一帧;fillAfter:是指动画结束时画面停留在此动画的最后一帧。fillEnabled属性值设置为true时考虑fillBefore属性,设置为false时不考虑fillBefore属性。
/**
* If fillEnabled is true, the animation will apply the value of fillBefore.
* Otherwise, fillBefore is ignored and the animation
* transformation is always applied until the animation ends.
*
* @param fillEnabled true if the animation should take the value of fillBefore into account
* @attr ref android.R.styleable#Animation_fillEnabled
*
* @see #setFillBefore(boolean)
* @see #setFillAfter(boolean)
*/
public void setFillEnabled(boolean fillEnabled) {
mFillEnabled = fillEnabled;
}
/**
* If fillBefore is true, this animation will apply its transformation
* before the start time of the animation. Defaults to true if
* {@link #setFillEnabled(boolean)} is not set to true.
* Note that this applies when using an {@link
* android.view.animation.AnimationSet AnimationSet} to chain
* animations. The transformation is not applied before the AnimationSet
* itself starts.
*
* @param fillBefore true if the animation should apply its transformation before it starts
* @attr ref android.R.styleable#Animation_fillBefore
*
* @see #setFillEnabled(boolean)
*/
public void setFillBefore(boolean fillBefore) {
mFillBefore = fillBefore;
}
/**
* If fillAfter is true, the transformation that this animation performed
* will persist when it is finished. Defaults to false if not set.
* Note that this applies to individual animations and when using an {@link
* android.view.animation.AnimationSet AnimationSet} to chain
* animations.
*
* @param fillAfter true if the animation should apply its transformation after it ends
* @attr ref android.R.styleable#Animation_fillAfter
*
* @see #setFillEnabled(boolean)
*/
public void setFillAfter(boolean fillAfter) {
mFillAfter = fillAfter;
}
默认fillBefore属性值为true,fillAfter和fillEnabled属性值为false。(设置在alpha、translate、scale、rotate节点无效,设置在set节点的属性)
3.duration:动画的持续时间,不能为负数。
4.startOffset:定义一个动画相对于整组动画的开始时间。
/**
* When this animation should start relative to the start time. This is most
* useful when composing complex animations using an {@link AnimationSet }
* where some of the animations components start at different times.
*
* @param startOffset When this Animation should start, in milliseconds from
* the start time of the root AnimationSet.
* @attr ref android.R.styleable#Animation_startOffset
*/
public void setStartOffset(long startOffset) {
mStartOffset = startOffset;
}
5.repeatCount:设置动画的重复次数。
6.repeatMode:定义动画重复的方式:重新开始(restart)或反向(reverse)。
7.zAdjustment:设置在动画播放期间播放内容在Z轴方向的顺序。normal:播放的动画内容保持当前的Z轴顺序;top:在动画播放期间,强制把当前播放的内容放到其他内容的上面;bottom:在动画播放期间,强制把当前播放的内容放到其他内容之下。
/**
* Requests that the content being animated be kept in its current Z
* order.
*/
public static final int ZORDER_NORMAL = 0;
/**
* Requests that the content being animated be forced on top of all other
* content for the duration of the animation.
*/
public static final int ZORDER_TOP = 1;
/**
* Requests that the content being animated be forced under all other
* content for the duration of the animation.
*/
public static final int ZORDER_BOTTOM = -1;
8.background:动画背景。
9.detachWallpaper:分离壁纸。
/**
* If detachWallpaper is true, and this is a window animation of a window
* that has a wallpaper background, then the window will be detached from
* the wallpaper while it runs. That is, the animation will only be applied
* to the window, and the wallpaper behind it will remain static.
*
* @param detachWallpaper true if the wallpaper should be detached from the animation
* @attr ref android.R.styleable#Animation_detachWallpaper
*/
public void setDetachWallpaper(boolean detachWallpaper) {
mDetachWallpaper = detachWallpaper;
}
差异属性:
一. alpha
1.fromAlpha:动画开始时透明度
2.toAlpha:动画结束时透明度
二. rotate
1.fromDegrees:动画开始时的角度
2.toDegrees:动画结束时的角度,可以大于360
说明:to - from > 0 顺时针旋转;to - from < 0 逆时针旋转。
3.pivotX:动画相对于物件的X坐标的开始位置
4.pivotY:动画相对于物件的Y坐标的开始位置
说明:以上两个属性值从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置。
三. scale
1.fromXScale:动画起始时X坐标上的伸缩尺寸
2.toXScale:动画结束时X坐标上的伸缩尺寸
3.fromYScale:动画起始时Y坐标上的伸缩尺寸
4.toYScale:动画结束时Y坐标上的伸缩尺寸
5.pivotX:同上rotate。
6.pivotY:同上rotate
四. translate
1.fromXDelta:动画起始时X坐标上的位置
2.toXDelta:动画结束时X坐标上的位置
3.fromYDelta:动画起始时Y坐标上的位置
4.toYDelta:动画结束时Y坐标上的位置