动画——属性动画(Property Animation)

版权声明:本文为博主原创文章,但部分内容来源网络,如有侵权,告知即删! https://blog.csdn.net/chenyuan_jhon/article/details/78548856

Property Animation

  • 概述:

    属性动画是android3.0以后提供的一种动画实现机制,它可以作用于任何对象,对对象的属性赋值
    修改,从而实现动画效果。
    实现原理:在一定的时间内,通过插值器(动画改变的趋势)与估值器(动画具体变化的数值),不断改
    变对象的属性,实现动画的效果。

  • 重要类:

    • Animator: 动画的超类,实现了动画的开始结束等状态,以及添加监听。
    • AnimatorSet: 继承Animator,动画的组合类,用于制作一组动画,设置动画执行的先后顺序。
    • ValueAnimator: 继承Animator,动画的执行类。
    • ObjectAnimator: 继承ValueAnimator,动画的执行类。
    • TypeEvaluator: 类型估值,用于设置动画属性的值。
    • TimeInterpolator: 时间插值,用于设置动画的改变趋势。
    • AnimatorInflater: 加载属性动画的xml文件
    • PropertyValuesHolder 用于同时修个某个对象的多个属性
      具体的时间插值器在View Animation中已经介绍过了,在属性动画中这些插值器同样适用。
  • xml方式实现动画:

    步骤一:在xml中定义动画(名字叫property_animation1)

    在res下新建 animator 文件夹,在animator下创建property_animation1文件并定义动画

    animator对应代码中的ValueAnimator
    objectAnimator对应代码中的ObjectAnimator
    set对应代码中的AnimatorSet

    <set
       android:ordering=["together" | "sequentially"]>//执行顺序:同时或按动画先后
    
         <objectAnimator
             android:propertyName="string"//属性名,例如alpah,translationX等
             android:duration="int"//时间
             android:valueFrom="float | int | color"//属性变化起始值
             android:valueTo="float | int | color"//属性变化结束值
             android:startOffset="int"//调用start后过多少时间开始动画
             android:repeatCount="int"//重复次数
             android:repeatMode=["repeat" | "reverse"]//重复类型
             android:valueType=["intType" | "floatType"]/>//数值类型
    
         <animator
             android:duration="int"
             android:valueFrom="float | int | color"
             android:valueTo="float | int | color"
             android:startOffset="int"
             android:repeatCount="int"
             android:repeatMode=["repeat" | "reverse"]
             android:valueType=["intType" | "floatType"]/>
     </set>
    

    步骤二:在Java代码中调用xml文件

    Animator animator = AnimatorInflater.loadAnimator(context, R.animator.property_animation1);
    animator.setTarget(view);
    animator.start();

  • Java代码实现属性动画

    • ValueAnimator:

      扫描二维码关注公众号,回复: 3292563 查看本文章

      属性动画的运行机制是通过不断地对值进行操作来实现的,而初始值和结束值之间的动画过渡就是由
      ValueAnimator这个类来负责计算的。它的内部使用一种时间循环的机制来计算值与值之间的动画过渡,
      我们只需要将初始值和结束值提供给ValueAnimator,并且告诉它动画所需运行的时长,那么
      ValueAnimator就会自动帮我们完成从初始值平滑地过渡到结束值这样的效果。除此之外,
      ValueAnimator还负责管理动画的播放次数、播放模式、以及对动画设置监听器等,确实是一个非常重要的类。

      用法:

      ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
      anim.setDuration(300);
      anim.start();

      当你执行这段代码时缺看不到任何界面效果,这是因为ValueAnimator只负责将值从0过渡到1,我们还没有把动
      画的变化值赋给任何对象,那么怎么赋值给对象呢?我们需要添加一个重绘监听

      ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
      anim.setDuration(300);
      anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
      float currentValue = (float) animation.getAnimatedValue();
      Log.d("TAG", "cuurent value is " + currentValue);
      view.setXXX(currentValue)//改变view的某些属性
      }
      });
      anim.start();

      这样就可以在界面上看到动画的效果了。

    • ObjectAnimator:

      ObjectAnimator alpha = ObjectAnimator.ofFloat(mTextView2, "alpha", 1.0f, 0f, 1.0f);
      alpha.setDuration(300);
      alpha.start();

      其他动画的写法和这个类似,不同的地方只是作用的属性不同而已,比如把alpha换成translationY等

    • 组合动画:

      • AnimatorSet:

        Builder

        • after(Animator anim) 将现有动画插入到传入的动画之后执行
        • after(long delay) 将现有动画延迟指定毫秒后执行
        • before(Animator anim) 将现有动画插入到传入的动画之前执行
        • with(Animator anim) 将现有动画和传入的动画同时执行

        AnimatorSet

        • playTogether() 同时
        • play() 获取Builder

        使用:

        //AnimatorSet
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mTextView2, "alpha", 1.0f, 0f, 1.0f);
        ObjectAnimator rotate = ObjectAnimator.ofFloat(mTextView2, "rotation", 0f, 360f);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(mTextView2, "translationY", 100f, 330f);
        animatorSet.play(alpha).with(rotate).after(translationY);
        animatorSet.setDuration(5000);
        animatorSet.start();

      • PropertyValuesHolder:

        使用:

        PropertyValuesHolder a1 = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
        PropertyValuesHolder a2 = PropertyValuesHolder.ofFloat("translationY", 0f, 300f);
        ObjectAnimator.ofPropertyValuesHolder(mTextView2, a1, a2).setDuration(1000).start();


致谢:

http://blog.csdn.net/yanbober/article/details/46481171

http://blog.csdn.net/guolin_blog/article/details/43536355

猜你喜欢

转载自blog.csdn.net/chenyuan_jhon/article/details/78548856