在Android上将int数组转换为Bitmap

min*_*rus 10 java android opengl-es

我有一个代表颜色的MxN数组(比如RGBA格式,但这很容易改变).我想将它们转换为MxN位图或其他可以渲染到屏幕的其他东西(例如OpenGL纹理).有没有快速的方法来做到这一点?循环遍历数组并将它们绘制到画布上太慢了.

dha*_*ram 17

试试这个它会给你位图.

 // 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));
Run Code Online (Sandbox Code Playgroud)

编辑:

 //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[i]);
        }
        dst.rewind();
        return dst;
    }*/
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助.


小智 10

为什么不使用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);
Run Code Online (Sandbox Code Playgroud)

您可以根据需要使用offset/stride/x/y.
没有循环.没有额外的分配.