2010-11-19 15 views
8

Tworzę aplikację i chciałbym ustawić różne kolory za pomocą danych wprowadzanych przez użytkownika (edittext) i wartości szesnastkowych, np. #eeeeee i tak dalej. Problem polega na tym, że nie mogę wymyślić, jak je przekonwertować.Konwersja Java/Android na kolor

Jeśli zrobię coś w tym kodzie, to działa dobrze: titlebar.setBackgroundColor (0xFF545455);

Jednak jeśli odzyskać wartość poprzez EditText powiedzieć „545455” Nie mogę dostać pracy

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

Ktoś ma jakieś pomysły, jak osiągnąć ten cel?

+0

Prawdopodobny duplikat [Konwertowanie ciągów znaków w kolorze android w środowisku wykonawczym na int] (http://stackoverflow.com/questions/3849607/converting -android-string-in-runtime-into-int) –

Odpowiedz

50

Co titlebar.setBackgroundColor(Color.parseColor("#545455"));

+0

+1 nie mógł dać więcej :) – Samuel

+0

this to właściwy sposób na java! –

0
StringBuffer hexString = new StringBuffer(); 
hexString.append(Integer.toHexString(0xFF); 
System.out.print(hexString.toString()); 
0

tu jest mój funkcji, aby uzyskać kolory z dowolny ciąg znaków. Np .: "Hello World!" zwróci ci zielony kolor 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); 
} 

Uwaga: Funkcja nie zwraca ten sam numer (kolor) za każdym razem, jeśli ciąg zawiera specjalne znaki lub cyfry. Jest tam trochę soli - przypisywałem im losowe liczby, tylko dla zabawy ...