如何更改位图的不透明度? [英] How to change a bitmap's opacity?
本文介绍了如何更改位图的不透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个位图:
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
但我不会向用户显示图像.我希望 alpha 为 100(共 255 个).如果这是不可能的,我可以设置 Bitmap
的不透明度吗?
But I'm not going to display the image to the user. I want the alpha to be 100 (out of 255). If this is not possible, can I set the opacity of the Bitmap
?
推荐答案
你也可以试试 BitmapDrawable 而不是 Bitmap
.如果这对您有用取决于您使用位图的方式...
You could also try BitmapDrawable instead of Bitmap
. If this is useful for you depends on the way you use the bitmap...
编辑
当一位评论者问他如何用 alpha 存储位图时,这里有一些代码:
As a commenter asked how he can store the bitmap with alpha, here is some code:
// lets create a new empty bitmap
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
// create a canvas where we can draw on
Canvas canvas = new Canvas(newBitmap);
// create a paint instance with alpha
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(42);
// now lets draw using alphaPaint instance
canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint);
// now lets store the bitmap to a file - the canvas has drawn on the newBitmap, so we can just store that one
// please add stream handling with try/catch blocks
FileOutputStream fos = new FileOutputStream(new File("/awesome/path/to/bitmap.png"));
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
这篇关于如何更改位图的不透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文