1

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)

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    Change that to `Bitmap.config.ARGB_8888`. Because transparency is defined by the Alpha channel (The "A" in the ARGB). With "RGB" There is no Alpha Channel => No Transparency – wanpanman Jul 25 '16 at 15:27
  • Also make sure the `png`'s themselves have transparent background...`white` is not transparent – boxed__l Jul 25 '16 at 15:30
  • RGB_565 means you use 5 bits for red, 6 for green, 5 for blue, and 0 for alpha. It makes a smaller bitmap in bytes but less color depth and no transparency. FOr transparency you generally use ARGB_8888- 8 for each color and 8 for transparency. – Gabe Sechan Jul 25 '16 at 15:35

1 Answers1

2

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.

boxed__l
  • 1,334
  • 1
  • 10
  • 24