【问题标题】:Get the coordinates of visible part of screen when zoomed in放大时获取屏幕可见部分的坐标
【发布时间】:2025-12-15 20:55:01
【问题描述】:

我正在使用custom lib,它实现了可缩放、可收缩和可滚动的布局,但是当我放大布局时,我无法获得可见部分的坐标。有没有办法做到这一点? Android 文档对视口没有任何解释。

这张图片有一个例子。左边的矩形是我的布局。当我放大它时,用户只会看到较小的那个,但它会填满整个屏幕。如何获取小矩形(红色)的坐标?

【问题讨论】:

  • 您的意思是关于“可见部分”的 Window 而不是 Viewport?因为我认为 Viewport 就是 ImageView 本身。
  • @hata 我的意思是布局的可见部分。当它被放大时,用户并没有看到它的全部。 Android 文档将可见内容的“窗口”称为视口:developer.android.com/training/gestures/scroll?hl=pt-br 但它不是图像视图,而是可缩放的布局。
  • 好的,我明白了。确切地说,据我所知,设备上的可见部分称为Viewport,而源图像上的可见部分称为Window。但现在我明白你的意思了。

标签: java android android-layout android-activity android-view


【解决方案1】:

所以我不是the library 的真正用户,我不知道实际答案。但我自己做的事情与我的自定义 ImageView 中的库类似。所以我可以提出一些建议。请稍后自行尝试检查。

图书馆有那些Pan APIs。我认为通过使用getPan()/getPanX()/getPanY()getScaledPan()/getScaledPanX()/getScaledPanY() 可以获得左上点的坐标。 (也许,后面的 scaled 是你想要的?)

一旦你得到左上点的坐标,你就可以通过矩形的宽度和高度来计算其他点。

要知道宽度和高度,您可以从 ImageView 的宽度和高度以及当前比例因子进行反向计算。

要了解 ImageView 的宽度和高度,请使用 getWidth()getHeight()

要知道库的当前比例因子,Zoom APIs 中的 getZoom()getRealZoom() 应该是可用的。

然后,将 ImageView 的宽度或高度除以当前比例因子,得到矩形的宽度或高度。

最后,根据左上点的坐标,左下点加高,右上点加宽,右下点加宽加高。


示例代码

布局/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.otaliastudios.zoom.ZoomImageView
        android:id="@+id/zoomImageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/darker_gray"
        app:alignment="top|left"
        app:layout_constraintBottom_toTopOf="@id/linearLayout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:maxZoom="2"
        app:maxZoomType="zoom"
        app:minZoom="0.5"
        app:minZoomType="zoom"
        app:overPinchable="false"
        app:overScrollHorizontal="false"
        app:overScrollVertical="false"
        app:threeFingersScrollEnabled="false"
        app:transformation="centerCrop"
        app:transformationGravity="top|left"
        app:twoFingersScrollEnabled="false" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/zoomImageView">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="@string/app_name" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

主活动:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.otaliastudios.zoom.ZoomImageView;

public class MainActivity extends AppCompatActivity {
    private ZoomImageView zoomImageView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        zoomImageView = findViewById(R.id.zoomImageView);
        zoomImageView.setImageDrawable(getResources().getDrawable(R.drawable.img2880x2880, null));
        textView = findViewById(R.id.textView);
    }

    public void click(View view) {
        textView.setText(String.format(
                "pan (on the World (src image) coordinates):\n\t%s\n" +
                        "scaledPan (on the Display coordinates):\n\t%s\n" +
                        "zoom (relative ratio to the initial scale):\n\t%f\n" +
                        "realZoom (real scale factor for the image):\n\t%f\n" +
                        "the View's layout width: %d\n" +
                        "the View's layout height: %d\n\n" +
                        "the Window rect: (%f, %f) - (%f, %f)",
                zoomImageView.getPan().toString(),
                zoomImageView.getScaledPan().toString(),
                zoomImageView.getZoom(),
                zoomImageView.getRealZoom(),
                zoomImageView.getWidth(),
                zoomImageView.getHeight(),
                -zoomImageView.getPanX(),
                -zoomImageView.getPanY(),
                -zoomImageView.getPanX() + ((float) zoomImageView.getWidth()) / zoomImageView.getRealZoom(),
                -zoomImageView.getPanY() + ((float) zoomImageView.getHeight()) / zoomImageView.getRealZoom()
        ));
    }
}

还有drawable-nodpi/img2880x2880.png。所以我的测试设备的显示宽度是 1440 像素,我准备了一个 2880 像素 x 2880 像素的正方形图像,带有 1440 像素的方格。

您可以平移(滚动)图像并捏拉放大/缩小它。当您按下按钮时,它会指示当前状态。最后,Window rect 应该是你想要的。

请注意,因为

-zoomImageView.getPanX() + ((float) zoomImageView.getWidth()) / zoomImageView.getRealZoom()

-zoomImageView.getPanY() + ((float) zoomImageView.getHeight()) / zoomImageView.getRealZoom()

是浮点值的计算,结果值可能部分包含计算错误。

【讨论】:

  • 我试过这样:rectangle.x = -1 * layout.engine.panX) + (layout.width/5f)/2f(值是负数,所以我只是乘以-1)。我除以 5f 因为那是我的比例,而 2f 因为我想要中心位置,但我的 imageview 没有居中。我使用了像 `rectangle.x = -1 * layout.engine.panX) + (layout.width/5f * 0.213f) 这样的随机值,只是尝试了随机值,直到我的 imageview 居中。它在我的手机上运行,​​但在 AVD 上运行并在另一个屏幕上测试,它不起作用。
  • 这很奇怪,因为我基于另一个 imageview 位置尝试随机值使 TextView 居中,并且它在不同的屏幕上工作,例如:actionText.x = star.position.x - dpToPx(26).toFloat()。但它不适用于矩形(我用 XML 制作的一个简单的图像视图)。 “星”是缩放前已经存在于布局中的图像视图,因此当用户触摸它时,布局会随着移动到星而缩放,以使星居中,因此我将 TextView 位置基于此星它奏效了。
  • @LucasRodrigues 我使用ZoomImageView 类制作了一个示例代码。
  • 谢谢!我是这样写的:rectangle.y = (-layout.engine.panY + view.height/layout.zoom) - rectangle.height,它在不同的屏幕上正确定位:)。