Last active
December 6, 2015 12:13
-
-
Save Deliganli/40f6cd88391f9d681b14 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Measures given view and runs the action provided after that | |
* @param view to be measured | |
* @param action to be performed | |
*/ | |
public static void measure(View view, ViewMeasurer action) { | |
ViewTreeObserver vto = view.getViewTreeObserver(); | |
if (vto.isAlive()) { | |
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { | |
@Override | |
public boolean onPreDraw() { | |
view.getViewTreeObserver().removeOnPreDrawListener(this); | |
int width = view.getMeasuredWidth(); | |
int height = view.getMeasuredHeight(); | |
if (action == null) return false; | |
action.onViewMeasured(width, height); | |
return true; | |
} | |
}); | |
} | |
} | |
public interface ViewMeasurer { | |
void onViewMeasured(int width, int height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment