Android的半透明位图的背景是黑色的 [英] android semitransparent bitmap background is black
问题描述
我试图创建半透明位图,我用这个code:
I'm trying to create semitransparent bitmaps, I used this code:
private Bitmap SemiTransparent(Bitmap bitmap, int opacity) {
// Create an array to hold the data of bitmap for which semi transparent bitmap is to be obtained
int[] data = new int[(int)(bitmap.getWidth()) + 1];
for (int y = 0; y < bitmap.getHeight(); ++y)
{
// Get a single line of data
bitmap.getPixels(data, 0, bitmap.getWidth(), 0, y,bitmap.getWidth(), 1);
// Reset the alpha values
for (int x = bitmap.getWidth(); x>0; --x)
{
data[x] = (data[x] & 0x00ffffff) | (opacity << 24);
}
//fill alphaBitmap data
bitmap.setPixels(data, 0, bitmap.getWidth(), 0, y, bitmap.getWidth(), 1);
}
return bitmap;
}
唯一的问题是背景(透明)的颜色总是黑色的 - 我怎么能修改此使背景色彩完全透明的,其余仍半透明
The only problem is the background (transparent) color is always black - how can I modify this to make the background Color completely transparent and the rest still semitransparent?
推荐答案
使用这个方法做一个透明位图:
Use this method to make a bitmap transparent:
/**
* @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.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;
}
在这里看到一个解释:
的http://blog.uncommons.org/2011/01/12/adjusting-the-opacity-of-an-android-bitmap/
在博客-后,检验该位是可变的或不 - 这可能会导致问题,如果该位是可变的,但是不具备的配置ARGB_8888 - 所以用我的方法,而不是
In the blog-post it checks if the bitmap is mutable or not - which can cause problems if the bitmap is mutable but doesn't have the config ARGB_8888 - so use my method instead.
这篇关于Android的半透明位图的背景是黑色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!