7.3 Android Framework - PropertyAnimation

来源:互联网 发布:游龙网络有出什么游戏 编辑:程序博客网 时间:2024/06/06 19:45

API11 引入的特性, ValueAnimator, ObjectAnimator, AnimatorSet
语法

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:ordering=["sequentially" | "together"]>    <objectAnimator        android:duration="int"        android:propertyName="string"        android:valueFrom="float | int | color"        android:valueTo="float | int | color"        android:startOffset="int"        android:repeatCount="int"        android:repeatMode=["restart" | "reverse"]        android:valueType=["floatType" | "intType"] />    <animator        android:duration="int"        android:valueFrom="float | int | color"        android:valueTo="float | int | color"        android:startOffset="int"        android:repeatCount="int"        android:repeatMode=["restart" | "reverse"]        android:valueType=["floatType" | "intType"] /></set>
  • set对应AnimatorSet
    • android:ordering 一起播放还是顺序播放
  • objectAnimator对应ObjectAnimator
    • android:propertyName 属性动画作用对象的属性名
    • android:duration 动画时长
    • android:valueFrom 属性的起始值
    • android:valueTo 属性的结束值
    • android:startOffset 动画的延迟时间, 动画开始后延迟多少毫秒才真正动画
    • android:repeatCount 动画重复次数, -1是无限循环
    • android:repeatMode 动画重复的模式
    • android:valueType 表示android:propertyName指定的属性的类型
  • animator对应ValueAnimator

插值器和估值器
+ 插值器 根据时间流逝的百分比计算当前属性值改变的百分比
+ 估值器 根据插值器计算的属性值的百分比计算属性的实际值


属性动画的监听器

public static interface AnimatorListener {    void onAnimationStart(Animator a);    void onAnimationEnd(Animator a);    void onAnimationCancel(Animator a);    void onAnimationRepeat(Animator a);}// 每播放一帧都会调用一次public static interface AnimatorUpdateListener {    void onAnimationUpdate(ValueAnimator a);}

给任意属性做动画
- 给对象添加getter和setter, 如果有权限的话
- 用一个类包装原始对象, 间接提供getter和setter
- 使用ValueAnimator

属性动画的原理
属性动画根据传递的属性的初始值和最终值, 以动画的效果多次调用set方法, 随着时间推移, 把插值器和估值器的计算结果传递给set, 使其越来越接近最终值. 达到动画效果.

注意的问题
1. OOM - 尽量避免帧动画
2. 内存泄漏 - 在Activity退出时要及时停止相关动画
3. 兼容性问题
4. View 动画完成后, 无法隐藏了, 需要调用View#clearAnimation
5. 不要使用px
6. 动画元素的交互 - 可能实际位置不是看到的位置
7. 硬件加速 - 提高流畅性

0 0