小编典典

在Android上将int数组转换为Bitmap

java

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


阅读 794

收藏
2020-11-16

共1个答案

小编典典

试试这个,它将为您提供位图。

 // 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));

编辑:

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

希望对您有帮助。

2020-11-16