Android gets the height of the view before drawing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.