一、前言
为了使用户的交互更加流畅自然,动画也就成为了一个应用中必不可少的元素之一。在 Android 中常用的动画分类无外乎三种,最早的 帧动画 、补间动画 以及 3.0 之后加入的 属性动画,是它们组成了 Android 中各种炫酷亮眼的动画效果。这里是官方的动画相关开发文档链接。
二、动画
2.1 动画分类
Android中有三种动画,分别是补间动画(Tween Animation),帧动画(Frame Animation)和属性动画(Property Animation)。
2.2 补间动画
这类动画比较简单,一般就是平移、缩放、旋转、透明度,或者其组合,可以用代码或者xml文件的形式。
四个动画效果实现类:TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation、组合动画实现类:AnimationSet,对应的的XML标签为translate、 scale、 rotate、alpha、set,其中set里还可以放set,然后放在放置在res/anim/目录下。
2.2.1 平移(TranslateAnimation)
共有属性:
duration
表示这一次动画持续的时间
fillAfter
表示动画结束时,是否保持最后一帧的样子
fillBefore
表示动画结束时,是否保持第一帧的样子
repeatCount
表示动画循环的次数,默认为 0 次不循环,-1 为无限循环。
repeatMode
表示是循环的模式,reverse 是从一次动画结束开始,restart 是从动画的开始处循环
interpolator
是一个插值器资源,它可以控制动画的播放速度
shareInterpolator
表示是否与 set 中其他动画共享插值器,false为各自使用各自的插值器
平移动画属性:
fromXDelta
fromYDelta
起始时,X/Y 方向的位置
toXDelta
toYDelta
终止时,X/Y 方向的位置
这四个属性都支持同样的单位,是三种表达方式,浮点数、num% 和 num%p
- 浮点数 位置为 View 的左边距/上边距 + 此数值 正数为右,负数为左
- num% 位置为 View 的左边距/上边距 + View宽的百分之num 正数为右,负数为左
- num%p 位置为 View 的左边距/上边距 + 父容器的百分之num 正数为右,负数为左
动画效果:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="300"
android:toYDelta="300"
android:repeatCount="-1"
android:repeatMode="reverse"/>
Animation translateAnim = AnimationUtils.loadAnimation(this, R.anim.view_translate);
view.startAnimation(translateAnim);
2.2.2 缩放(ScaleAnimation)
属性:
fromXScale
fromYScale
代表缩放时,X/Y 坐标起始大小,浮点值,0.5代表自身的一半,2.0代表自身的两倍大小。
toXScale
toYScale
代表缩放时,X/Y 缩放结束时候大小。
pivotX
浮点数。在对象缩放时要保持不变的 X 坐标。
pivotY
浮点数。在对象缩放时要保持不变的 Y 坐标。
动画效果:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotX="0"
android:pivotY="0"
android:toXScale="1.0"
android:toYScale="1.0"
android:repeatCount="-1"
android:repeatMode="reverse"/>
Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.view_scale);
view.startAnimation(scaleAnim);
2.2.3 旋转(RotateAnimation)
属性:
fromDegrees
起始角度 单位度 浮点值
toDegrees
结尾角度 单位度 浮点值
pivotX
旋转中心点的 X 坐标,这个数值有三种表达方式
pivotY旋转中心点的 Y 坐标,这个数值有三种表达方式
- 纯数字 例如 20 ,代表相对于自身左边缘或顶边缘 + 20 像素
- num% 代表 相对于自身左边缘或顶边缘 + 自身宽 的百分之 num
- num%p 代表相对于自身左边缘或顶边缘 + 父容器 的百分之 num
动画效果:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toDegrees="1800" />
Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.view_rotate);
view.startAnimation(rotateAnim);
2.2.4 透明度(AlphaAnimation)
属性:
fromAlpha
表示动画开始时的透明度
toAlpha
表示动画结束时候的透明度
取值为[0.0,1.0],0代表完全透明,1代表不透明。
动画效果:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toAlpha="1" />
Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.view_scale);
view.startAnimation(scaleAnim);
2.2.5 组合(AnimationSet)
动画效果:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
android:fillAfter="true"
android:duration="15000">
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720" />
<scale
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotX="0%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="300"
android:toYDelta="300" />
</set>
java代码:
AnimationSet set = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.view_animation_set);
view.startAnimation(set);
//动画监听
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
//结束动画
//view.clearAnimation();
2.2.6 TimeInterpolator 时间插值器
TimeInterpolator
的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比。负责控制动画变化的速率,使得基本的动画效果能够以匀速、加速、减速、抛物线速率等各种速率变化。其实质是一个数学函数 y = f(x),定义域 x 属于 (0.0,1.0) 的 float 值,值域 y 也是 (0.0,1.0) 的 float 值