I am pretty new in Android and I have the following problem.
I create this immage:
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?
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 of this type will be initialized to (all zeros).
Use a bitmap configuration that supports transparency:
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 7, myBitmap.getHeight(), Bitmap.Config.ARGB_8888);
In this case the bitmap will be initialized to transparent black (all zeros again) and anything behind it will be visible where the icon is not drawn.
The difference between the two methods is that transparency requires a bitmap with an alpha channel. Which method is preferred will depend on other details of your application.
RGB_565
, for example, is more compact than ARGB_8888
(but so is ARGB_4444
which does support transparency).
Using transparency can also slow down animations, because partially covered views need to be redrawn more often.