[英]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. 我在Android上还很陌生,但遇到以下问题。
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. 效果很好,唯一的问题是在一个chef_hat_ok_resize.png图像和下一个图像之间的空间中,该空白空间为灰色暗色。
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 在调用
drawBitmap
之前, drawBitmap
插入
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. 两种方法之间的区别在于,透明度需要具有alpha通道的位图。 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). RGB_565
,例如,比更紧凑ARGB_8888
(但所以是ARGB_4444
其不支持透明度)。
Using transparency can also slow down animations, because partially covered views need to be redrawn more often. 使用透明度还会减慢动画的速度,因为部分覆盖的视图需要更频繁地重绘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.