0

I want to get the actual X and Y coordinates of an image in ImageView using the touch function.

So far I am only getting the coordinates within the ImageView which is 800x600. My image would mostly have a higher resolution than that. How do I do this?

MainActivity.java

private void addTouchListener(){
        ImageView image = findViewById(R.id.result);

        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                float x = motionEvent.getX();
                float y = motionEvent.getY();

                String message = String.format("Coordinates: (%.1f, %.1f)", x, y);

                Log.d(MainActivity.DEBUGTAG, message);

                return false;
            }
        });

    }

activity_main.xml

 <ImageView
        android:id="@+id/result"
        android:layout_width="330dp"
        android:layout_height="250dp"
        android:layout_marginTop="150dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/leaf" 
/>
gongetout
  • 31
  • 5

2 Answers2

0

you'll need to do a calculation between the image view width and the actual image width. and same with the height. You'll find this answer helpful Check this

Viroj Fernando
  • 461
  • 4
  • 8
0

Asume image view is like a camera position xv,yv within bigest image. XinImage = xtouch + xv YinImage = ytouch + yv

Chronoslog
  • 107
  • 8