简体   繁体   English

Android - 将TextView的backgroundColor和textColor一起闪烁

[英]Android - Flashing backgroundColor and textColor of TextView together

I am trying to get a TextView to invert the background and text colors at an interval specified by a variable called "interval" (integer value representing milliseconds). 我试图让TextView以一个名为“interval”的变量指定的间隔反转背景和文本颜色(整数值代表毫秒)。

Currently, I am using an ObjectAnimator for each property, then using an AnimatorSet object to play them together. 目前,我正在为每个属性使用ObjectAnimator,然后使用AnimatorSet对象将它们一起播放。 This works; 这有效; however, I'm not sure how to get rid of all of the transitional colors. 但是,我不知道如何摆脱所有的过渡色。 I don't need a smooth transition, just a hard flashing. 我不需要平滑过渡,只需要一个硬闪烁。 I tried using setFrameDelay(interval) to accomplish this, but that's not working. 我尝试使用setFrameDelay(interval)来完成此任务,但这不起作用。

I had tried doing it with a separate thread/runnable, but I kept running into issues where I needed to stop the thread before performing another action. 我曾尝试用一个单独的线程/ runnable来做这件事,但我一直在遇到需要在执行另一个动作之前停止线程的问题。 I could not get the new action to reliably wait for the thread to stop, so I would end up with weird background and text overlays when the timing wasn't just right. 我无法让新动作可靠地等待线程停止,所以当时机不正确时,我最终会得到奇怪的背景和文本叠加。 Using the Android's Animation framework seemed better suited for easy starting and stopping. 使用Android的动画框架似乎更适合轻松启动和停止。

Here's the code I'm working with: 这是我正在使用的代码:

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();

Any help would be greatly appreciated. 任何帮助将不胜感激。

You can work with ValueAnimator for the countdown and a custom ValueAnimator.AnimatorUpdateListener : 您可以使用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();        
}

Code for the custom ValueAnimator.AnimatorUpdateListener : 自定义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);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM