3
/**
 * @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;
}

使用adjustOpacity,我使ImageView'Bitmap是半透明的。

Bitmap newBitmap = adjustOpacity(orignalBitmap, 10);
view.setImageBitmap(newBitmap);
view.setBackgroundColor(Color.WHITE);

但是,Imageview显示更暗之前不是白色。如何使用 制作半透明(白色背景)Imageview Bitmap

4

6 回答 6

7
// 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.

于 2014-06-19T18:19:23.197 回答
4

尝试这个。还要注意 setAlpha 在 0-255 范围内

//bmp is your Bitmap object

BitmapDrawable bd = new BitmapDrawable(bmp);
bd.setAlpha(50);

ImageView v = (ImageView) findViewById(R.id.image);
v.setImageDrawable(bd);
于 2013-02-26T12:18:51.313 回答
2

使现有位图透明太简单了。

如果您想在进一步绘制之前使您的位图透明,请按照此操作。

如果您的位图是 mBitmap,那么只需使用:-

mBitmap.eraseColor(Color.TRANSPARENT) 

而已。祝你好运 !!

于 2020-06-18T10:01:59.823 回答
1

添加方法

Bitmap bmp; DrawView DrawView;

private static Bitmap makeTransparentBitmap(Bitmap bmp, int alpha) {
        Bitmap transBmp = Bitmap.createBitmap(bmp.getWidth(),
                bmp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(transBmp);
        final Paint paint = new Paint();
        paint.setAlpha(alpha);
        canvas.drawBitmap(bmp, 0, 0, paint);
        return transBmp;
    }


@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.rgb(43,44,45));

    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mouse);
    canvas.drawBitmap(bmp, pos_x, pos_y, null); 

}

你事件:

bmp = makeTransparentBitmap( bmp, 122 );
DrawView.invalidate();

DrawView

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DrawView = new DrawView(this);
    setContentView( DrawView );
}

我在这里找到了这个答案

于 2020-01-28T21:23:48.310 回答
0

@akaya 的答案是正确的方法,只是构造函数已被弃用。自 API 4 以来的新方法是使用:

BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
drawable.setAlpha(100);
于 2014-11-22T13:51:53.667 回答
0

尝试这个,

newBitmap.setImageResource(android.R.color.transparent);
于 2013-02-26T08:06:10.067 回答