如下图所示.我有一个relative layout
.我有我ImageView
的相同,我需要在视图开始和这个ImageView之间获得像素差距.我的活动如下.请指教.我试过各种各样的方法,topY()
但找不到解决办法.
Douglas Nass.. 7
要计算一个视图底部与另一个视图顶部之间的距离:
View oneView = ...; View otherView = ...; RectF oneRect = calculeRectOnScreen(oneView); RectF otherRect = calculeRectOnScreen(otherView); float distance = Math.abs(oneRect.bottom - otherRect.top);
util方法:
public static RectF calculeRectOnScreen(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight()); }
在某些情况下,您需要替换view.getLocationOnScreen
to view.getLocationInWindow
.
要计算一个视图底部与另一个视图顶部之间的距离:
View oneView = ...; View otherView = ...; RectF oneRect = calculeRectOnScreen(oneView); RectF otherRect = calculeRectOnScreen(otherView); float distance = Math.abs(oneRect.bottom - otherRect.top);
util方法:
public static RectF calculeRectOnScreen(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight()); }
在某些情况下,您需要替换view.getLocationOnScreen
to view.getLocationInWindow
.