(Android) Change background of a bitmap

By | July 14, 2014
  1. Create a new bitmap, with the size and config of the bitmap you wish to change.
  2. Create a Canvas and prepare it to paint to the new bitmap.
  3. Paint the new bitmap with your color using the canvas object.
  4. Draw the old bitmap to the new bitmap through the canvas object.
  5. Do what you want with the new bitmap
// Create new bitmap based on the size and config of the old
Bitmap newBitmap = Bitmap.createBitmap(oldBitmap.getWidth(), oldBitmap.getHeight(), oldBitmap.getConfig()); 

// Instantiate a canvas and prepare it to paint to the new bitmap
Canvas canvas = new Canvas(newBitmap);

// Paint it white (or whatever color you want)
canvas.drawColor(Color.WHITE);

// Draw the old bitmap ontop of the new white one
canvas.drawBitmap(oldBitmap, 0, 0, null);

2 thoughts on “(Android) Change background of a bitmap

  1. Nilesh Maheshwari

    After literally scrolling through dozens of articles, this finally gets me the solution. Everyone gave me the complete code except for one very crucial part –
    Bitmap newBitmap = Bitmap.createBitmap(oldBitmap.getWidth(), oldBitmap.getHeight(), oldBitmap.getConfig());

    Apparently, I was overwriting the same thing. Thanks a lot for this bud!

    Reply

Leave a Reply to Andrey Cancel reply

Your email address will not be published. Required fields are marked *