简体   繁体   English

以这种方式创建新的位图时为什么会得到黑色背景? 如何获得透明的背景?

[英]Why I obtain a black background when I create a new Bitmap in this way? How can I obtain a transparent backround?

I am pretty new in Android development and I have the following problem. 我在Android开发领域还很陌生,但遇到以下问题。

I have implement this code that draw a Bitmap using Canvas (it draw 5 icons one beside each other), so this is my code: 我已经实现了使用Canvas绘制位图的代码(它彼此相邻绘制了5个图标),所以这是我的代码:

@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). 这样就可以正常工作,并正确创建了图像,并显示了5张star.png图像(彼此相邻)。

The only problem is that the background of the new image (behind the star.png showed images) is black. 唯一的问题是新图像的背景(在star.png显示图像之后)是黑色的。 The star.png image have a white background. star.png图像具有白色背景。

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 . 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) (或至少要更改颜色,例如获得白色背景)

In the Android documentation for createBitmap you will find that: 在Android文档createBitmap您会发现:

Android Doc for createBitmap 适用于createBitmap的Android文档

(for the last argument) If the config does not support per-pixel alpha (eg RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF) (对于最后一个参数)如果配置不支持每像素的alpha(例如RGB_565),那么colors []中的alpha字节将被忽略(假定为FF)

So, instead use Bitmap.Config.ARGB_8888 as the last argument. 因此,请改用Bitmap.Config.ARGB_8888作为最后一个参数。

In this configuration, each pixel is stored on 4 bytes. 在此配置中,每个像素存储在4个字节上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作位图的透明背景? - How can I make transparent background of a Bitmap? 为什么当我创建这个新的位图时其背景是深灰色? 如何将其设置为布局背景的相同颜色? - 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? 如何获取发出HTTPRequest的时间? - How can I obtain the time when a HTTPRequest is made? 如何获取 URL 参数? - How can I obtain a URL parameter? 如何获得去电的结果 - How can I obtain the result of an outgoing call 我如何在Java中获取子流程的输出 - how can i obtain the output of a subprocess in java 如何在WebSphere 8.0上获取TransactionManager? - how can i obtain TransactionManager on WebSphere 8.0? 如何获取当前的GPS位置? - How can I obtain the current GPS location? 为什么在尝试为Oracle数据库创建Connection对象时获得此SQLException(没有合适的驱动程序)? - Why I obtain this SQLException (No suitable driver) when I try to create a Connection object for an Oracle database? 当我尝试从File-> New_> Maven Project创建新项目时,出现错误: - When I am trying to create new Project from File->New_>Maven Project, I obtain error:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM