在 Android 上将 int 数组转换为位图
我有一个MxN整数数组来表示颜色(比如RGBA格式,但这很容易改变)。我想将它们转换为MxN位图或其他可以渲染到屏幕上的东西(例如OpenGL纹理)。有没有一种快速的方法可以做到这一点?循环遍历数组并将其绘制到画布上太慢了。
我有一个MxN整数数组来表示颜色(比如RGBA格式,但这很容易改变)。我想将它们转换为MxN位图或其他可以渲染到屏幕上的东西(例如OpenGL纹理)。有没有一种快速的方法可以做到这一点?循环遍历数组并将其绘制到画布上太慢了。
试试这个,它会给你位图:
 // 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));
或者,您可以从以下本机方法生成:IntBuffer
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;
}
						为什么不使用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。
无循环。无额外分配。