In the Android development process, we may need to get the height or width before the view is drawn, one of the possible scenarios is that when we initialize a view that is visible = gone, we need it to display when we trigger an event and want to have some animation effect. At this point we are going to get the width or height before the view is drawn. The principle is simple, and we know that the Onmeasure method will be executed before the view's drawing process occurs. Then we can take advantage of the reflection or the value we need. The following gives the code to get the height, the same width.
private int gettargetheight (View v) {
try { Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class); m.setAccessible(true); m.invoke(v, MeasureSpec.makeMeasureSpec( ((View) v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); } catch (Exception e) { } return v.getMeasuredHeight();}
When we get this height, we can do the animation we want to do or something else.
Android gets the height of the view before drawing