I am trying to create an array of images that can be drawn on a canvas. This is what I have:
List<Integer> imageHolder = new ArrayList<Integer>();
imageHolder.add((int)R.drawable.bus_1);
imageHolder.add((int)R.drawable.bus_2);
imageHolder.add((int)R.drawable.bus_3);
Then I try and access the images like this from my onDraw method:
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
for (int i = 0; i < imageHolder.size(); i++){
canvas.drawBitmap(imageHolder.get(i), 0, 0, null);
}
}
But I get an error saying that my arguments are not applicable for my canvas.drawbitmap. Does anyone know how to do this?I have been looking all over for an explanation on how to do this and I can’t find it anywhere.
EDIT: This is how I got it working to print out the 3 images at different points on the screen:
for (int i = 0; i < imageHolder.size(); i++) {
bMap = BitmapFactory.decodeResource(res, imageHolder.get(0));
canvas.drawBitmap(bMap, 100, 100, null);
bMap2 = BitmapFactory.decodeResource(res, imageHolder.get(1));
canvas.drawBitmap(bMap2, 500, 100, null);
bMap3 = BitmapFactory.decodeResource(res, imageHolder.get(2));
canvas.drawBitmap(bMap3, 900, 100, null);
}
What you do is you initially add a bunch of integers to an ArrayList and then you try to loop through this ArrayList and draw a Bitmap, using a resource identified by this integer. The problem is that the first argument to the
drawBitmapmethod in theCanvasclass must be either an integer array of colors or a Bitmap resource. Not just an integer, pointing to one. For more information, check the documentation.In order to get a specific resource by ID as Bitmap you need to do this:
Or in your case your loop needs to look like this: