[英]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.