How can you tell when a layout has been drawn?

后端 未结 8 652
轻奢々
轻奢々 2020-11-22 08:07

I have a custom view that draws a scrollable bitmap to the screen. In order to initialize it, i need to pass in the size in pixels of the parent layout object. But during th

8条回答
  •  不知归路
    2020-11-22 08:20

    To avoid deprecated code and warnings you can use:

    view.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        view.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(this);
                    } else {
                        view.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                    }
                    yourFunctionHere();
                }
            });
    

提交回复
热议问题