2011-10-21 15 views
8

Ho una matrice MxN di int che rappresenta i colori (ad esempio il formato RGBA, ma è facilmente modificabile). Mi piacerebbe convertirli in un Bitmap MxN o qualcos'altro (come una trama OpenGL) che posso renderizzare sullo schermo. C'è un modo veloce per farlo? Passare attraverso l'array e trascinarli sulla tela è troppo lento.Conversione di array di int in Bitmap su Android

risposta

15

Prova questo vi darà il bitmap.

// You are using RGBA that's why Config is ARGB.8888 
    bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); 
// vector is your int[] of ARGB 
    bitmap.copyPixelsFromBuffer(IntBuffer.wrap(vector)); 

EDIT:

//OR , you can generate IntBuffer from following native method 
    /*private IntBuffer makeBuffer(int[] src, int n) { 
     IntBuffer dst = IntBuffer.allocate(n*n); 
     for (int i = 0; i < n; i++) { 
      dst.put(src); 
     } 
     dst.rewind(); 
     return dst; 
    }*/ 

Spero che vi aiuterà.

+0

C'è un bug nel tuo 'makeBuffer' da qualche parte. Riempi la bitmap in questo modo: 'bmp.copyPixelsFromBuffer (IntBuffer.wrap (vector));' –

3

Sì, sembra che tu abbia tutte le informazioni di cui hai bisogno. Se M è la larghezza e N è l'altezza, puoi creare una nuova bitmap con Bitmap.createBitmap e puoi inserire i valori ARGB con il metodo setPixels che accetta un array int.

Bitmap.createBitmap

Bitmap.setPixels

8

Perché non utilizzare Bitmap.setPixel? È anche API 1:

int[] array = your array of pixels here... 
int width = width of "array"... 
int height = height of "array"... 

// Create bitmap 
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

// Set the pixels 
bitmap.setPixels(array, 0, width, 0, 0, width, height); 

È possibile giocare con offset/falcata/x/y secondo necessità.
Nessun anello. Nessuna allocazione aggiuntiva.

Problemi correlati