35

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?

Jona
  • 13,325
  • 15
  • 86
  • 129
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

6 Answers6

80

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().

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Given that `BitmapDrawable.setAlpha(int)` is just doing the same what you described here. Also you can set complex color filters on the bitmap itself, using `Paint.setColorFilter(ColorFIlter)`. – Avinash R Apr 26 '16 at 07:52
35

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);
Rajesh
  • 521
  • 5
  • 19
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • i am using BitmapDrawable to set the alpha it is success but after applying the alpha resulted drawable image saved to my sd card.how to store resulted drawable?Thanks in advance – Rajesh Dec 05 '15 at 11:09
  • @Rajesh you need to draw the bitmap with alpha on another empty bitmap and store that in your application folder. – WarrenFaith Dec 07 '15 at 09:15
  • Thank for your answer,i am not understand your answer.this is my code BitmapDrawable drawable=new BitmapDrawable(getResources(),bitmap); drawable.setAlpha(42); how can i save the drawable into my device?please provide any sample code for this.Thanks – Rajesh Dec 09 '15 at 05:58
  • @Rajesh I added some code, but you could have done this with some simple search by yourself – WarrenFaith Dec 09 '15 at 09:41
  • ,Thank You for your answer – Rajesh Dec 09 '15 at 10:12
22
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;
}
Ruban
  • 1,514
  • 2
  • 14
  • 21
18
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);       
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
Chris Knight
  • 24,333
  • 24
  • 88
  • 134
Martin
  • 181
  • 1
  • 2
4

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.

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • what is meaning of int colour = (opacity & 0xFF) << 24; ? this is not transparing image ..this is changing opactity to black color – BiRjU Apr 15 '22 at 09:09
  • @BiRjU (opacity & 0xFF) << 24 is: take the 8 least-significant bits of opacity (that is, opacity%256), and shift them into the most-significant byte. In fact, just opacity<<24 would do, because the inserted least-significant bits are all zeroes, and the bits cleared by &0xFF are anyway shifted out (that is, if you pass 258 = 0x102, after the shift you will have 0x1_02_00_00_00, but since int is a 32-bit value, the 1 will be "shifted out", and you will get 0x02_00_00_00). With &0xFF, you get: 0x102&0xFF = 0x02 = 2, and then 0x02 << 24 = 0x02_00_00_00, that is, the same value. – 18446744073709551615 Apr 15 '22 at 10:49
0

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);
    }
}
Frédéric
  • 77
  • 2