2011-10-21 3 views
8

色を表すint型のMxN配列を持っています(たとえばRGBA形式ですが、簡単に変更可能です)。私は、それらをMxNのビットマップや、スクリーンにレンダリングできる他のもの(OpenGLテクスチャなど)に変換したいと思っています。これを行うには速い方法がありますか?配列をループしてキャンバスに描画するのは非常に遅いです。intの配列をAndroidのビットマップに変換する

答えて

15

それはあなたにビットマップを与える、これを試してみてください。

// 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; 
    }*/ 

は、それはあなたを助けることを願っています。

+0

どこかの 'makeBuffer'にバグがあります。代わりに、この方法でビットマップを埋めてください: 'bmp.copyPixelsFromBuffer(IntBuffer.wrap(vector));' –

3

あなたが必要とするすべての情報を持っているようです。 Mが幅でNが高さである場合、Bitmap.createBitmapで新しいビットマップを作成し、int配列をとるsetPixelsメソッドでARGB値を入力できます。

Bitmap.createBitmap

Bitmap.setPixels

8

なぜ使用しないのですかBitmap.setPixel?それは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); 

必要に応じてオフセット/ストライド/ x/yで再生できます。
ループがありません。追加の割り当てはありません。

関連する問題