[英]Click on ImageView on Android screen
my problem is to click on imageview and to know where, on the image (resized by imageview), i click on. 我的问题是单击imageview并知道我在图像上的位置(按imageview调整大小)。
My current algorithm is: 我当前的算法是:
On the onTouch method of ImageView, get the X and Y positions of the MotionEvent and do the math 在ImageView的onTouch方法上,获取MotionEvent的X和Y位置并进行数学计算
int realX = (X / this.getWidth()) * bitmapWidth;
int realY = (Y / this.getHeight()) * bitmapHeight;
Where bitpmapWidth and bitmapHeight are from the original bitmap. 其中bitpmapWidth和bitmapHeight来自原始位图。
Can anyone help me? 谁能帮我?
Almost right, math should be just a little different. 几乎正确,数学应该有所不同。 Also keep in mind that ImageView's getHeight() and getWidth() would be 0 during onCreate, I have used info from this answer getWidth() and getHeight() of View returns 0 to use get width and height once it's available
还请记住,在onCreate期间ImageView的getHeight()和getWidth()将为0,我已使用此答案中的信息getView的getWidth()和getHeight()返回0,以在可用时使用获取宽度和高度
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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.