How to change a bitmap's opacity?
Asked Answered
C

6

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?

Christos answered 25/2, 2011 at 15:13 Comment(1)
see this: dzone.com/articles/adjusting-opacity-androidSquarerigger
H
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);
Hewitt answered 25/2, 2011 at 15:17 Comment(4)
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 advancePortillo
@Portillo you need to draw the bitmap with alpha on another empty bitmap and store that in your application folder.Hewitt
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.ThanksPortillo
@Portillo I added some code, but you could have done this with some simple search by yourselfHewitt
S
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().

Shimmy answered 25/2, 2011 at 15:30 Comment(1)
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).Butterfat
H
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);
Hewitt answered 25/2, 2011 at 15:17 Comment(4)
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 advancePortillo
@Portillo you need to draw the bitmap with alpha on another empty bitmap and store that in your application folder.Hewitt
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.ThanksPortillo
@Portillo I added some code, but you could have done this with some simple search by yourselfHewitt
S
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;
}
Smear answered 24/2, 2014 at 10:49 Comment(0)
R
18
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);       
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
Runlet answered 13/9, 2011 at 20:37 Comment(0)
B
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.

Bonnes answered 7/2, 2016 at 6:38 Comment(2)
what is meaning of int colour = (opacity & 0xFF) << 24; ? this is not transparing image ..this is changing opactity to black colorGupta
@Gupta (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.Bonnes
C
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);
    }
}
Cheddite answered 3/9, 2016 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.