0

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.

Hunt
  • 8,215
  • 28
  • 116
  • 256

2 Answers2

1

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.

DavGin
  • 8,205
  • 2
  • 19
  • 26
  • Ohhh that is a quite cumbersome process do u have any alternate solution to it ? – Hunt Oct 13 '10 at 13:10
  • If i will go with your approach then how would i get/calculate the bounding box of an image inside canvas ? – Hunt Oct 13 '10 at 17:22
  • 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. – DavGin Oct 14 '10 at 07:39
  • 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 Oct 14 '10 at 11:11
-1

I believe you're asking for something like that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/clickable_image"
        android:src="@drawable/ic_image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="true"
        />
</LinearLayout>

Now you have your background "setting the whole view as a wallpaper" in your own words, and then you have your image, which is clickable. In your code you implement onClickListener and attach it to your ImageView and it will do whatever you want it to do.

Nora Madjarova
  • 169
  • 1
  • 4