【发布时间】: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