我的问题是单击imageview,并知道在图像上(通过imageview调整大小)在哪里单击。
我目前的算法是:
关于onTouch方法ImageView,求出MotionEvent的X和Y位置并进行数学计算
int realX = (X / this.getWidth()) * bitmapWidth;
int realY = (Y / this.getHeight()) * bitmapHeight;
其中bitpmapWidth和bitmapHeight来自原始位图。
有谁可以帮我?
发布于 2017-03-06 18:27:55
几乎正确,数学应该是有点不同的。还请记住,ImageView的getHeight()和getWidth()在onCreate期间为0,我使用了这个答案中的信息-- getWidth() and getHeight() of View returns 0 --一旦可用,就使用get宽度和高度
iv = (ImageView) findViewById(R.id.img);
iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
iv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
xScaleFactor = iv.getWidth() / originalBitmapWidth;
yScaleFactor = iv.getHeight() / originalBitmapHeight;
Log.v(TAG, "xScaleFactor:" + xScaleFactor + " yScaleFactor:" + yScaleFactor);
}
});
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int realX = touchX / xScaleFactor;
int realY = touchY / yScaleFactor;
Log.v(TAG, "realImageX:" + realX + " realImageY:" + realY);
return false;
}
});
https://stackoverflow.com/questions/42634748
复制