获取ImageView顶部和底部的Y坐标

时间:2013-09-12 13:42:00

标签: android coordinates translation android-sensors

我有一个包含在相对布局中的图像视图。我试图获取图像视图顶部的屏幕y坐标和图像视图底部的屏幕y坐标。我试过这个:

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

浮动yMin似乎几乎是正确的。我正在这个图像视图(IM1)上下翻译另一个图像视图(IM2)。所以我试图设置一个限制(IM2)可以向上和向下翻译。所以我的想法是得到(IM1)的y顶部和底部我可以将它们设置为max和min。

任何人都知道怎么做?

ps我使用android accelometer移动(IM2)

2 个答案:

答案 0 :(得分:17)

getTop()getBottom()查看其父级内的坐标。要获得它在屏幕上的位置坐标,您可以使用getLocationOnScreen

像这样使用:

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

答案 1 :(得分:0)

使用View.getLocationOnScreen()和/或getLocationInWindow()

相关问题