我有一個位圖:如何更改位圖的不透明度?
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
但我不會顯示圖像給用戶。我希望alpha是100(255)。如果這是不可能的,我可以設置Bitmap
的不透明度嗎?
我有一個位圖:如何更改位圖的不透明度?
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
但我不會顯示圖像給用戶。我希望alpha是100(255)。如果這是不可能的,我可以設置Bitmap
的不透明度嗎?
您也可以嘗試BitmapDrawable而不是Bitmap
。如果你要看你用位圖的方式,這是非常有用...
編輯
作爲一個評論者問他怎麼可以存儲與阿爾法位圖,下面是一些代碼:
// 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);
據我所知,不透明度或其他顏色過濾器不能在位圖本身設置。您需要在使用圖像時設置Alpha:
如果您使用ImageView,則有ImageView.setAlpha()。
如果您使用的是畫布,那麼你需要使用Paint.setAlpha():
Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);
此外,結合WarrenFaith的答案,如果你將使用位圖在需要繪製,可以使用BitmapDrawable.setAlpha()。
對錢! – Li3ro 2014-10-11 09:47:11
鑑於'BitmapDrawable.setAlpha(int)'與您在這裏描述的相同。你也可以使用'Paint.setColorFilter(ColorFIlter)'在位圖本身上設置複雜的顏色過濾器。 – 2016-04-26 07:52:54
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-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可以修改(而不是復位)已經透明圖像的透明度,也就是,你可以使圖像越來越多透明。
如果您使用的是可繪製顯示圖像,您可以按如下改變阿爾法:
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);
}
}
我使用BitmapDrawable設置alpha它是成功的,但應用的α-後導致保存到繪製的圖像我的SD card.how存儲結果drawable?在此先感謝 – Rajesh 2015-12-05 11:09:39
@Rajesh您需要在另一個空位圖上繪製帶有alpha的位圖並將其存儲在您的應用程序文件夾中。 – WarrenFaith 2015-12-07 09:15:30
感謝您的回答,我不明白你的答案。這是我的代碼BitmapDrawable drawable = new BitmapDrawable(getResources(),bitmap); drawable.setAlpha(42);我怎樣才能將繪圖保存到我的設備?請提供任何示例代碼爲此。謝謝 – Rajesh 2015-12-09 05:58:51