5

我有一些像这样的图像

在此处输入图像描述

我想让红色区域成为可点击区域。当用户在某个红色点触摸屏幕时,我想让这个区域可点击,我想收到通知,我想注册一个监听器。

问题在于不同屏幕的图像尺寸不同,有些屏幕的尺寸为 240x320,有些屏幕的尺寸为 400x800,我使用 fill_parent,因此图像将在每个屏幕中填满整个屏幕。这个可点击的区域有时会距左边框 50dip 有时会是 150dip。有时距离顶部 10dip 有时是 500dip ......一切都取决于屏幕尺寸

如何处理这种情况?

4

3 回答 3

2

The best way is to cut your image for separate pieces, and put them inside RelativeLayout.

Then set click listener for those images where you need.

Another way you could handle this is determine screen size and calculate touch areas:

final Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

final int width = display.getWidth();
final int height = display.getHeight();
于 2012-02-06T16:02:55.360 回答
2

我在这个网站上找到了一个很好的教程:http: //blahti.wordpress.com/2012/06/26/images-with-clickable-areas/

你这样做的方式: - 你有2个图像。- 1 是背景,另一个在它上面,是一个不可见的蒙版。- 掩码获得一个 OnTouchListener。- 面具有不同的颜色区域(您的触摸区域)。- 如果您触摸图像,遮罩会检查颜色并执行操作。

好处是:如果你缩放图像,触摸区域也会缩放,所以你的触摸区域总是在正确的位置。

这很简单。希望我能提供帮助,即使您的问题已经超过一年。

于 2014-01-29T11:10:54.297 回答
0

这是另一个例子(最后一个)。您只需将图像透明按钮放在顶部并处理点击。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@android:drawable/edit_text" />

    <Button
        android:id="@+id/bottomLeft"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@android:color/transparent" />

    <Button
        android:id="@+id/center"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent" />

    <Button
        android:id="@+id/bottomRight"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_centerInParent="true"
        android:background="@android:color/transparent" />

</RelativeLayout>
于 2012-02-07T09:23:16.650 回答