如何使位图透明? [英] How to make bitmap transparent?
问题描述
/ **
* @参数位图
*源位图。
* @参数不透明度
*介于0(完全透明)和255(完全值
*不透明)。
返回:不透明度调整位图。如果源位图是可变它
*将调整并返回,否则新的位图被创建。
*资料来源:http://stackoverflow.com/questions/7392062/android-
*半透明位图,背景为黑色/ 14858913#14858913
* /
私人位图adjustOpacity(位图位图,诠释不透明度){
位图mutableBitmap = bitmap.isMutable()?位图:bitmap.copy(Bitmap.Config.ARGB_8888,真);
帆布帆布=新的Canvas(mutableBitmap);
INT颜色=(不透明度和放大器; 0xFF的)LT;< 24;
canvas.drawColor(颜色,PorterDuff.Mode.DST_IN);
返回mutableBitmap;
}
使用 adjustOpacity
,我让的ImageView
的位图
是半透明的。
位图newBitmap = adjustOpacity(orignalBitmap,10);
view.setImageBitmap(newBitmap);
view.setBackgroundColor(Color.WHITE);
然而,的ImageView
显示之前不白更darkent。我提出与位图半透明(白色背景)的ImageView怎么办
//转换transparentColor是在一个透明位图。
公共静态位图makeTransparent(位图位,颜色transparentColor){
INT宽度= bit.getWidth();
INT高度= bit.getHeight();
位图MYBITMAP = Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
INT [] allpixels =新INT [myBitmap.getHeight()* myBitmap.getWidth()];
bit.getPixels(allpixels,0,myBitmap.getWidth(),0,0,myBitmap.getWidth(),myBitmap.getHeight());
myBitmap.setPixels(allpixels,0,宽度,0,0,宽度,高度); 的for(int i = 0; I< myBitmap.getHeight()* myBitmap.getWidth();我++){
如果(allpixels [I] == transparentColor) allpixels [I] = Color.alpha(Color.TRANSPARENT);
} myBitmap.setPixels(allpixels,0,myBitmap.getWidth(),0,0,myBitmap.getWidth(),myBitmap.getHeight());
返回MYBITMAP;
}
以上code将采取位图,并返回一个位图,每一个颜色非常transparentColor像素是透明的。这在API低至8级,我也没有任何降低了测试。
我通常使用类似Color.RED让我透明的像素,因为我没有在我的资产使用大量RED的,但如果我这样做,是一个自定义的红色。
/**
* @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.
* Source : http://stackoverflow.com/questions/7392062/android-
* semitransparent-bitmap-background-is-black/14858913#14858913
*/
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;
}
Using adjustOpacity
, I make ImageView
's Bitmap
be semi-transparent.
Bitmap newBitmap = adjustOpacity(orignalBitmap, 10);
view.setImageBitmap(newBitmap);
view.setBackgroundColor(Color.WHITE);
However, Imageview
show more darkent before not white. How do I make a semi-transparent (white background) Imageview with Bitmap
?
// Convert transparentColor to be transparent in a Bitmap.
public static Bitmap makeTransparent(Bitmap bit, Color transparentColor) {
int width = bit.getWidth();
int height = bit.getHeight();
Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int [] allpixels = new int [ myBitmap.getHeight()*myBitmap.getWidth()];
bit.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),myBitmap.getHeight());
myBitmap.setPixels(allpixels, 0, width, 0, 0, width, height);
for(int i =0; i<myBitmap.getHeight()*myBitmap.getWidth();i++){
if( allpixels[i] == transparentColor)
allpixels[i] = Color.alpha(Color.TRANSPARENT);
}
myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
return myBitmap;
}
The above code will take a Bitmap, and return a Bitmap where every pixel which the color is transparentColor is transparent. This works in API as low as level 8, and I have not tested it in any lower.
I typically use something like Color.RED to make my transparent pixels, because I don't use a lot of RED in my assets, but if I do it's a custom red color.
这篇关于如何使位图透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!