Moh*_*nde 31 java android alpha bitmap opacity
我有一个位图:
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
Run Code Online (Sandbox Code Playgroud)
但我不打算向用户显示图像.我希望alpha为100(满分为255).如果这不可能,我可以设置不透明度Bitmap吗?
Mat*_*lis 76
据我所知,不能在Bitmap本身上设置不透明度或其他颜色过滤器.使用图像时需要设置alpha:
如果您使用的是ImageView,则会有ImageView.setAlpha().
如果您使用的是Canvas,那么您需要使用Paint.setAlpha():
Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);Run Code Online (Sandbox Code Playgroud)
另外,结合WarrenFaith的答案,如果你将使用需要drawable的Bitmap,你可以使用BitmapDrawable.setAlpha().
War*_*ith 28
您也可以尝试使用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);
Run Code Online (Sandbox Code Playgroud)
Rub*_*ban 19
public Bitmap makeTransparent(Bitmap src, int value) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap transBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(transBitmap);
canvas.drawARGB(0, 0, 0, 0);
// config paint
final Paint paint = new Paint();
paint.setAlpha(value);
canvas.drawBitmap(src, 0, 0, paint);
return transBitmap;
}
Run Code Online (Sandbox Code Playgroud)
小智 17
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
56888 次 |
| 最近记录: |