美文网首页
属性动画核心API以及使用

属性动画核心API以及使用

作者: 凌烟醉卧 | 来源:发表于2020-02-23 15:49 被阅读0次

    属性动画的原理:初始化动画值,然后把该值设置到属性中,接着注册垂直同步信号,最后进行View重绘。这个过程是一直循环进行的。动画对值进行了修改,但是没有涉及到重绘,是因为垂直同步信号,在安卓中每隔16ms发送一次,对View进行了重新绘制测量和布局。

    API3.0之后提出的动画模式,优点如下

    • 不再局限于View对象,无对象也可以进行动画处理。
    • 不再局限于4种基本变换:平移,旋转,缩放,透明度。
    • 可以灵活的操作任意对象属性,根据自己业务来实现自己想要的结果。

    核心API

    • ObjectAnimator 对象动画
    • ValueAnimator 值动画
    • PropertyValuesHolder 用于多个动画同时执行
    • TypeEvaluator 估值器
    • Interpolator 插值器
    • AnimatorSet 动画集合

    基础使用

    • ObjectAnimator 操作对象属性,不局限于对象,并且动画可以一起执行。
    ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.3f,1.0f);
    alphaAnim.setDuration(1000);//执行时间
    alphaAnim.setStartDelay(300);//延迟
    alphaAnim.start();
    

    “alpha”是透明度,除此之外,还有平移,缩放,旋转。

    • AnimatorSet 动画集合
      ObjectAnimator除了单个执行外,还可以使用AnimatorSet一起执行动画。
    ObjectAnimator animator1 = ObjectAnimator.ofFloat(iv, "translationX", 0f, 500F);
    ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
    ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 2f);
    
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.setDuration(500);
    
    //animatorSet.play(animator3).with(animator2).before(animator1);//链式调用顺序
    //animatorSet.play(animator3).with(animator2).after(animator1);//链式调用顺序      
    //animatorSet.playTogether(animator1,animator2,animator3);//一起执行
    animatorSet.playSequentially(animator2, animator1, animator3);//顺序执行
    animatorSet.start();
    
    • ValueAnimator 值动画
      上面的ObjectAnimator设置的属性的名字都是系统要求固定的,还可以使用其它任意的名字来进行操作,需要配置ValueAnimator
    ObjectAnimator animator = ObjectAnimator.ofFloat(v, "hello", 0f, 100f,50f);
    animator.setDuration(300);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
               float value = (float) animation.getAnimatedValue();//百分比的所对应的值
               Log.d("dn_alan", value + "");
               v.setScaleX(0.5f + value / 200);
               v.setScaleY(0.5f + value / 200);
         }
    });
    animator.start();
    //animator.setRepeatCount(2);//重复次数
    //animator.setRepeatCount(ValueAnimator.INFINITE);//无限次数
    
    • Interpolator 插值器

    插值器是由API提供的一组算法,用来操作动画执行是的变换规则,省去了一些自己写算法的麻烦,大致分为九种

    ValueAnimator animator = new ValueAnimator();
    animator.setDuration(3000);
    animator.setObjectValues(new PointF(0, 0));
    final PointF point = new PointF();
    //估值
    animator.setEvaluator(new TypeEvaluator() {
        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            point.x = 100f * (fraction * 5);
            // y=vt=1/2*g*t*t(重力计算)
            point.y = 0.5f * 98f * (fraction * 5) * (fraction * 5);
            return point;
        }
    });
    
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            v.setX(point.x);
            v.setY(point.y);
        }
    });
    
    
    //加速插值器,公式: y=t^(2f) (加速度参数. f越大,起始速度越慢,但是速度越来越快)
    animator.setInterpolator(new AccelerateInterpolator(10));
    
    //减速插值器公式: y=1-(1-t)^(2f) (描述: 加速度参数. f越大,起始速度越快,但是速度越来越慢)
    //animator.setInterpolator(new DecelerateInterpolator());
    
    //先加速后减速插值器 y=cos((t+1)π)/2+0.5
    //animator.setInterpolator(new AccelerateDecelerateInterpolator());
    
    //张力值, 默认为2,T越大,初始的偏移越大,而且速度越快 公式:y=(T+1)×t3–T×t2
    //animator.setInterpolator(new AnticipateInterpolator());
    
    //张力值tension,默认为2,张力越大,起始和结束时的偏移越大,
    //而且速度越快;额外张力值extraTension,默认为1.5。公式中T的值为tension*extraTension
    //animator.setInterpolator(new AnticipateOvershootInterpolator());
    
    //弹跳插值器
    //animator.setInterpolator(new BounceInterpolator());
    
    //周期插值器 y=sin(2π×C×t)  周期值,默认为1;2表示动画会执行两次
    //animator.setInterpolator(new CycleInterpolator(2));
    
    //线性插值器,匀速公式:Y=T
    //animator.setInterpolator(new LinearInterpolator());
    
    //公式: y=(T+1)x(t1)3+T×(t1)2 +1
    //描述: 张力值,默认为2,T越大,结束时的偏移越大,而且速度越快
    //animator.setInterpolator(new OvershootInterpolator());
    
    animator.start();
    
    • PropertyValuesHolder 用于多个动画同时执行
    PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);
    PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f);
    PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f);
    ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(v, holder1, holder2, holder3);
    animator.setDuration(200);
    animator.start();
    
    • TypeEvaluator 估值器的核心目的:自定义变换规则
    ValueAnimator animator = new ValueAnimator();
    animator.setDuration(3000);
    animator.setObjectValues(new PointF(0, 0));
    final PointF point = new PointF();
    //估值
    animator.setEvaluator(new TypeEvaluator() {
         @Override
         public Object evaluate(float fraction, Object startValue, Object endValue) {
            point.x = 100f * (fraction * 5);
            // y=vt=1/2*g*t*t(重力计算)
            point.y = 0.5f * 130f * (fraction * 5) * (fraction * 5);
                return point;
         }
    });
    
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
             PointF p = (PointF) animation.getAnimatedValue();
             Log.d("dn_alan", p.x + "===" + p.y);
             v.setX(p.x);
             v.setY(p.y);
         }
    });
    animator.start();
    

    附有源码两篇文章:
    属性动画源码分析-ofFloat方法
    属性动画源码分析-start方法

    相关文章

      网友评论

          本文标题:属性动画核心API以及使用

          本文链接:https://www.haomeiwen.com/subject/btkmvctx.html