Why I obtain a black background when I create a new Bitmap in this way? How can I obtain a transparent backround?

2.2k Views Asked by At

I am pretty new in Android development and I have the following problem.

I have implement this code that draw a Bitmap using Canvas (it draw 5 icons one beside each other), so this is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Retrieve the ImageView having id="star_container" (where to put the star.png image):
    ImageView myImageView = (ImageView) findViewById(R.id.star_container);

    // Create a Bitmap image startin from the star.png into the "/res/drawable/" directory:
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star);

    // Create a new image bitmap having width to hold 5 star.png image:
    Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565);

    /* Attach a brand new canvas to this new Bitmap.
       The Canvas class holds the "draw" calls. To draw something, you need 4 basic components:
       1) a Bitmap to hold the pixels.
       2) a Canvas to host the draw calls (writing into the bitmap).
       3) a drawing primitive (e.g. Rect, Path, text, Bitmap).
       4) a paint (to describe the colors and styles for the drawing).
     */
    Canvas tempCanvas = new Canvas(tempBitmap);

    // Draw the image bitmap into the cavas:
    tempCanvas.drawBitmap(myBitmap, 0, 0, null);
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth(), 0, null);
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 2, 0, null);
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 3, 0, null);
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 4, 0, null);

    myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));

}

So it works fine and the image is correctly created and the 5 star.png images are showed, one beside each other).

The only problem is that the background of the new image (behind the star.png showed images) is black. The star.png image have a white background.

I think it depends by this line:

// Create a new image bitmap having width to hold 5 star.png image:
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565);

In particolar by Bitmap.Config.RGB_565.

What exactly means?

How can I change this value to obtain a transparent background? (or at least to change color, for example to obtain a white background)

1

There are 1 best solutions below

0
boxed__l On

In the Android documentation for createBitmap you will find that:

Android Doc for createBitmap

(for the last argument) If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)

So, instead use Bitmap.Config.ARGB_8888 as the last argument.

In this configuration, each pixel is stored on 4 bytes.