Search code examples
androidimageviewandroid-imageviewandroid-image

How to get Coordinates on touch Event on bitmap not of Screen


Greets ,

How can I get the Coordinates of the Bitmap not of the screen. Screen Coordinates got by event.getX(),event.getY() methods but not getting coordinates of the bitmap. Please help anyone.


Solution

  • You can't get the coordinates of the bitmap directly. You need to calculate to yourself. Use the position of the ImageView, and with that you can handle it.

    Of course, when you are after Activity onCreate you can acces to any inflated (active) views parameter. Exemple. ImageView a; a.getPaddingBottom(); Like all coordinats (left right etc...) After this you need the Height and Width of the ImageView. Nah, when you know the views position, hegiht and width you can calculate.

    Example:

    final ImageView iv_YourImage = (ImageView) findViewById(R.id.iv_imageview);
    public boolean onTouch(View v, MotionEvent event){
    int topParam =  iv_YourImage.getPaddingTop();
     int rightParam =  iv_YourImage.getPaddingRight();
    int maxTopParam = topParam+iv_YourImage.getMaxHeight();
    int maxRightParam = rightParam + iv_YourImage.getMaxWidth();
     if(event.getX>topParam&&event.getX<maxTopParam){
        //the x coordinate is in your image... do the same to Y
     }
    return true;
    }