Java/Android 字符串到颜色的转换

发布于 2024-10-04 03:19:26 字数 460 浏览 2 评论 0原文

我正在制作一个应用程序,我希望能够通过用户输入(edittext)和十六进制值(例如#eeeeee等)设置各种颜色。问题是我似乎不知道如何转换它们。

如果我在代码中做这样的事情,它工作得很好: 标题栏.setBackgroundColor(0xFF545455);

但是,如果我通过编辑文本检索一个值,例如“545455”,我将无法使其工作

          String tbColor = tb_color.getText().toString();             
          String value = "0xFF" + tbColor;  
          int setColor = Integer.valueOf(value);
          titlebar.setBackgroundColor(setColor);

任何人对如何实现此目标有任何想法吗?

I'm making an app and I'd like to be able to set various colors via user input(edittext) and hex values e.g. #eeeeee and so on. Problem is I cannot seem to figure out how to convert them.

If I do something in code like this it works fine:
titlebar.setBackgroundColor(0xFF545455);

However if I retrieve a value via the edittext say "545455" I cannot get it work

          String tbColor = tb_color.getText().toString();             
          String value = "0xFF" + tbColor;  
          int setColor = Integer.valueOf(value);
          titlebar.setBackgroundColor(setColor);

Anyone have any ideas on how to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光的暖冬 2024-10-11 03:19:26

怎么样 titlebar.setBackgroundColor(Color.parseColor("#545455"));

What about titlebar.setBackgroundColor(Color.parseColor("#545455"));

酷炫老祖宗 2024-10-11 03:19:26

http://download.oracle. com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)

例如:

titlebar.setBackgroundColor(Integer.parseInt("545455", 16)+0xFF000000);

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)

For example:

titlebar.setBackgroundColor(Integer.parseInt("545455", 16)+0xFF000000);
故事灯 2024-10-11 03:19:26
StringBuffer hexString = new StringBuffer();
hexString.append(Integer.toHexString(0xFF);
System.out.print(hexString.toString());
StringBuffer hexString = new StringBuffer();
hexString.append(Integer.toHexString(0xFF);
System.out.print(hexString.toString());
花伊自在美 2024-10-11 03:19:26

这是我从任何字符串中获取颜色的函数。
例如:“世界你好!”将返回一些绿色的 R G B:84 181 132

public static int getColorFromString(String string) {
    int[] RGB = {0,0,0};
    int l = string.length();
    String sub_string_0 = string.substring(0, (int) Math.ceil((double) l / 3));                 // responsable for Red
    int l_0 = sub_string_0.length();
    String sub_string_1 = string.substring(l_0,  l_0 + (int) Math.ceil((double) (l - l_0)/2));  // responsable for Green
    String sub_string_2 = string.substring(l_0 + sub_string_1.length(), string.length());       // responsable for Blue

    String[] sub_string = new String[]{
            sub_string_0,
            sub_string_1,
            sub_string_2
    };
    for(int i = 0; i < sub_string.length; i++) {
        if(sub_string[i].length()==0)
            sub_string[i] = " ";                                                                // we asign a value (a space)
        Log.d("sub_string", i + " " + sub_string[i]);
        for (char c : sub_string[i].toCharArray()) {
            int c_val = Character.getNumericValue(c) - Character.getNumericValue('a');          // for 'a' -> 0     for 'z' -> 25
            if(c_val < 0)                                                                       //  spaces, numbers ...
                c_val= new Random().nextInt(25);                                                //add some salt - a random number
            Log.d("value ", c + " -> " + c_val);
            RGB[i] = RGB[i] + c_val;
        }
    }

    int letters_number = Character.getNumericValue('z') - Character.getNumericValue('a');       //  z - a    35 - 10

    // normalizing
    int R = 255 * RGB[0]/sub_string[0].length()/letters_number;
    int G = 255 * RGB[1]/sub_string[1].length()/letters_number;
    int B = 255 * RGB[2]/sub_string[2].length()/letters_number;
    Log.d("R G B", R +" " + G + " "  + B);

    return Color.rgb(R, G, B);
}

注意:
如果您的字符串包含特殊字符、空格或数字,该函数不会每次都返回相同的数字(颜色)。那里有一些盐 - 我为这些角色分配了随机数字,只是为了好玩......

here is my function to get colors from ANY string.
Eg: "Hello World!" will return you some green color of R G B: 84 181 132

public static int getColorFromString(String string) {
    int[] RGB = {0,0,0};
    int l = string.length();
    String sub_string_0 = string.substring(0, (int) Math.ceil((double) l / 3));                 // responsable for Red
    int l_0 = sub_string_0.length();
    String sub_string_1 = string.substring(l_0,  l_0 + (int) Math.ceil((double) (l - l_0)/2));  // responsable for Green
    String sub_string_2 = string.substring(l_0 + sub_string_1.length(), string.length());       // responsable for Blue

    String[] sub_string = new String[]{
            sub_string_0,
            sub_string_1,
            sub_string_2
    };
    for(int i = 0; i < sub_string.length; i++) {
        if(sub_string[i].length()==0)
            sub_string[i] = " ";                                                                // we asign a value (a space)
        Log.d("sub_string", i + " " + sub_string[i]);
        for (char c : sub_string[i].toCharArray()) {
            int c_val = Character.getNumericValue(c) - Character.getNumericValue('a');          // for 'a' -> 0     for 'z' -> 25
            if(c_val < 0)                                                                       //  spaces, numbers ...
                c_val= new Random().nextInt(25);                                                //add some salt - a random number
            Log.d("value ", c + " -> " + c_val);
            RGB[i] = RGB[i] + c_val;
        }
    }

    int letters_number = Character.getNumericValue('z') - Character.getNumericValue('a');       //  z - a    35 - 10

    // normalizing
    int R = 255 * RGB[0]/sub_string[0].length()/letters_number;
    int G = 255 * RGB[1]/sub_string[1].length()/letters_number;
    int B = 255 * RGB[2]/sub_string[2].length()/letters_number;
    Log.d("R G B", R +" " + G + " "  + B);

    return Color.rgb(R, G, B);
}

Note:
The function does not return the same number(color) each time if your string includes special characters, spaces or numbers. There is some salt there - I asigned random numbers to those characters, just for fun...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文