【问题标题】:ValueAnimator working with Button but not with LinearLayoutValueAnimator 使用 Button 但不使用 LinearLayout
【发布时间】:2019-07-18 17:17:19
【问题描述】:

以下代码在提供的视图是 Button 时有效,但当它是 LinearLayout 时没有任何反应(背景颜色不会改变)。

如果我在没有 ValueAnimator 的情况下手动设置 LinearLayout 的颜色,它会起作用。我也尝试使用 ObjectAnimator,但结果看起来很糟糕。

一个示例调用:

playBlinkAnimation(view, activity.getResources().getColor(R.color.transparent), activity.getResources().getColor(R.color.colorRed), 5000);


view.getBackground().setColorFilter(activity.getResources().getColor(R.color.colorRed), PorterDuff.Mode.DARKEN);


private void playBlinkAnimation(final View view, int colorFrom, int colorTo, int duration) {

        ValueAnimator colorAnimation = new ValueAnimator();
        colorAnimation.setIntValues(colorFrom, colorTo);
        colorAnimation.setEvaluator(new ArgbEvaluator());


        colorAnimation.setDuration(duration);
        colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
        colorAnimation.setRepeatMode(ValueAnimator.REVERSE);

        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                view.getBackground().setColorFilter((int) animator.getAnimatedValue(), PorterDuff.Mode.DARKEN);
            }

        });

        colorAnimation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.getBackground().setColorFilter(null);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                view.getBackground().setColorFilter(null);
                view.setAlpha(1);
                view.getBackground().setColorFilter(null);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        colorAnimation.start();
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以尝试使用 AnimatorSet 和它的 playTogether 功能。它需要一组 Animator,它旨在同时运行多个动画,但它也应该只适用于一个动画。

    希望对你有所帮助!

    【讨论】:

      【解决方案2】:

      由于您似乎希望您的布局从透明背景过渡到不透明的红色,您应该为布局的 alpha 参数设置动画

      ObjectAnimator animator = ObjectAnimator.ofInt(view, "alpha", 0, 1);
      animator.setDuration(5000);
      animator.setInterpolator(new LinearInterpolator());
      animator.start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-24
        • 2012-08-09
        • 1970-01-01
        • 2014-04-28
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        • 1970-01-01
        相关资源
        最近更新 更多