使用 Bitmap.Config.RGB_565 将内存中的 Bitmap 转换为 Bitmap [英] Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565

查看:62
本文介绍了使用 Bitmap.Config.RGB_565 将内存中的 Bitmap 转换为 Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载的位图,我想将其转换为将配置设置为 Bitmap.Config.RGB_565.在位图已加载到内存中后,是否有一种简单的方法可以将 Bitmap 转换为这种配置?例如,下面我有一个从应用程序资源中解码的位图,但是,我如何将已加载的 Bitmap 转换为 RGB_565?我确信这很简单,但是,我对使用位图还很陌生,在网上看了几个小时后,不幸的是我找不到我特别需要的东西.

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 this configuration after the Bitmap is already loaded into memory? For example, below I have a bitmap being decoded from the application resources, however, how would I convert an already loaded Bitmap to RGB_565? I'm sure it's something simple, however, I'm fairly new to working with Bitmaps and after a few hours of looking online, unfortunately I couldn't find what I needed specifically.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myphoto ,options);

推荐答案

我还没有测试过,但它应该可以工作:

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

像这样调用方法:

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

如果您将 drawBitmap 与 Matrix 一起使用,您可以进行各种额外的转换,例如旋转、拉伸等.

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

这篇关于使用 Bitmap.Config.RGB_565 将内存中的 Bitmap 转换为 Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆