【发布时间】:2011-07-04 09:12:47
【问题描述】:
我有一张位图:
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
但我不会向用户显示图像。我希望 alpha 为 100(255 中)。如果这不可能,我可以设置Bitmap 的不透明度吗?
【问题讨论】:
标签: java android bitmap opacity alpha
我有一张位图:
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
但我不会向用户显示图像。我希望 alpha 为 100(255 中)。如果这不可能,我可以设置Bitmap 的不透明度吗?
【问题讨论】:
标签: java android bitmap opacity alpha
如果您使用 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);
}
}
【讨论】:
https://dzone.com/articles/adjusting-opacity-android提议:
/**
* @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可以修改(而不是重置)已经透明图片的透明度,也就是可以让图片越来越透明。
【讨论】:
您也可以尝试使用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);
【讨论】:
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;
}
【讨论】:
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
【讨论】:
据我所知,不能在位图本身上设置不透明度或其他颜色过滤器。使用图像时需要设置 alpha:
如果您使用的是 ImageView,则有 ImageView.setAlpha()。
如果您使用的是 Canvas,则需要使用 Paint.setAlpha():
Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);
此外,结合 WarrenFaith 的回答,如果您将在需要可绘制对象的地方使用位图,您可以使用 BitmapDrawable.setAlpha()。
【讨论】:
BitmapDrawable.setAlpha(int) 所做的与您在此处描述的相同。您还可以使用Paint.setColorFilter(ColorFIlter) 在位图本身上设置复杂的颜色过滤器。