笔记之android动画 初识ValueAnimator 属性动画

来源:互联网 发布:网络分离器s61088o 编辑:程序博客网 时间:2024/06/10 16:44

学习于大神博客

http://blog.csdn.net/harvic880925/article/details/50525521


Android动画中,总共有两种类型的动画View Animation(视图动画)和Property Animator(属性动画);


  • View Animation包括Tween Animation(补间动画)和Frame Animation(逐帧动画); 
  • Property Animator包括ValueAnimator和ObjectAnimation;

    引入属性动画的原因:

    1.Property Animator能实现补间动画无法实现的功能 
    2,.View Animation仅能对指定的控件做动画,而Property Animator是通过改变控件某一属性值来做动画的。 
    3.补间动画虽能对控件做动画,但并没有改变控件内部的属性值。而Property Animator则是恰恰相反,Property Animator是通过改变控件内部的属性   值来    达到动画效果的


    ValueAnimator 的使用:

    第一步:创建ValueAnimator实例

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. ValueAnimator animator = ValueAnimator.ofInt(0,400);  
    2. animator.setDuration(1000);  
    3. animator.start();  

    第二步:添加监听

    添加监听:
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. ValueAnimator animator = ValueAnimator.ofInt(0,400);  
    2. animator.setDuration(1000);  
    3.   
    4. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
    5.     @Override  
    6.     public void onAnimationUpdate(ValueAnimator animation) {  
    7.         int curValue = (int)animation.getAnimatedValue(); 
    8.        
    9.     }  
    10. });  
    11. animator.start();  
    • alueAnimator只负责对指定的数字区间进行动画运算
    • 我们需要对运算过程进行监听,然后自己对控件做动画操作

    ValueAnimator 的常用方法:

    ofInt与ofFloat

    传进去的值列表,就表示动画时的变化范围

    常用函数

    1. /** 
    2.  * 设置动画时长,单位是毫秒 
    3.  */  
    4. ValueAnimator setDuration(long duration)  
    5. /** 
    6.  * 获取ValueAnimator在运动时,当前运动点的值 
    7.  */  
    8. Object getAnimatedValue();  
    9. /** 
    10.  * 开始动画 
    11.  */  
    12. void start()  
    13. /** 
    14.  * 设置循环次数,设置为INFINITE表示无限循环 
    15.  */  
    16. void setRepeatCount(int value)  
    17. /** 
    18.  * 设置循环模式 
    19.  * value取值有RESTART,REVERSE, 
    20.  */  
    21. void setRepeatMode(int value)  
    22. /** 
    23.  * 取消动画 
    24.  */  
    25. void cancel()  

    ValueAnimator 的不常用方法:

    1. /** 
    2.  * 延时多久时间开始,单位是毫秒 
    3.  */  
    4. public void setStartDelay(long startDelay)  
    5. /** 
    6.  * 完全克隆一个ValueAnimator实例,包括它所有的设置以及所有对监听器代码的处理 
    7.  */  
    8. public ValueAnimator clone()  

    两个监听器:

    监听器animator.addUpdateListener,以监听动画过程中值的实时变化
    1. /** 
    2.  * 监听器一:监听动画变化时的实时值 
    3.  */  
    4. public static interface AnimatorUpdateListener {  
    5.     void onAnimationUpdate(ValueAnimator animation);  
    6. }  
    7. //添加方法为:public void addUpdateListener(AnimatorUpdateListener listener)  
    8. /** 
    9.  * 监听器二:监听动画变化时四个状态 
    10.  */  
    11. public static interface AnimatorListener {  
    12.     void onAnimationStart(Animator animation);  
    13.     void onAnimationEnd(Animator animation);  
    14.     void onAnimationCancel(Animator animation);  
    15.     void onAnimationRepeat(Animator animation);  
    16. }  

    监听器2 中的1当动画开始时,会调用
        2当动画结束时,会调用
        3当动画取消时,调用
    4当动画重复时,会调用

    取消监听

    移除监听器:

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. /** 
    2.  * 移除AnimatorUpdateListener 
    3.  */  
    4. void removeUpdateListener(AnimatorUpdateListener listener);  
    5. void removeAllUpdateListeners();  
    6.  /** 
    7.   * 移除AnimatorListener 
    8.   */  
    9. void removeListener(AnimatorListener listener);  
    10. void removeAllListeners();  

  • 0 0
    原创粉丝点击