枫叶「零碎记录」

那些年一起学过的Android动画(二)

字数统计: 1k阅读时长: 4 min
2016/08/26 Share

场景动画


  1. LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,当他的子元素出场的时候都会具有这种动画,ListView上用的多,LayoutAnimation也是一个View动画。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml version="1.0" encoding="utf-8"?>
    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animationOrder="normal"
    android:delay="0.3" android:animation="@anim/anim_item"/>
    //--- delay 表示动画开始的时间延迟,比如子元素入场动画的时间周期为300ms,
    //那么0.5表示每个子元素都需要延迟150ms才开始播放入场动画。
    //--- animationOrder 表示子元素的动画的顺序,有三种选项:
    //normal(顺序显示)、reverse(逆序显示)和random(随机显示)。
    //---1. android:animation 为子元素指定具体的入场动画
    //----------------------
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:shareInterpolator="true">
    <alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />
    <translate
    android:fromXDelta="300"
    android:toXDelta="0" />
    </set>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//--- 第一种方法、为需要的ViewGroup指定android:layoutAnimation属性
//---这样ViewGroup的子View就有出场动画了
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layoutAnimation="@anim/anim_layout"/>
//
//--- 第二种方法、通过LayoutAnimationController来实现
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listview.setLayoutAnimation(controller);
  1. Activity/Fragment的切换效果
    在startActivity(Intent)或finish()之后调用overridePendingTransition(int enterAnim,int exitAnim)方法。
    Fragment也可以添加切换动画,通过FragmentTransaction中的
    setCustomAnimations()方法来添加;需考虑兼容性使用View动画,属性动画是API11新引入的;

插值器和估值器


  1. 时间插值器(TimeInterpolator)的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比,系统预置的有LinearInterpolator(线性插值器:匀速动画)AccelerateDecelerateInterpolator(加速减速插值器:动画两头慢中间快)DecelerateInterpolator(减速插值器:动画越来越慢)
  2. 估值器(TypeEvaluator)的作用是根据当前属性改变的百分比来计算改变后的属性值。系统预置有IntEvaluator 、FloatEvaluator 、ArgbEvaluator。
1
2
3
4
5
6
7
8
9
10
11
12
/**
* @param fraction 表示开始和结束值之间的比例
* @param startValue 开始值
* @param endValue 结束值
* @return
*/
public class IntEvaluator implements TypeEvaluator<Integer> {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int)(startInt + fraction * (endValue - startInt));
}
}
  1. 插值器和估值器除了系统提供之外,我们还可以自定义。自定义插值器需要实现Interpolator或者TimeInterpolator;自定义估值器算法需要实现TypeEvaluator。

自定义动画一般用的比较少,基础篇就西先不介绍了!不过需要先了解矩阵和Camera,做AE动画3D等技术人员肯定会知道哒!

##总结

参考Android开发艺术探索

使用动画的注意事项

  1. 使用帧动画时,当图片数量较多且图片分辨率较大的时候容易出现OOM,需注意,尽量避免使用帧动画。
  2. 使用无限循环的属性动画时,在Activity退出时即使停止,否则将导致Activity无法释放从而造成内存泄露。
  3. View动画是对View的影像做动画,并不是真正的改变了View的状态,因此有时候会出现动画完成后View无法隐藏(setVisibility(View.GONE)失效),这时候调用view.clearAnimation()清理View动画即可解决。
  4. 不要使用px,使用px会导致不同设备上有不同的效果。
  5. View动画是对View的影像做动画,View的真实位置没有变动,也就导致点击View动画后的位置
  6. 触摸事件不会响应,属性动画不存在这个问题。
  7. 使用动画的过程中,使用硬件加速可以提高动画的流畅度。
  8. 动画在3.0以下的系统存在兼容性问题,特殊场景可能无法正常工作,需做好适配工作。

还有一些动画的高级用法比如贝赛尔曲线等自行探索一下更有趣哈

有问题可以来我博客或者简书反馈,谢谢大家的停留在这里时间

CATALOG
  1. 1. 场景动画
  2. 2. 插值器和估值器
  3. 3. ##总结