2
I’m making an application that has an image that occupies the whole ImageView
, and when the user clicks at some point on that image, the X and Y coordinates corresponding to the image are obtained and a circle is drawn with center at that point.
Whereas my image has the following size (and consequently ImageView
, that by default on Android, has its size determined by the image dimensions):
imgView.width = (int) (screen_width * 0.8);
imgView.height = (int) (screen_heigh * 0.8);
Using the method of ImageView
imgView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
real_x = (float) (event.getX());
real_y = (float) (event.getY());
//desenhe um círculo com centro em Point(real_x,real_y) na imagem contida no imgView
}
}
The problem is that event.getX()
and event.getY()
do not return the real coordinates, corresponding to the point clicked on the image. Thus a conversion is required, **but how to do this conversion in a way that is valid for any screen size?? **
Thank you for your attention! Since the size of the imageView will be the same as the bitmap inserted in it through
android:src
, What is the need to convert between bitmap size and image view size ? ratioX and ratioY will not be 1?– João Luiz
Yes and in this case the coordinates you are looking for are the ones returned by
event.getX()
andevent.getY();
. So I don’t understand what you mean by "Event.getx() and Event.gety() do not return the actual coordinates"– ramaral
How is it "that the size of the imageView will be the same as the bitmap inserted in it" if the size of Imageview is adjusted to 80% of the screen size?
– ramaral
Aren’t you confusing real coordinates with absolute coordinates? Real coordinates are relative to the Bitmap, absolute coordinates are relative to the screen and relative coordinates are relative to the Imageview
– ramaral
So I need to resize my bitmap to
bitamp.width = (int) (screen_width * 0.8);
bitmap.height = (int) (screen_heigh * 0.8);
– João Luiz
how the imageView adapts to the size of the
src
, if for examplesrc
was a 320x240 image, if I doLayoutParams params = (LayoutParams) imageView.getLayoutParams();
params.widht
andparams.height
would have to be 320 and 240 respectively, but return -1 and -1. For it to work normally, these values have to have the actual size of the image, since it works normally when I doparams.width=320; params.height=240; imageView.setLayoutParams(params);
– João Luiz
If you have both layout params as
wrap_content
to Imageview the real size of the image but nay can get it ononCreate()
see this question, however in the methodonTouch()
should already be available. I still haven’t figured out what kind of coordinates you want to get, whether they are real, absolute or relative.– ramaral
Thank you. Finally I got it, the problem was that imageview was starting with different image dimensions. What I did was, first of all, load the image in an array (Mat() - with Opencv) with custom sizes (80% of the screen) and after that, pass the dimensions to the imageview.
– João Luiz