Search code examples
androidcoordinatestranslationandroid-sensors

Get Y coordinate of top and bottom of ImageView


I have an image view which is contained within a relative layout. I am trying to get the screen y coordinates of the top of the image view and the screen y coordinates of the bottom of the image view. I have tried this:

 float yMin = (float)levelH.getTop();
 float yMax = (float)levelH.getBottom();

float yMin seems almost correct. I am translating another image view (IM2) up and down this image view(IM1). So I am trying to set a limit on how far (IM2) can translate up and down. So my thinking was to get the y top and bottom of (IM1) I can set those as max and min.

Anyone know how to do this?

ps Im using android accelometer to move (IM2)


Solution

  • getTop() ansd getBottom() look at coordinates within it's parent. To get coordinates of it's position on the screen you can use getLocationOnScreen

    Use it like this:

    int[] coords = {0,0};
    view.getLocationOnScreen(coords);
    int absoluteTop = coords[1];
    int absoluteBottom = coords[1] + view.getHeight();