Converting array of int to Bitmap on Android

后端 未结 3 1264
谎友^
谎友^ 2021-01-02 02:08

I have an MxN array of ints representing colors (say RGBA format, but that is easily changeable). I would like to convert them to an MxN Bitmap or something else (such as an

3条回答
  •  星月不相逢
    2021-01-02 02:28

    Why not use Bitmap.setPixel? It's even API level 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);
    

    You can play with offset/stride/x/y as needed.
    No loops. No additional allocations.

提交回复
热议问题