【问题标题】:Android - Flashing backgroundColor and textColor of TextView togetherAndroid - TextView的背景颜色和文本颜色一起闪烁
【发布时间】:2016-08-30 00:54:33
【问题描述】:

我试图让 TextView 以一个名为“interval”的变量(整数值表示毫秒)指定的间隔反转背景和文本颜色。

目前,我为每个属性使用 ObjectAnimator,然后使用 AnimatorSet 对象将它们一起播放。这行得通;但是,我不确定如何摆脱所有的过渡颜色。我不需要平滑过渡,只需要硬闪。我尝试使用 setFrameDelay(interval) 来完成此操作,但这不起作用。

我曾尝试使用单独的线程/runnable 执行此操作,但我一直遇到需要在执行另一个操作之前停止线程的问题。我无法让新操作可靠地等待线程停止,因此当时机不正确时,我会得到奇怪的背景和文本覆盖。使用 Android 的动画框架似乎更适合轻松启动和停止。

这是我正在使用的代码:

ObjectAnimator backgroundColorValueAnimator = ObjectAnimator.ofInt(
            textView,
            "backgroundColor",
            Color.BLACK,
            Color.parseColor(globalSettings.color)
    );
    ObjectAnimator textColorValueAnimator = ObjectAnimator.ofInt(
            textView,
            "textColor",
            Color.parseColor(globalSettings.color),
            Color.BLACK
    );
    backgroundColorValueAnimator.setFrameDelay(interval);
    textColorValueAnimator.setFrameDelay(interval);
    backgroundColorValueAnimator.setRepeatCount(ObjectAnimator.INFINITE);
    backgroundColorValueAnimator.setRepeatMode(ObjectAnimator.RESTART);
    textColorValueAnimator.setRepeatCount(ObjectAnimator.INFINITE);
    textColorValueAnimator.setRepeatMode(ObjectAnimator.RESTART);
    globalSettings.invertBackground = new AnimatorSet();
    globalSettings.invertBackground.play(backgroundColorValueAnimator).with(textColorValueAnimator);
    globalSettings.invertBackground.setDuration(interval);
    globalSettings.invertBackground.start();

任何帮助将不胜感激。

【问题讨论】:

    标签: android textview objectanimator animatorset


    【解决方案1】:

    您可以使用ValueAnimator 进行倒计时和自定义ValueAnimator.AnimatorUpdateListener

    ValueAnimator myColorAnimator;
    MyColorUpdateListener myColorUpdateListener;
    int interval = 2000;
    
    @Override
    public void onCreate(Bundle savedState){
        TextView textView = (TextView) findViewById(R.id.textView);
    
        myColorAnimator = ValueAnimator.ofObject(new IntEvaluator(), 0, 2 * interval);
        myColorAnimator.setDuration(2 * interval);
        myColorUpdateListener = new MyColorUpdateListener(textView, Color.BLACK, Color.parseColor(globalSettings.color), interval );
        myColorAnimator.addUpdateListener(myColorUpdateListener);
    
        myColorAnimator.setInterpolator(new LinearInterpolator());
        myColorAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        myColorAnimator.setRepeatMode(ObjectAnimator.RESTART);
        myColorAnimator.start();        
    }
    

    自定义ValueAnimator.AnimatorUpdateListener的代码:

    public class MyColorUpdateListener implements ValueAnimator.AnimatorUpdateListener {
    
    private TextView textView;
    private int colorText, colorBackground, interval;
    
    MyColorUpdateListener(TextView tv, int colorBg, int colorText, int interval) {
        this.textView = tv;
        this.colorText = colorText;
        this.colorBackground = colorBg;
        this.interval = interval;
    }
    
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        Object o = animation.getAnimatedValue();
        int counter = (int)o;
    
        if (counter == 0)
        {
            textView.setBackgroundColor(colorBackground);
            textView.setTextColor(colorText);
        }
        else if (counter == interval)
        {
            textView.setBackgroundColor(colorText);
            textView.setTextColor(colorBackground);
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多