Get coordinate of the image by clicking on it

1

I'm in a Xamarin / Android project. I need that when the user touches a ImageView I can get the coordinates (of the image) of that touch.

How to do this?

    
asked by anonymous 19.07.2018 / 22:09

2 answers

1

Recently I went through a similar case, and if you're really wanting the coordinates of the image , the way you did it will not work, because the argument e strong> ImageView that has been touched, not the Image .

for a better example:

Let's say that you have a device with a resolution of 1920x1080, and ImageView with width with value set to match_parent , and src with a 1280x720 image. If you touch the upper right corner of the image, GetX() will return 1080 (screen location) instead of 720 (location of image that was actually touched)

Solution: Continue using this method, but to know the actual touch position in Image , calculate the difference in image size before it is placed in ImageView , and after it is placed.

byte[] decodedString = Base64.Decode(ImagemOriginal, Base64Flags.Default);
Bitmap decodedByte = BitmapFactory.DecodeByteArray(decodedString, 0, decodedString.Length);       
int originalWidth = decodedByte.Width;

ImageView imag = _View.FindViewById<ImageView>(Resource.Id.img);
imag.SetImageBitmap(decodedByte);
Double fatorDiferenca = (double)imag.MeasuredWidth /(double)originalWidth;

Then whenever you want to get the position in the image, multiply the value obtained by GetX() by fatorDiferenca :

var posicaoReal = e.Event.GetX() * fatorDiferenca;

(this holds for both X and Y)

Edit: In order to draw something in the place you clicked, you should only use GetX () and GetY () normally, since you will be effectively drawing in ImageView and not in the image you served as a resource. For you to draw something inside your% w / o% I'm afraid you'll have to make a class inheriting from ImageView so you can do this:

   public class CustomImageView : ImageView
   {
        private int _CurrentX;
        private int _CurrentY;

        public override bool OnTouchEvent(MotionEvent e)
        {
            if (e.Action == MotionEventActions.Up)
            {
                _CurrentX = (int)e.GetX();
                _CurrentY = (int)e.GetY();

                Invalidate();
            }

            return true;
        }

        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            using (var paint = new Paint(PaintFlags.AntiAlias))
            {
                paint.Color = Color.Red;

                canvas.DrawCircle(_CurrentX, _CurrentY, 20, paint);    
            }
        }
    }

In order for you to know which place of the image you are clicking in pixels, you have to do it the way I said, get GetX () and multiply by the difference factor if you want can store in a variable for future uses.

    
26.07.2018 / 23:30
0

I solved my problem by doing the following:

First I created a relationship between my ImageView and my Activity :

private ImageView _myImage; _myImage = FindViewById<ImageView>(Resource.Id.myImage);

So I created a touch event from my ImageView :

_myImage.Touch += _myImage_Touch;

And in his method, it takes the coordinates of the touch through the parameter e by accessing e.Event.GetX(); and e.Event.GetY(); I also used a if that verifies if the value of e.Event.Action is equal to MotionEventActions.Down . This way, the event only runs once, when the finger touches the screen.

The method looks like this:

private void _imgRoute_Touch(object sender, View.TouchEventArgs e)
{
    if(e.Event.Action == MotionEventActions.Down)
    {   
      float x = e.Event.GetX();
      float y = e.Event.GetY();

      //resto do meu código
    }
}
    
25.07.2018 / 21:35