0

Well my problem is this: I have an image and I want when I click on a specific point of the map to display an other image. I am using this code

 private void setImageClickListener() {
    ImageView map_image=(ImageView)findViewById(R.id.map_icon);
    map_image.setOnTouchListener(new ImageView.OnTouchListener() {
    //OnTouchListener listener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if(!(event.getAction() == MotionEvent.ACTION_DOWN))
                return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
            final float x = event.getX();
            final float y = event.getY();
            System.out.println("Coordinates of button pressed are: X is %d"+x+" and Y is %d"+ y);
            if(x>260 && x<390 && y>270 && y< 290)
               DoFirst();
            //... and so on...
            //In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not.
           return true;
        }

});

and my xml file is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<ImageView
    android:id="@+id/map_icon"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:scaleType="fitXY"
    android:src="@drawable/home_map" />

The problem is that when I run it on emulator the coordinates are different from running it on different phones. How can i do it run on multiple screens? I thought that bu using the event.getX the coordinates refer to image coordinates and not on screen coordinates. So what i want is that the range to be independent from the screen.

Any help?

ghostrider
  • 5,131
  • 14
  • 72
  • 120

1 Answers1

0

I would suggest that you use a custom view in order to have a full control of the ImageView. Then in onSizeChange() you would know the exact size whatever the mobile...

Here is an example: Make certain area of bitmap transparent on touch

Community
  • 1
  • 1
Lumis
  • 21,517
  • 8
  • 63
  • 67