Get the touch position inside the imageview in android

后端 未结 2 963
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 20:22

I have a imageview in my activity and I am able to get the position where the user touch the imageview, through onTouchListener. I placed another image where the user touch

相关标签:
2条回答
  • 2020-11-27 21:06

    You can get the top left corner of your view as follows:

    int[] viewCoords = new int[2];
    imageView.getLocationOnScreen(viewCoords);
    

    From this and the touch coordinates you can calculate the point inside the ImageView:

    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    
    int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
    int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate
    
    0 讨论(0)
  • 2020-11-27 21:22

    Add these lines to ImageView

    android:scaleType="fitXY" 
    

    and

    android:adjustViewBounds="true"
    

    to your image view it is because image are not having their original dimensions in IV

    0 讨论(0)
提交回复
热议问题