gpt4 book ai didi

java - Android TextView 中的动画背景颜色

转载 作者:行者123 更新时间:2023-12-02 12:14:48 29 4
gpt4 key购买 nike

有谁知道为什么这段代码不起作用以及如何修复它?我正在尝试将 TextView 的背景颜色设置为动画。 IDE 不显示错误。

private void animationButton(final TextView textView) {

int firstColor = Color.parseColor("FFFFFFFF");
int secondColor = Color.parseColor("FF00FF00");

ValueAnimator animation = new ValueAnimator();
animation.setIntValues(firstColor, secondColor);
animation.setEvaluator(new ArgbEvaluator());

animation.setDuration(300);

animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
textView.setBackgroundColor((int) animation.getAnimatedValue());
}
});

animation.start();
}

最佳答案

IDE doesn't show error

然后我假设您将 animationButton 包围在 try-catch block 中,并且没有记录任何内容,因为 Color.parseColor需要 # 来解析十六进制颜色代码。您发布的代码应该会收到 IllegalArgumentException

来自文档

Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are:

#RRGGBB

#AARRGGBB

否则您需要传入以下值之一:

red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy, olive, purple, silver, and teal.

来源

@ColorInt
public static int parseColor(@Size(min=1) String colorString) {
if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int)color;
} else {
Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
}

关于java - Android TextView 中的动画背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46268223/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com