我有一个加载位图,我想转换设置配置为 Bitmap.Config.RGB_565
。是否有转换位图
的位图后,这个配置已经加载到内存中的一个简单的方法?例如,下面我有一个位图之中去$ C $从应用程序的资源光盘,但是,我将如何转换已加载的位图
到 RGB_565
?我敢肯定,这是简单的东西,不过,我是相当新的使用位图和几个小时在网上找工作后,不幸的是我没有找到我所需要的具体。
BitmapFactory.Options选项=新BitmapFactory.Options();
options.in preferredConfig = Bitmap.Config.RGB_565
位= BitmapFactory.de codeResource(getResources(),R.drawable.myphoto,期权);
解决方案
我没有测试过这一点,但它应该工作:
私人位图转换(位图位图,Bitmap.Config配置){
位图convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),配置);
帆布油画=新的Canvas(convertedBitmap);
涂料粉刷=新的油漆();
paint.setColor(Color.BLACK);
canvas.drawBitmap(位图,0,0,油漆);
返回convertedBitmap;
}
这样调用方法:
位图convertedBitmap =转换(位图,Bitmap.Config.RGB_565);
您可以做各种额外的转换像旋转,伸展等,如果你使用的drawBitmap一个矩阵。
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;
}
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.