Skip to content

Instantly share code, notes, and snippets.

@ColeMurray
Created January 18, 2016 01:31
Show Gist options
  • Select an option

  • Save ColeMurray/99a87786db9d8d7512de to your computer and use it in GitHub Desktop.

Select an option

Save ColeMurray/99a87786db9d8d7512de to your computer and use it in GitHub Desktop.
Inner class used to calculate if a view is visible within it's parent.
static class VisibilityChecker {
private final Rect mClipRect = new Rect();
boolean isVisible(@Nullable final View view, final int minPercentageViewed) {
if (view == null || view.getVisibility() != View.VISIBLE || view.getParent() == null) {
return false;
}
if (!view.getGlobalVisibleRect(mClipRect)) {
return false;
}
final long visibleArea = (long) mClipRect.height() * mClipRect.width();
final long totalViewArea = (long) view.getHeight() * view.getWidth();
return totalViewArea > 0 && 100 * visibleArea >= minPercentageViewed * totalViewArea;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment