简体   繁体   English

为什么当我创建这个新的位图时其背景是深灰色? 如何将其设置为布局背景的相同颜色?

[英]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? 如何解决此问题?

Method 1 方法1

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). 您看到的背景颜色仅为黑色,这就是这种类型的空新位图将被初始化为(全零)的情况。

Method 2 方法二

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.

相关问题 以这种方式创建新的位图时为什么会得到黑色背景? 如何获得透明的背景? - Why I obtain a black background when I create a new Bitmap in this way? How can I obtain a transparent backround? 如何将所有 JButtons 的默认鼠标按下背景颜色覆盖为其当前背景颜色的较暗阴影? - How can I override the default mousepressed background color of all JButtons into a darker shade of its current background color? 如何制作位图的透明背景? - How can I make transparent background of a Bitmap? Java GUI在运行时设置背景色,保持灰色,然后显示背景色 - Java GUI set background color when run keep having grey color then display the background color 如何在JList中设置背景颜色和字体大小? - How can I set the background color and font size in JList? 如何允许用户设置背景颜色? - How can I allow the user to set the background color? 如何在XWPFTableCell上设置自定义背景色? - how can I set custom background color on a XWPFTableCell? 如何在TableView中为特定行设置背景颜色 - How can I set the background color for a specific row in my TableView 如何在 libgdx 中设置背景颜色? - How do I set background color in libgdx? 如何为JButton设置“非常深的蓝色”背景色? - How to set “ very dark blue ” background color to the JButton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM