开发者

Detecting when a View has been invalidated

开发者 https://www.devze.com 2023-03-22 23:23 出处:网络
In a custom View, I need to perform some additional work inside onDraw() if and only if the View was invalidated by the application; that is, my own code called invalidate() in the UI thread or postIn

In a custom View, I need to perform some additional work inside onDraw() if and only if the View was invalidated by the application; that is, my own code called invalidate() in the UI thread or postInvalidate() in a non-UI thread. If on the other hand onDraw() is being called because the system invalidated the View, I don't wish that additional work to be performed.

What's the best way to achieve this? My immediate thought is to simply override invalidate() and postInvalidate() and set a flag in both of tho开发者_C百科se, but it would be nicer if there was a single UI-thread method I could override.

Any thoughts please?

Thanks, Trev


postInvalidate() ends up calling invalidate() so you don't need to override both. But if you override invalidate(), the system will call the overridden version.


There is a way to do that without extending view class.

view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
        @Override
        public void onDraw() {
            //View was invalidated
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消