[英]Android: drawing an arraylist of Bitmaps results in a black background
it's my first time asking a question here so I'll try to stay on-topic. 这是我第一次在这里提问,所以我将尽量保持话题。 I'm trying to randomly generate a background by creating an appropriately-sized ArrayList of Bitmap objects, and drawing them in order.
我试图通过创建适当大小的Bitmap对象的ArrayList并按顺序绘制它们来随机生成背景。 This implementation works fine loading a single Bitmap, by the way;
顺便说一下,该实现可以很好地加载单个Bitmap。 it's just stumbling with a list.
它只是在绊脚石。
Before I get to the code, I'd like to point out that Ideally I would make a single Bitmap by adding the individual pixels or tiles, and indeed have tried a few variations of that, but they all result in black screens; 在开始编写代码之前,我想指出一下,理想情况下,我将通过添加单个像素或图块来制作单个位图,的确尝试了一些变化,但它们都会导致黑屏。 I'm starting to think it might be a problem with how I draw to the Canvas.
我开始认为如何绘制画布可能存在问题。 Anyways, here's what I have:
无论如何,这就是我所拥有的:
First, I generate the random ArrayList, only using 3 colors right now. 首先,我现在仅使用3种颜色生成随机ArrayList。 I'd make it return the list, but it's just a private method inside the thread referencing one of the thread's variables so it doesn't matter much.
我会让它返回列表,但这只是线程内引用线程变量之一的私有方法,所以没什么大不了的。
private void genMap(Resources res)
{
// Load basic tiles.
Bitmap green = BitmapFactory.decodeResource(res, R.drawable.green);
Bitmap red = BitmapFactory.decodeResource(res, R.drawable.red);
Bitmap blue = BitmapFactory.decodeResource(res, R.drawable.blue);
// All tiles must be the same size.
int tile_width = green.getWidth();
int tile_height = green.getHeight();
int num_x = mCanvasWidth / tile_width;
int num_y = mCanvasHeight / tile_height;
for (int j = 0; j < num_y; j++)
{
for (int i = 0; i < num_x; i++)
{
double r = Math.random();
Bitmap tile;
if (r <= 1/3) {tile = green;}
else if (r <= 2/3) {tile = red;}
else {tile = blue;}
// Create a new Bitmap in order to avoid referencing the old value.
mBackgroundImages.add(Bitmap.createBitmap(tile));
}
}
}
So, that's how the random values are mapped to a pattern. 因此,这就是将随机值映射到模式的方式。 The method is called in the thread's constructor, which is in turn called every time onCreate is called;
该方法在线程的构造函数中调用,该构造函数在每次调用onCreate时又调用一次。 for now, I'm just clearing the list and making a new random pattern each time:
现在,我只是清除列表并每次创建一个新的随机模式:
...
Resources res = context.getResources();
mBackgroundImages = new ArrayList<Bitmap>();
genMap(res);
...
And finally, the draw method; 最后是draw方法; it works fine loading a single Bitmap via BitmapFactory.decodeResources, but shows a black screen when doing this:
它可以通过BitmapFactory.decodeResources很好地加载单个Bitmap,但是这样做时会显示黑屏:
private void doDraw(Canvas canvas)
{
/* Draw the bg.
* Remember, Canvas objects accumulate.
* So drawn first = most in the background. */
if (canvas != null)
{
if (mBackgroundImages.size() > 0)
{
int tile_width = mBackgroundImages.get(0).getWidth();
int tile_height = mBackgroundImages.get(0).getHeight();
for (int y = 0; y < mCanvasHeight / tile_height; y++)
{
for(int x = 0; x < mCanvasWidth / tile_width; x++)
{
// Draw the Bitmap at the correct position in the list; Y * XWIDTH + X, at pos X * XWIDTH, Y * YWIDTH.
canvas.drawBitmap(mBackgroundImages.get((x + y * (mCanvasWidth/tile_width))), x * tile_width, y * tile_height, null);
}
}
}
}
}
Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。
Have you double-checked the order of precedence of the following line? 您是否仔细检查了下一行的优先顺序?
((x + y * (mCanvasWidth/tile_width)))
Interesting question though: are you drawing solid colors? 但是有趣的问题是:您在绘制纯色吗? I would also see if this is actually making a Bitmap:
我还将查看这是否实际上是在制作位图:
mBackgroundImages.add(Bitmap.createBitmap(tile));
You might actually be able to just keep references instead of creating a lot of bitmaps, but that can come later 您实际上可能只保留引用而不是创建很多位图,但这可能会在以后出现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.