AndreaNobili AndreaNobili - 2 months ago 7
Android Question

Why when I create this new Bitmap its background is dark grey? How can I set it to the same color of the layout background?

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

I create this immage:

enter image description here

using this method:

public static Bitmap createRankingImg(Context context, int difficulty) {

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


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

Canvas tempCanvas = new Canvas(tempBitmap);

// Draw the image bitmap into the cavas:
tempCanvas.drawBitmap(myBitmap, 0, 0, null); // FROM 0 TO 1
tempCanvas.drawBitmap(myBitmap, (float) (myBitmap.getWidth() * 1.5), 0, null); // FROM 1.5 TO 2.5
tempCanvas.drawBitmap(myBitmap, (float) ( myBitmap.getWidth() * 3), 0, null); // FROM 3 TO 4
tempCanvas.drawBitmap(myBitmap, (float) (myBitmap.getWidth() * 4.5), 0, null); // FROM 4.5 TO 5.5
tempCanvas.drawBitmap(myBitmap, (float) (myBitmap.getWidth() * 6), 0, null); // FROM 6 TO 7


return tempBitmap;

}


It works quite fine the only problem is that in the space between one chef_hat_ok_resize.png image and the next one the empty space have a grey dark color.

I want that it have the same color of the layout backgroud (white).

I think that maybe it could depend by this line:

Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 7, myBitmap.getHeight(), Bitmap.Config.RGB_565);


Why? What am I missing? How can I fix this issue?

Answer

Before your drawBitmap calls, insert

tempCanvas.drawColor(Color.WHITE);

The background color you are seeing is just black, which is what an empty new bitmap will be initialized to (all zeros).

Comments