前言
补间动画:给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。只能应用于View
对象,而且只支持一部分属性,主要分为四大类移动,缩放,旋转,透明。
1. 使用方式
- 在XML资源中定义,使用
AnimationUtils.loadAnimation(Context, int)
方式加载duration
,属性为动画持续时间,时间以毫秒为单位fillAfter
,当设置为true
,该动画转化在动画结束后状态fillBefore
,当设置为true
,该动画转化在动画开始前状态interpolator
,指定一个动画的插入器repeatCount
,动画的重复次数repeatMode
,定义重复的行为,"restart"
和"reverse"
startOffset
,动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
- 定义
Animation
的子类TranslateAnimation
,画面转换位置移动动画效果ScaleAnimation
,渐变尺寸伸缩动画效果RotateAnimation
,画面转移旋转动画效果AlphaAnimation
,渐变透明度动画效果AnimationSet
,动画效果集合
2. TranslateAnimation
主要配置
fromXDelta
,属性为动画起始时X坐标上的位置,直接定义或者使用百分比toXDelta
,属性为动画结束时X坐标上的位置,直接定义或使用百分比fromYDelta
,属性为动画起始时Y坐标上的位置,直接定义或使用百分比toYDelta
,属性为动画结束时Y坐标上的位置,直接定义或使用百分比
百分比可以是"100%“,表示自身的100%,也就是从View
自己的位置开始。也可以表示为"80%p”,表示父层View
的80%,是以它父层View
为参照的。
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%"
android:duration="3000" />
代码实现
Animation animation = new TranslateAnimation(0, 0, Animation.RELATIVE_TO_SELF, 1.0f,
0, 0, 0, 0);
animation.setDuration(3000);
XType
和YType
Animation.ABSOLUTE // 绝对值
Animation.RELATIVE_TO_SELF // 相对于自身位置
Animation.RELATIVE_TO_PARENT // 相对于父控件位置
3. ScaleAnimation
主要配置
pivotX
,动画相对于物件的X坐标的开始位置,直接定义或使用百分比0%~100%pivotY
,动画相对于物件的Y坐标的开始位置,直接定义或使用百分比0%~100%fromXScale
,动画起始时X坐标上的伸缩尺寸toXScale
,动画结束时X坐标上的伸缩尺寸fromYScale
,动画起始时Y坐标上的伸缩尺寸toYScale
,动画结束时Y坐标上的伸缩尺寸
0.0表示收缩到没有,1.0表示正常无伸缩,值小于1.0表示收缩,值大于1.0表示放大
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1"
android:toXScale="0.5"
android:fromYScale="1"
android:toYScale="0.5"
android:duration="3000" />
代码实现
Animation animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
4. RotateAnimation
主要配置
pivotX
,动画相对于物件的X坐标的开始位置,直接定义或使用百分比0%~100%pivotY
,动画相对于物件的Y坐标的开始位置,直接定义或使用百分比0%~100%fromDegrees
,为动画起始时物件的角度toDegrees
,属性为动画结束时物件旋转的角度
当角度为负数表示逆时针旋转,当角度为正数表示顺时针旋转。
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="3000" />
代码实现
Animation animation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
5. AlphaAnimation
主要配置
fromAlpha
,属性为动画起始时透明度toAlpha
,属性为动画结束时透明度
1为不透明,0为完全透明
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="3000" />
代码实现
Animation animation = new AlphaAnimation(1.0f, 0f);
animation.setDuration(3000);
6. AnimationSet
AnimationSet
为动画集合,可同时运行多个动画。
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000">
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="0.5"
android:toXScale="1"
android:fromYScale="0.5"
android:toYScale="1" />
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
</set>
代码实现
AnimationSet animation = new AnimationSet(true);
Animation alphaAnim = new AlphaAnimation(0f, 1.0f);
animation.addAnimation(alphaAnim);
Animation scaleAnim = new ScaleAnimation( 0.5f, 1.0f, 0.5f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.addAnimation(scaleAnim);
animation.setDuration(3000);
7. 控制动画
开始动画
- 调用
View.startAnimation(Animation)
方法开始。 - 调用
View.setAnimation(Animation)
和Animation.start()
方法开始。
取消动画
- 调用
View.clearAnimation()
方法。 - 调用
Animation.cancel()
方法。
在View
的startAnimation(Animation)
方法中,设置了新的Animation
,并且刷新界面。
public void startAnimation(Animation animation) {
animation.setStartTime(Animation.START_ON_FIRST_FRAME);
setAnimation(animation);
invalidateParentCaches();
invalidate(true);
}
在ViewGroup
的dispatchDraw(Canvas)
方法里面调用drawChild(Canvas, View, long)
方法,该方法最终会调用Animation.getTransformation(long, Transformation, float)
使动画生效。
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
return child.draw(canvas, this, drawingTime);
}
boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
final Animation a = getAnimation();
if (a != null) {
more = applyLegacyAnimation(parent, drawingTime, a, scalingRequired);
}
}
private boolean applyLegacyAnimation(ViewGroup parent, long drawingTime,
Animation a, boolean scalingRequired) {
... ...
boolean more = a.getTransformation(drawingTime, t, 1f);
}
AnimationListener
是对Animation
设置监听器,在Animation
动画效果开始执行前,执行完毕和重复执行时可以触发监听器。
onAnimationStart(Animation animation)
,动画开始onAnimationEnd(Animation animation)
,动画结束onAnimationRepeat(Animation animation)
,动画重复
8. Interpolator
Interpolator
被用来修饰动画效果,定义动画的变化率。
AccelerateDecelerateInterpolator
,在动画开始与结束的地方速率改变比较慢,在中间的时候加速AccelerateInterpolator
,在动画开始的地方速率改变比较慢,然后开始加速AnticipateInterpolator
,开始的时候向后然后向前甩AnticipateOvershootInterpolator
,开始的时候向后然后向前甩一定值后返回最后的值BounceInterpolator
,动画结束的时候弹起CycleInterpolator
,动画循环播放特定的次数,速率改变沿着正弦曲线DecelerateInterpolator
,在动画开始的地方快然后慢LinearInterpolator
,以常量速率改变OvershootInterpolator
,向前甩一定值后再回到原来位置。
相关文章
Animation动画
帧动画
属性动画
组件动画
Transition动画