clickable image inside canvas
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 i开发者_如何学运维n 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.
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.
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.
精彩评论