我正在尝试获取一个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();任何帮助都将不胜感激。
发布于 2016-08-30 13:52:33
您可以使用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);
}
}}
https://stackoverflow.com/questions/39217298
复制相似问题