2011-09-19 11 views
9

Ho un oggetto RelativeLayout e voglio modificare dinamicamente l'immagine di sfondo con un oggetto Bitmap creato dinamicamente (cambia il suo colore dinamicamente).Come usare relativelayout.setBackgroundDrawable() con una bitmap?

Ho visto che quando volevo aggiornare l'immagine di sfondo dell'oggetto RelativeLayout, posso scegliere solo setBackgroundDrawable() che richiede un oggetto Drawable come parametro.

La mia domanda è, come posso convertire l'oggetto Bitmap creato dinamicamente in un oggetto Drawable?

+0

l'immagine proviene dalla sorgente locale o dall'URL? –

risposta

19

BitmapDrawable(obj) convertire oggetto bitmap in oggetto disegnabile.

Prova:

RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1); 
Drawable dr = new BitmapDrawable(bit); 
(view).setBackgroundDrawable(drawable); 

spero che questo vi aiuterà.

+1

nuovo BitmapDrawable (bitmap) è deprecato; usa il nuovo BitmapDrawable (getResources(), bitmap) –

+0

Questa risposta è stata data più di prima di 3 anni. Quindi potrebbe avere la più alta probabilità di deprecare i vecchi metodi quando nuovi metodi entrano in scena. A quel tempo era giusto. – Siten

3

Prova questo,

Drawable drawable = new BitmapDrawable(bitmap); 
1
Drawable d = new BitmapDrawable(bitmap); 
6

Si può fare in questo modo

Drawable drawable = new BitmapDrawable(bitmap); 
RelativeLayout r; 
r = (RelativeLayout) findViewById(R.id.relativelayout1); 
ll.setBackgroundDrawable(drawable); 
2
RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1); 
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
Drawable dr = new BitmapDrawable(myImage); 
rl.setBackgroundDrawable(dr); 
0

// Crea Domanda RelativeLayout

RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl); 

// imposta Contesto riferimento layout utilizzando il seguente metodo

Drawable drawable = getResources().getDrawable(R.drawable.bg); 
layout.setBackground(drawable); 

Nota: R.id.rl è id RelativeLayout

R.drawable.bg id Immagine in drawabl e cartella

Problemi correlati