通过对view的内容进行一系列的图形变换(包括平移、缩放、旋转、改变透明度)来实现动画效果动画效果的实现可以采用XML来做,也可以采用编码来做。
Tween动画有4中类型:透明度、缩放、旋转、平移
Android自带的几种动画插入器: AccelerateInterpolator 加速,开始时慢中间加速 DecelerateInterpolator 减速,开始时快然后减速 AccelerateDecelerateInterolator 先加速后减速,开始结束时慢,中间加速 AnticipateInterpolator 反向,先向反向方向改变一段在加速播放 AnticipateOvershootInterpolator 反向加超越,先反向相反方向改变,再加速播放,会超出目的值然后缓慢一移动值目的的值 BounceInterpolator 跳跃,快到目的值时会跳跃,如目的值100,后面的值一次可能为85,77,70,80,90,100 CycleLinterpolator 循环,动画循环一定次数, 值的改变为一次正弦函数:Math.sin(2*mCycle*Math.Pl*input) LinearrInterpolator 线性,线性均匀改变 OvershottInterpolator 超越最后超出目的值然后缓慢改变到目的值
抽象类 Animation 是一个实现 AndroidUI 界面动画效果的API,Animation是补间动画的基类,它的直接子类:
AlphaAnimation,
RotateAnimation,
ScaleAnimation,
TranslateAnimation,
AnimationSet
提供了一系列的动画效果,可以进行淡入淡出、旋转、缩放、动画集等,这些效果可以应用在绝大多数的控件中。
一、透明度动画:
方式一:代码方式实现:
Animation alphaAnim = new AlphaAnimation(1, (float) 0.1);
alphaAnim.setDuration(3000);
/* 动画结束后是否保持当前位置 */
alphaAnim.setFillAfter(false);
tweenAlphaGroupLl.startAnimation(alphaAnim);
方式二:xml方式实现:
1:anim目录下定义 动画xml tween_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fillAfter="false"
android:fromAlpha="1.0"
android:repeatCount="2"
android:toAlpha="0.1" />
</set>
2:加载xml动画
Animation alphAnim = AnimationUtils.loadAnimation(context, R.anim.tween_alpha);
tweenAlphaGroupLl.startAnimation(alphAnim);
二、旋转动画
主要属性及说明:
repeatCount 重复次数
fromDegrees 为动画起始时物件的角度
当角度为负数 - 表示逆时针旋转
当角度为证书 - 表示顺时针旋转
(负数fromDegrees——toDegrees正数:顺时针旋转)
(负数fromDegrees——toDegrees负数:逆时针旋转)
(正数fromDegrees——toDegrees正数:顺时针旋转)
(正数fromDegrees——toDegrees负数:逆时针旋转)
toDegrees属性为动画结束时物件旋转的角度可以大于360度
pivotX,pivotY 为动画相对于物件的X、Y坐标的开始位.
说明:以上两个属性值从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置。
实例:
方式一:代码方式实现:
Animation rotateAnim = new RotateAnimation(0, 45);
rotateAnim.setDuration(3000);
/* 动画结束后时否保持动画结束的状态 */
rotateAnim.setFillAfter(false);
groupLl.startAnimation(rotateAnim);
方式二:xml方式实现:
1:anim目录下定义 动画xml tween_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fillAfter="false"
android:fromDegrees="0"
android:repeatCount="2"
android:toDegrees="45" />
</set>
2:加载xml动画
Animation rotateAnim = AnimationUtils.loadAnimation(context, R.anim.tween_rotate);
groupLl.startAnimation(rotateAnim);
三、缩放动画
主要属性及说明:
fromXScale(浮点型)属性为动画起始时X坐标上的缩放尺寸
fromYScale(浮点型)属性为动画起始时Y坐标上的缩放尺寸
toXScale(浮点型) 属性为动画结束时X坐标上的缩放尺寸
toYScale(浮点型) 属性为动画结束时Y坐标上的缩放尺寸
说明: 以上四种属性值
0.0表示收缩到没有
1.0表示正常无缩放
值小于1.0表示收缩
值大于1.0表示放大
pivotX(浮点型) 属性为动画相对于物件的X坐标的开始位置
pivotY(浮点型) 属性为动画相对于物件的Y坐标的开始位置
说明:
以上两个属性值从0%-100%中取值50%为物件的X或Y方向坐标上的中点位置
duration(长整型)属性为动画持续时间。说明: 时间以毫秒为单位
fillAfter(布尔型)属性当设置为true,该动画转化在动画结束后被应用
实例:
方式一:代码方式实现:
Animation rotateAnim = new ScaleAnimation(0.5f, 1f, 0.5f, 1f);
rotateAnim.setDuration(3000);
/* 动画结束后时否保持动画结束的状态 */
rotateAnim.setFillAfter(false);
groupLl.startAnimation(rotateAnim);
方式二:xml方式实现:
1:anim目录下定义 动画xml tween_scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="3000"
android:fillAfter="false"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:repeatCount="2"
android:toXScale="1"
android:toYScale="1" />
</set>
2:加载xml动画
Animation rotateAnim = AnimationUtils.loadAnimation(context, R.anim.tween_scale);
groupLl.startAnimation(rotateAnim);
四、平移动画
方式一:代码方式实现:
Animation translateAnimation = new TranslateAnimation(0, 250, 0, 250);
translateAnimation.setDuration(3000);
/* 动画结束后时否保持动画结束的状态 */
translateAnimation.setFillAfter(false);
/* 添加插入器 */
translateAnimation.setInterpolator(context, android.R.anim.cycle_interpolator);
groupLl.startAnimation(translateAnimation);
方式二:xml方式实现:
1:anim目录下定义 动画xml tween_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="3000"
android:fillAfter="false"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:repeatCount="2"
android:toXDelta="250"
android:toYDelta="250" />
</set>
2:加载xml动画
/* 添加了先加速后减速插值器 */
Animation rotateAnim = AnimationUtils.loadAnimation(context, R.anim.tween_translate);
groupLl.startAnimation(rotateAnim);
五、AnimationSet 实现组合动画
方式一:代码方式实现:
/* 参数如果使用自己的插值器传false,否则true */
AnimationSet animSet = new AnimationSet(true);
animSet.addAnimation(AnimationUtils.loadAnimation(context, R.anim.tween_translate));
animSet.addAnimation(AnimationUtils.loadAnimation(context, R.anim.tween_scale));
animSet.addAnimation(AnimationUtils.loadAnimation(context, R.anim.tween_rotate));
animSet.addAnimation(AnimationUtils.loadAnimation(context, R.anim.tween_alpha));
animSet.setInterpolator(context, android.R.anim.anticipate_interpolator);
groupLl.startAnimation(animSet);
方式二:xml方式实现:
1:anim目录下定义 动画xml tween_set_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fillAfter="false"
android:fromAlpha="1.0"
android:repeatCount="2"
android:toAlpha="0.1" />
<rotate
android:duration="3000"
android:fillAfter="false"
android:fromDegrees="0"
android:repeatCount="2"
android:toDegrees="45" />
<scale
android:duration="3000"
android:fillAfter="false"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:repeatCount="2"
android:toXScale="1"
android:toYScale="1" />
<translate
android:duration="3000"
android:fillAfter="false"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:repeatCount="2"
android:toXDelta="250"
android:toYDelta="250" />
</set>
2:加载xml动画
Animation animSet = AnimationUtils.loadAnimation(context, R.anim.tween_set_anim);
groupLl.startAnimation(animSet);
Demo源码详见gitlab:https://github.com/BianJianZhou/BAnimation
运行效果:
Demo 下载链接:BAnimation
持续更新中 ing...