How to change a bitmap's opacity?

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?

  • Cropping circular area from bitmap in Android
  • How to add margin to a Button at run time?
  • versionCode vs versionName in Android Manifest
  • Android spinner with underline appcompat
  • part-1 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart
  • Android schedule action
  • Related posts:

    getActionBar() returns null
    Check if directory exist on android's sdcard
    How can I give a style bold in android?
    Remove underline from links in TextView - Android
    Get Touch Coordinates Relative To A View (ScreenToClient Equivalent?)
    Using CursorLoader to get emails causes duplication of emails
  • Incorrect items sizing in layer-list on pre-lollipop
  • Multiple ListRows for each Header on BrowseFragment - Leanback library
  • Android: can't replace one fragment with another
  • Declare EditText as TextView in Android
  • Showing dex method count by package
  • How to get N text that can be fit on Screen/TextView with a specific size?
  • 6 Solutions collect form web for “How to change a bitmap's opacity?”

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


    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();
    // 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);

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

    Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);       
    Paint transparentpainthack = new Paint();
    canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
    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();
            canvas.drawBitmap(src, 0, 0, paint);    
            return transBitmap;

     * @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.

    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)
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.