所以我不是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()
是浮点值的计算,结果值可能部分包含计算错误。