2011-11-11 18 views

risposta

73

Purtroppo non so come recuperare il colore reale.

E 'facile ottenere questo come un Drawable

Button button = (Button) findViewById(R.id.my_button); 
Drawable buttonBackground = button.getBackground(); 

Se sai che questo è un colore allora si può provare

ColorDrawable buttonColor = (ColorDrawable) button.getBackground(); 

E se siete su Android 3.0+ si può uscire l'identificativo della risorsa del colore.

int colorId = buttonColor.getColor(); 

E confrontare questo ai colori assegnati, vale a dire.

if (colorID == R.color.green) { 
    log("color is green"); 
} 
+2

Sei sicuro getColor() Ottiene l'ID? Penso che ottenga il valore int reale del colore (cioè 0xAARRGGBB). Ho provato questo con "# 00000001" e ha restituito 1. – brianestey

+1

Nel mio test ho fatto 'button.setBackground (R.color.green)' e quindi ho controllato la risposta e sicuramente non era il vero ID colore. Preferirei un numero intero di colori appropriato, quindi posso 'Color.red (int)', 'Color.green (int)', 'Color.blue (int)' it. Ma nel mio test su Android 3.2, questo non era il caso.Immagino che sia incoerente, restituendo un colore int o resid, a seconda del contesto. –

+1

è solo API 11+ –

2

Per ottenere lo sfondo Drawable, si utilizza

public Drawable getBackground(); 

come definito nella base View di classe.

Non dimenticare che lo Button può avere uno sfondo che è un'immagine, un colore, una sfumatura. Se si utilizza Android: background = "# ffffff", la classe dello sfondo sarà

android.graphics.drawable.ColorDrawable

Da lì si può semplicemente chiamare

public int getColor() 
14

Si può anche provare qualcosa di simile impostare il valore del colore come il tag come

android:tag="#ff0000" 

e accedervi da codice

String colorCode = (String)btn.getTag(); 
19
private Bitmap mBitmap; 
private Canvas mCanvas; 
private Rect mBounds; 

public void initIfNeeded() { 
    if(mBitmap == null) { 
    mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888); 
    mCanvas = new Canvas(mBitmap); 
    mBounds = new Rect(); 
    } 
} 

public int getBackgroundColor(View view) { 
    // The actual color, not the id. 
    int color = Color.BLACK; 

    if(view.getBackground() instanceof ColorDrawable) { 
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
     initIfNeeded(); 

     // If the ColorDrawable makes use of its bounds in the draw method, 
     // we may not be able to get the color we want. This is not the usual 
     // case before Ice Cream Sandwich (4.0.1 r1). 
     // Yet, we change the bounds temporarily, just to be sure that we are 
     // successful. 
     ColorDrawable colorDrawable = (ColorDrawable)view.getBackground(); 

     mBounds.set(colorDrawable.getBounds()); // Save the original bounds. 
     colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds. 

     colorDrawable.draw(mCanvas); 
     color = mBitmap.getPixel(0, 0); 

     colorDrawable.setBounds(mBounds); // Restore the original bounds. 
    } 
    else { 
     color = ((ColorDrawable)view.getBackground()).getColor(); 
    } 
    } 

    return color; 
} 
+3

questa dovrebbe essere la risposta accettata – IHeartAndroid

5

Il modo più semplice per ottenere il il colore per me è:

int color = ((ColorDrawable)button.getBackground()).getColor(); 

Testato e funzionante su Lollipop 5.1.1

0

Prova questo:

list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);   
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground(); 
if(corItem.getColor() == Color.YELLOW){ 
    Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show(); 
    }else{ 
    Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show(); 
} 

o

int color =((ColorDrawable) list_view.getChildAt(position).getBackground()).getColor(); 
Problemi correlati