5

I have some image like this

enter image description here

and I want to make the red areas clickable areas. I want to make this areas clickable meaning when user touches the screen at some red sot I want to be notified a.i I want to register a listener.

The problem is with that the image is different size for different screen, some screens have a size of 240x320 some 400x800 for the image view I use fill_parent so the image will fill the whole screen in every screen. and this clickable areas sometimes will be 50dip from the left border sometimes will be 150dip. Sometimes it is 10dip from the top sometimes it is 500dip... everything depends on the screen size

how to handle this kind of scenario ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Lukap
  • 31,523
  • 64
  • 157
  • 244

3 Answers3

2

I found a very good tutorial on this site here: http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/

The way you do it: - You have 2 images. - 1 is the background and the other is on top of it and is an invisible mask. - The mask gets an OnTouchListener. - The mask has different colored areas(your touch areas). - If you touch the image, the mask checks for the color and do an action.

The nice thing is: If you scale the image, the touch areas also scale, so you will always have your touch areas on the right position.

It is pretty straight forward. Hope i could help, even if your question is older than a year.

qweret
  • 893
  • 10
  • 16
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();
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • 1
    is there some other option ? cause this is not quite a solution for my case :( – Lukap Feb 07 '12 at 08:39
  • I haven't done this jet. But I want to follow some design patter for this , I want to register 3 listeners (let say a,b,c) and every time when someone press to the top right corner I want to make sure that listener a is called for EVERY screen no mater of the screen size of the device. I hope you get my point I do not want to make different calculations for different screen sizes . . . (I hope you get my point) – Lukap Feb 07 '12 at 09:03
  • Ok there is another method. Wait a few second for example – Dmytro Danylyk Feb 07 '12 at 09:16
0

Here is another example (last). You just put on top of your image transparent buttons and handle click.

<?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>
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • you give a hard-coded values like 100dip and that is precisely the thing I do not want to do, because 100dip on small-ldpi it will be totally different with 100dip large-hdpi. I guess that I am not on the right way to find solution for my problem, I will have to search how this things are done, if I do not find a way then I will have to do it like you explained but that means that I will need to provide around 10 different layouts with combinations of screen size(small, middle, large ) and density(ldpi,mdpi,hdpi) – Lukap Feb 07 '12 at 11:15