views:

37

answers:

1

I have create a canvas in android and inside that i have multiple bitmap images.not i want to make these images click able.

I have tried following things so far..

I tried to add bitmap in image view as imageview has a setOnClickListner but i think ImageView can't be added into Canvas , so i dropped this idea. because even Bitmap itself has no click events.

A: 

If you want to use Canvas, keep in mind that it is a low level drawing mechanism.

Therefore, you need to implement the click logic yourself.

  • Catch the coordinates of any incoming TouchEvent.
  • If the TouchEvent is a "touch down" (finger pressed) or "touch up" (finger released), depending on your choice, consider it to be a click.
  • Compare the click event coordinates to every attached image's bounding box to find which image was touched. Take the z-index into account in case of overlap.
  • Trigger an onClickListener.

You also have to keep the coordinates of all images and the corresponding onClickListeners somewhere in memory.

Other solution:

Use a Layout, possibly a RelativeLayout, in which you add the ImageViews as children.

DavLink
Ohhh that is a quite cumbersome process do u have any alternate solution to it ?
Hunt
If i will go with your approach then how would i get/calculate the bounding box of an image inside canvas ?
Hunt
Yes, this is a cumbersome process. By following this procedure, you are coding a custom view from scratch.It's up to you to measure the bounding box of each added image and store it in an ArrayList, for example.There's another solution, I edited the answer.
DavLink
okay , if i will go with the RelativeLayout and stores ImageView inside it and later on can i set the whole view as a wallpaper ? and even after can i still be able to click on images ?
Hunt