小编典典

如何更改位图的不透明度?

java

我有一个位图:

Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");

但我不会将图像显示给用户。我希望Alpha为100(总共255)。如果这不可能,我可以设置透明度Bitmap吗?


阅读 258

收藏
2020-09-08

共1个答案

小编典典

您也可以尝试使用BitmapDrawable代替Bitmap。如果这对您有用,则取决于您使用位图的方式…

编辑

正如评论者所问的那样,他如何使用alpha存储位图,下面是一些代码:

// 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);
2020-09-08