How to change a bitmap's opacity?

2019-01-04 02:28发布

I have a bitmap:

Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");

But I'm not going to display the image to the user. I want the alpha to be 100 (out of 255). If this is not possible, can I set the opacity of the Bitmap?

6条回答
男人必须洒脱
2楼-- · 2019-01-04 02:39
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);       
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
查看更多
叛逆
3楼-- · 2019-01-04 02:39
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;
}
查看更多
虎瘦雄心在
4楼-- · 2019-01-04 02:39

https://dzone.com/articles/adjusting-opacity-android proposes:

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

Note that with DST_IN you can modify (rather than reset) the transparency of already transparent image, that is, you can make the image more and more transparent.

查看更多
叼着烟拽天下
5楼-- · 2019-01-04 02:41

If you are using a Drawable to display the image, you can change the alpha as follows:

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);
    }
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-04 02:50

As far as I know, opacity or other color filters can't be set on the Bitmap itself. You will need to set the alpha when you use the image:

If you're using ImageView, there is ImageView.setAlpha().

If you're using a Canvas, then you need to use Paint.setAlpha():

Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);

Also, incorporating WarrenFaith's answer, if you will use the Bitmap where a drawable is required, you can use BitmapDrawable.setAlpha().

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-04 02:59

You could also try BitmapDrawable instead of Bitmap. If this is useful for you depends on the way you use the bitmap...

Edit

As a commenter asked how he can store the bitmap with alpha, here is some code:

// 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);
查看更多
登录 后发表回答