简体   繁体   English

动态设置视图的背景颜色

[英]setting the background color of a view dynamically

i have this small app that chooses values in a number picker and is supposed to dynamically change the background color. 我有这个小应用程序,在数字选择器中选择值,并应该动态更改背景颜色。 through log messages i have seen that the hex color value is generated correctly, but when i call set background color an error pops up and the program crashes. 通过日志消息,我已经看到正确生成了十六进制颜色值,但是当我调用设置背景颜色时,弹出错误并且程序崩溃。 the error java.lang.NumberFormatException: Invalid int: "0xFF010000".... 错误java.lang.NumberFormatException:无效的int:“0xFF010000”....

the issue (i think) is in the setBackground() call at the bottom of ActivityMain. 问题(我认为)是在ActivityMain底部的setBackground()调用中。

here is my code: 这是我的代码:

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

and the color class incase you are wondering how i generated the hex value: 和颜色类,你想知道我是如何生成十六进制值的:

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

}

I would actually use the standard Android Color API to get the color. 我实际上会使用标准的Android Color API来获取颜色。

Color.parseColor(colorCode);

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

1) Your int is too big, because the Integer.parseInt() function only takes a signed int as an argument. 1)你的int太大了,因为Integer.parseInt()函数只接受一个带符号的int作为参数。

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

You can fix this easily by using parseLong instead 您可以使用parseLong轻松解决此问题

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

Implemented in your code it would look like this: 在您的代码中实现它看起来像这样:

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

2) As Alexander pointed out in his answer, don't include the "0x"-bit, the 'x' will mess up the parser. 2)正如亚历山大在他的回答中指出的那样,不要包含“0x” - 位,“x”会破坏解析器。

---EDIT--- - -编辑 - -

Thanks to Alexander I came up with another solution, you could always do this: 感谢亚历山大,我提出了另一种解决方案,你可以随时做到:

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

This works because the datatype int itself actually can contain RGBA data. 这是有效的,因为数据类型int本身实际上可以包含RGBA数据。

从数字字符串中排除“0x”部分

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

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