[英]Android get Position of Bitmap in ImageView
我正在使用CropView ,它扩展了Android ImageView
。 它允许视图中的图像缩放和移动。 似乎没有任何公开引用图像位置或比例。 有没有办法获得图像相对于我可以尝试的常规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;
}
});
你可以尝试下面的方法,
final float[] getPointOfTouchedCordinate(ImageView view, MotionEvent e) {
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix m = new Matrix();
view.getImageMatrix().invert(m);
m.postTranslate(view.getScrollX(), view.getScrollY());
m.mapPoints(coords);
return coords;
}
你可以获得价值
mCropView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float[] points = getPointOfTouchedCordinate(v, event);
Log.d(TAG, "///" + points[0] + " __ " + points(1));
return false;
}
});
我建议依靠纯xml并添加到Imagevie:
android:scaleType="centerCrop"
这将导致绘制所需的输出。
CropView本身就是视口。 Cropview有两个方法getViewportWidth和getViewportHeight。 你需要它们。
缩放,移动,裁剪等图像是Drawable。 Drawable有一个getBounds方法,它将返回一个Rect。 您可以从此Rect获得顶部,左侧宽度和高度,但您需要先获得Drawable。
CropView调用此Drawable“位图”。 您可以调用CropView的getImageBitmap()方法。
一旦你拥有它,调用getBounds,它给你图像的Rect。
要获得你想要的x和y,你必须使用Rect的顶部,左边,宽度和高度以及视口的高度和宽度进行一些数学运算,你可以从CropView的getViewportHeight和getViewportWidth方法获得。
只是:
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;
>
数学在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.