动态设置视图的背景颜色

时间:2014-08-24 21:17:58

标签: java android

我有这个小应用程序,它在数字选择器中选择值,并且应该动态地改变背景颜色。通过日志消息,我已经看到正确生成了十六进制颜色值,但是当我调用设置背景颜色时,弹出错误并且程序崩溃。 错误java.lang.NumberFormatException:无效的int:“0xFF010000”....

问题(我认为)是在ActivityMain底部的setBackground()调用中。

这是我的代码:

package com.example.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

public class MainActivity extends Activity {

static final int MIN_VAL = 0;
static final int MAX_VAL = 255;
NumberPicker alphaPicker, redPicker, greenPicker, bluePicker;
View colorView;
Color bgColor = new Color();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    alphaPicker = (NumberPicker) findViewById(R.id.alphaPicker);
    redPicker = (NumberPicker) findViewById(R.id.redPicker);
    greenPicker = (NumberPicker) findViewById(R.id.greenPicker);
    bluePicker = (NumberPicker) findViewById(R.id.bluePicker);

    alphaPicker.setMinValue(MIN_VAL);
    alphaPicker.setMaxValue(MAX_VAL);
    alphaPicker.setWrapSelectorWheel(false);

    redPicker.setMinValue(MIN_VAL);
    redPicker.setMaxValue(MAX_VAL);
    redPicker.setWrapSelectorWheel(false);

    greenPicker.setMinValue(MIN_VAL);
    greenPicker.setMaxValue(MAX_VAL);
    greenPicker.setWrapSelectorWheel(false);

    bluePicker.setMinValue(MIN_VAL);
    bluePicker.setMaxValue(MAX_VAL);
    bluePicker.setWrapSelectorWheel(false);

    colorView = findViewById(R.id.color_box);
    colorView.setBackgroundColor(0xFF000000);

    redPicker.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            bgColor.setRedVal(newVal);
            Log.v(bgColor.getRedVal(), " = redVal");
            Log.v(bgColor.getColorCode(), " = color code");
            colorView.setBackgroundColor(bgColor.getColorCodeAsInt());
        }
    });
}
}

和颜色类,你想知道我是如何生成十六进制值的:

package com.example.android.test;

public class Color {

private String redVal;
private String blueVal;
private String greenVal;
private String alphaVal;
private String colorCode;

public Color (){
    alphaVal = "FF";
    redVal = "00";
    greenVal = "00";
    blueVal = "00";
    colorCode = "0xFF000000";
}

public void generateColorCode() {

    StringBuilder theColor = new StringBuilder("0x")
            .append(this.alphaVal)
            .append(this.redVal)
            .append(this.greenVal)
            .append(this.blueVal);
    colorCode = theColor.toString();

}

public String getRedVal() {
    return redVal;
}

public void setRedVal(Integer redVal) {
    this.redVal = String.format("%02x", redVal);
    generateColorCode();
}

public String getBlueVal() {
    return blueVal;
}

public void setBlueVal(Integer blueVal) {
    this.blueVal = String.format("%02x", blueVal);
}

public String getGreenVal() {
    return greenVal;
}

public void setGreenVal(Integer greenVal) {
    this.greenVal = String.format("%02x", greenVal);
}

public String getAlphaVal() {
    return alphaVal;
}

public void setAlphaVal(Integer alphaVal) {
    this.alphaVal = String.format("%02x", alphaVal);
}

public String getColorCode() {
    return colorCode;
}

public Integer getColorCodeAsInt() {
    return Integer.parseInt(colorCode, 16);
}

}

3 个答案:

答案 0 :(得分:2)

我实际上会使用标准的Android Color API来获取颜色。

Color.parseColor(colorCode);

http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29

答案 1 :(得分:1)

1)你的int太大了,因为Integer.parseInt()函数只将一个带符号的int作为参数。

0xFF010000 == 4294901760  // your number
0x7FFFFFFF == 2147483647  // maximum signed Integer Value

您可以使用parseLong轻松解决此问题

0xFFFFFFFF     == 4294967295            // RGBA color format maximum
Long.MAX_VALUE == 9223372036854775807L  // maximum signed Long value

在您的代码中实现它看起来像这样:

public int getColorCodeAsInt() {
    return (int)Long.parseLong(colorCode, 16);
}

2)正如亚历山大在他的回答中指出的那样,不要包含“0x” - 位,“x”会破坏解析器。

<强> --- --- EDIT

感谢亚历山大,我想出了另一个解决方案,你可以随时做到这一点:

public int getColorCodeAsInt() {
    // this would return hex value 0xFFFFFFF8;
    return (Integer.parseInt("FFFFFFF", 16) * 16) // first 7 digits
          + Integer.parseInt("8", 16);            // last digit
}

这是有效的,因为数据类型int本身实际上可以包含RGBA数据。

答案 2 :(得分:0)

从数字字符串

中排除“0x”部分