Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565

后端 未结 3 940
囚心锁ツ
囚心锁ツ 2020-12-25 14:18

I have a loaded Bitmap which I would like to convert to set the config to Bitmap.Config.RGB_565. Is there a simple way of converting a Bitmap to th

3条回答
  •  情话喂你
    2020-12-25 14:57

    I haven't tested this but it should work:

    private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
        Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
        Canvas canvas = new Canvas(convertedBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return convertedBitmap;
    }
    

    call the methods like this:

    Bitmap convertedBitmap = convert(bitmap, Bitmap.Config.RGB_565);
    

    You can do all kinds of additional transformations like rotating, stretching etc. if you use the drawBitmap with a Matrix.

提交回复
热议问题