Moayad Hani Abu Rmilah
Moayad Hani Abu Rmilah

Reputation: 830

How to know which bitmap on canvas is touched?

I have an array of bitmaps myBitmapArray that are drawn on canvas randomly. I have a random variable Random rnd = new Random(); and each time the onDraw method is called (with a rate of 10 frames per second), I perform this code: canvas.drawBitmap(myBitmapArray.get(rnd.nextInt(6)), x + xSpeed, y + ySpeed, null); where xSpeed and ySpeed are used to change the location of every bitmap on the canvas. Bitmaps are drawn as expected.

I also use the onTouchEvent to play some sounds according to the bitmap touched (e.g. if the bitmap touched is a character, then that character is pronounced). My problem is in performing this task.

I know that I need to check that the touch event x and y are inside the bitmap image but since I draw the bitmaps randomly, I don't know how to get their coordinates to make it possible to check.

Any idea on how I may solve this issue?

FYI: I have checked this question but without much help: here!

Upvotes: 0

Views: 383

Answers (1)

Tabrej Khan
Tabrej Khan

Reputation: 894

You have to keep track of the X and Y position of your bitmap. Otherwise its impossible to know where your bitmap is drawn. Once you have your X and Y position of bitmap you can put the logic you have found and provided the link in your question.

Upvotes: 1

Related Questions