0

我在 android 中创建了一个画布,里面有多个位图图像。我不想让这些图像可以点击。

到目前为止,我已经尝试过以下事情..

我试图在图像视图中添加位图,因为 imageview 有一个 setOnClickListner 但我认为 ImageView 不能添加到 Canvas 中,所以我放弃了这个想法。因为即使是 Bitmap 本身也没有点击事件。

4

2 回答 2

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.

于 2010-10-13T12:11:49.743 回答
-1

我相信你要求这样的东西:

<?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>

现在您有了自己的背景“将整个视图设置为墙纸”,然后您就有了可点击的图像。在您的代码中,您实现 onClickListener 并将其附加到您的 ImageView 中,它将执行您想要执行的任何操作。

于 2012-08-20T20:44:06.660 回答