Created
January 18, 2016 01:31
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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