gpt4 book ai didi

Android 在 ImageView 中获取位图的位置

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:01 25 4
gpt4 key购买 nike

我正在使用 CropView它扩展了一个 Android ImageView。它允许缩放和移动 View 中的图像。似乎没有公开提及图像位置或比例。有没有办法获取图像相对于我可以尝试的常规 ImageView 的位置。

我还尝试在 CropView 上设置一个触摸监听器,它只打印触摸的 x 和 y 位置,但我不知道如何使用这些来移动图像的更新位置。

mCropView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "///" + event.getX() + " __ " + event.getY());
return false;
}
});

enter image description here

最佳答案

https://github.com/lyft/scissors/blob/master/scissors/src/main/java/com/lyft/android/scissors/CropView.java

CropView 本身就是视口(viewport)。 Cropview 有两个方法 getViewportWidth并获取视口(viewport)高度。您将需要它们。

被缩放、移动、裁剪等的图像是一个 Drawable。 Drawable 有一个 getBounds 方法,它将返回一个 Rect。您可以从此 Rect 获取顶部、左侧的宽度和高度,但您需要先获取 Drawable。

CropView 将此 Drawable 称为“位图”。您可以调用 CropView 的 getImageBitmap() 方法获取它。

获得图像后,调用 getBounds,它会为您提供图像的 Rect。

要获得您想要的 x 和 y,您必须对 Rect 的顶部、左侧、宽度和高度以及您将从 CropView 的 getViewportHeight 和 getViewportWidth 方法获得的视口(viewport)的高度和宽度做一些数学运算。

简单地:

vpwidth=cropview.getViewPortWidth();
vpheight=cropview.getViewPortHeight();
imagetop=cropview.getImageBitmap().getBounds().top;
imageleft=cropview.getImageBitmap().getBounds().left;
imageheight=cropview.getImageBitmap().getBounds().height;
imagewidth=cropview.getImageBitmap().getBounds().width;

>这里是数学

关于Android 在 ImageView 中获取位图的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38127367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com