【问题标题】:How do I get the actual image coordinates using touch in ImageView?如何在 ImageView 中使用触摸获取实际图像坐标?
【发布时间】:2020-01-28 23:06:26
【问题描述】:

我想使用触摸功能在 ImageView 中获取图像的实际 X 和 Y 坐标。

到目前为止,我只获得了 800x600 的 ImageView 中的坐标。我的图像大多具有比这更高的分辨率。我该怎么做?

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" 
/>

【问题讨论】:

    标签: java android image events touch


    【解决方案1】:

    您需要在图像视图宽度和实际图像宽度之间进行计算。和高度一样。你会发现这个答案很有帮助 Check this

    【讨论】:

      【解决方案2】:

      假设图像视图就像最大图像内的相机位置 xv,yv。 XinImage = xtouch + xv YinImage = ytouch + yv

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多