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

我有一个位图:

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

但是我不打算把图像显示给用户。 我想alpha是100(255)。 如果这是不可能的,我可以设置Bitmap的不透明度吗?

您也可以尝试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); 

据我所知,不透明度或其他颜色filter不能在位图本身设置。 您在使用图像时需要设置alpha:

如果您使用ImageView,则有ImageView.setAlpha() 。

如果你正在使用Canvas,那么你需要使用Paint.setAlpha() :

 Paint paint = new Paint(); paint.setAlpha(100); canvas.drawBitmap(bitmap, src, dst, paint); 

另外,结合WarrenFaith的答案,如果您将使用需要drawable的位图,则可以使用BitmapDrawable.setAlpha() 。

 Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2); Paint transparentpainthack = new Paint(); transparentpainthack.setAlpha(100); canvas.drawBitmap(bgr, 0, 0, transparentpainthack); 
 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; } 

https://dzone.com/articles/adjusting-opacity-androidbuild议:;

 /** * @param bitmap The source bitmap. * @param opacity a value between 0 (completely transparent) and 255 (completely * opaque). * @return The opacity-adjusted bitmap. If the source bitmap is mutable it will be * adjusted and returned, otherwise a new bitmap is created. */ private Bitmap adjustOpacity(Bitmap bitmap, int opacity) { Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); int colour = (opacity & 0xFF) << 24; canvas.drawColor(colour, PorterDuff.Mode.DST_IN); return mutableBitmap; } 

请注意,使用DST_IN可以修改(而不是重置)已经透明的图像的透明度,也就是说,您可以使图像变得越来越透明。

如果您使用Drawable来显示图像,则可以按如下方式更改Alpha:

 private Drawable mTriangle; mTriangle = context.getResources().getDrawable(R.drawable.triangle_arrow_for_radar); ... protected void onDraw(Canvas canvas) { // Draw the triangle arrow float imageTargetWidth = getWidth() / 15; float scale = mTriangle.getIntrinsicWidth() / imageTargetWidth; int imgWidth = (int)(imageTargetWidth); int imgHeight = (int)(mTriangle.getIntrinsicHeight() / scale); if (mTriangle != null) { mTriangle.setBounds(getWidth() / 2 - imgWidth / 2, getHeight() / 2 - imgHeight / 2, getWidth() / 2 + imgWidth / 2, getHeight() / 2 + imgHeight / 2); mTriangle.setAlpha(150); // from (transparent) to 255 (opaque) mTriangle.draw(canvas); } }