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?

  • Difference between SurfaceView and GLSurfaceView in Android
  • Using javamail to send from hotmail?
  • How to add scrolling to the view when screen orientation is changed to landscape?
  • howto in eclipse debug the Proguard obfuscated apk file
  • Calculating the distance between two latitude and longitude points in android
  • Static android shortcuts for multiple flavors?
  • Related posts:

    How to add dividers between specific menu items?
    Android OAuth2 Bearer token best practices
    How do I vertically align an item within a list using relative layout?
    Do GCM registration id's expire?
    How do you implement context menu in a ListActivity on Android?
    Margin between items in recycler view Android
  • App seems to stop working when the screen goes to sleep
  • Limitations of Cordova PhoneGap
  • Force lint to check translations on exact languages
  • Android: Lint says - Class referenced in the layout file, was not found in the project or the libraries
  • react-native :app:installDebug FAILED
  • Retrofit callback on main thread
  • 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.