Basically I have the following bitmap: Bitmap cloud; cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
What I want to do is draw the same bitmap(in this case cloud) multiple times in different(random) locations on canvas. I want to do this in the OnDraw method of my view.
So the final product should look like this:
However
This is the code for drawing the bitmap multiple times on the same canvas:
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// canvas.drawColor(Color.BLUE);
//drawBackground(canvas);
//objectMovement(canvas);
for (int i = 0; i < 500; i += 50) {
canvas.drawBitmap(cloud, 50 + rand.nextInt(350),
10 + rand.nextInt(350), null);
}
invalidate();
}
As you can see, I'm using random to generate the position of the bitmap, and every time the bitmap is drawn in a different place on the canvas.
The only problem is, every time invalidate() is being called, the drawing in the for loop happens again. Because of this, it looks like the drawings(cloud) is moving very fast all over the place.
After searching for a while, I found this question: Draw multiple times from a single bitmap
It's pretty much the same question as my question, but unfortunately, there is no right answer there.
So how do I achieve this?
Thanks!
The first problem is that you are reading in the bitmap 10 times per frame which is completely unnecessary. You should read the bitmap in once when you instantiate your class. I've also added a Paint. I don't know if it's completely necessary but just incase you decide to do more in your draw method it's there.