美文网首页
AnimatorSet和ObjectAnimator的结合使用

AnimatorSet和ObjectAnimator的结合使用

作者: ReleaseYH | 来源:发表于2018-07-02 09:15 被阅读0次

    众所周知,AnimatorSet是用来处理view的一系列动画的集合,允许对控件设置一系列的动画。在AnimatorSet里可以指定动画的播放顺序、是否一起播放或者是否延迟播放。

    向AnimatorSet添加动画有两种不同的方法,其一是调用playTogether()或playSequentially()方法来一次性添加一组动画,其二是播放(Animator)可以与Builder类一个一个添加动画。
    注:在添加系列动画时,一定要考虑到同时执行的动画之间是否会出现冲突。比如添加的动画且同时执行的,一个向左移动,另外一个向右移动,此时Android系统必然不知道如何处理,这种小失误应该是规避的。
    这里要知道AnimatorSet.Builder这个内部类是一个使用的动画工具类,用于方便向AnimatorSet添加动画以及设置各种动画之间的关系。在AnimatorSet.Builder中,共声明了after(long)、after(Animator)、before(Animator)、with(Animator)等四个方法。

    after(delay):设置动画延迟delay时间后播放
    after(anim):设置在anim动画结束后播放此动画
    before(anim):设置此动画早于anim播放
    with(anim):设置此动画与anim一起播放

    与playTogether()和playSequentially()方法想比较,采用AnimatorSet.Builder向AnimatorSet逐一添加动画,更能准确的知道动画的播放顺序。

    示例:

    AnimatorSet bouncer = new AnimatorSet();
    bouncer.play(bounceAnim).before(squashAnim1);
    bouncer.play(squashAnim1).with(squashAnim2);
    bouncer.play(squashAnim1).with(stretchAnim1);
    bouncer.play(squashAnim1).with(stretchAnim2);
    bouncer.play(bounceBackAnim).after(stretchAnim2);
    ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
    fadeAnim.setDuration(250);
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.play(bouncer).before(fadeAnim);
    animatorSet.start();
    

    最终的动画执行顺序是:

    1,bounceAnim
    2,squashAnim1与squashAnim2、stretchAnim1、stretchAnim2一起执行
    3,bounceBackAnim
    4,fadeAnim
    

    还有一些常用的方法:
    play(Animator anim):添加一个动画,并返回AnimatorSet.Builder
    playSequentially(List items):添加一组动画,播放顺序为一步步播放
    playSequentially(Animator… items):添加一组动画,播放顺序为一步步播放
    playTogether(Collection items):添加一组动画,播放顺序为一起播放
    playTogether(Animator… items):添加一组动画,播放顺序为一起播放

    下面结合ObjectAnimator类来展示这些方法的执行顺序。

    ObjectAnimator属性动画:直接动画所给的对象,他会调用对象对应属性的get/set方法吧属性的值设置给对象的属性,直接实现动画效果。
    ObjectAnimator的初始化也是通过一系列ofXXX()方法来进行,但是参数有所变化,他的这些方法都要求传入一个Object对象,然后就会在这个对象上执行动画。
    分析一下ofFloat(Object target, String propertyName, float... values)参数:

    参数 解释
    target 对象,一般就某个view,指定要改变谁的属性 。
    propertyName 属性名,指定要改变对象的什么属性,这个属性名要求在兑现中必须有对应的public的PsetPropertyName的方法。比如下面要讲到的rotationX,rotaionY等等。
    values 一系列这个属性将会到达的值。

    这是propertyName的一些解释和效果

    ObjectAnimator第二个参数的属性 对应的set方法 效果
    alpha public void setAlpha(float alpha) 改变透明度
    translationX setTranslationX 沿X轴平移
    translationY setTranslationY 沿Y轴平移Y
    scaleX setScaleX 沿X轴缩放
    scaleY setScaleY 沿Y轴缩放
    rotationX setRotationX 绕X轴旋转
    rotationY setRotationY 绕Y轴旋转
    rotation setRotation 绕Z轴旋转

    下面是动画的实现

       /**
         * 光效 沿x轴翻转
         * @return
         */
        public ObjectAnimator buildCoverLightrotationX(){
             //0,为初始状态, -20为角度,中间变化值。-50为角度是最终动画截止角度
             return ObjectAnimator.ofFloat(mBoxLight, "rotationX", 0, -20, -50).setDuration(TIME_TRANSLATE_DURATION);
        }
    
        
        /**
         * 光效 渐变
         * @return
         */
        public ObjectAnimator buildCoverLightAlpha(){
            return ObjectAnimator.ofFloat(mBoxLight, "alpha", 0.2f, 1, 0).setDuration(TIME_TRANSLATE_DURATION);
        }
    
          /**
             * 半打开
             *
             * @return
             */
            public ObjectAnimator buildCoverHalfOn() {
                final float halfHeight = mBoxcover.getHeight() * 0.4f;
                return ObjectAnimator.ofFloat(mBoxcover, "translationY", 0, halfHeight, halfHeight)
                        .setDuration(TIME_TRANSLATE_DURATION);
            }
    
    
          /**
             * 光效 滑到一半
             * @return
             */
            public ObjectAnimator buildCoverLightHalfOn() {
                final float fullHeight = mBoxcover.getHeight() * 0.4f;
                return ObjectAnimator.ofFloat(mBoxLight, "translationY", 0, fullHeight, fullHeight)
                        .setDuration(TIME_TRANSLATE_DURATION);
            }
    

    然后是对这些动画的一并播放

     AnimatorSet set = new AnimatorSet();
    ObjectAnimator coverAnim = mAnimationControler.buildCoverHalfOn();
    ObjectAnimator lightMoveY = lightMoveY = mAnimationControler.buildCoverLightHalfOn();
    ObjectAnimator lightMoveX = mAnimationControler.buildCoverLightrotationX();
    ObjectAnimator lightAlpha = mAnimationControler.buildCoverLightAlpha();
    coverAnim.setDuration(TIME_TRANSLATE_DURATION);
    set.playTogether(coverAnim, lightMoveX, lightAlpha, lightMoveY);
    

    最终的效果图是这样子的。界面方面的就不讲了。

    1.gif

    playSequentially方法其实就是动画一个个播放,这里由于效果不好看所以就不展示啦,哈哈哈。

    相关文章

      网友评论

          本文标题:AnimatorSet和ObjectAnimator的结合使用

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