首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextView的Android - Flashing backgroundColor和textColor

TextView的Android - Flashing backgroundColor和textColor
EN

Stack Overflow用户
提问于 2016-08-30 08:54:34
回答 1查看 177关注 0票数 1

我正在尝试获取一个TextView来反转背景和文本颜色,其间隔由一个名为" interval“(代表毫秒的整数值)的变量指定。

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

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

下面是我正在使用的代码:

代码语言:javascript
复制
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();

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2016-08-30 13:52:33

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

代码语言:javascript
复制
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的代码

代码语言:javascript
复制
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);
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39217298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档