简体   繁体   English

我如何知道是否已绘制视图

[英]How do I know if a view has been drawn

I want to know if its possible to know if a view has been drawn on the screen. 我想知道是否有可能在屏幕上绘制视图。

I am not interested in setting a listener and waiting for it, as I already know how to do that. 我对设置侦听器和等待它不感兴趣,因为我已经知道该怎么做。 I am looking for a call, to get a true/false on if the view is currently on the screen. 我正在寻找一个电话,如果视图当前在屏幕上,则获取是/否。

I dont believe I can simply check the visibility as that is a preset variable, regardless of its state, though I could be wrong. 我不相信我可以简单地检查可见性,因为这是一个预设变量,无论其状态如何,尽管我可能是错的。

If the view is not draw, I will set a ViewTreeObserver and wait for it, but that is only neccessary if the view has not yet been drawn. 如果未绘制视图,则将设置一个ViewTreeObserver并等待它,但这仅在尚未绘制视图时才需要。

since getVisibility() returns a predefined property and u don't want to set a listener; 因为getVisibility()返回预定义的属性,并且您不想设置侦听器; i think the only option is View.isShown(). 我认为唯一的选择是View.isShown()。 i hope this helps. 我希望这有帮助。

if you just want to observe one view, try to create a custom view and override the draw() and set a flag in it like: 如果您只想观察一个视图,请尝试创建一个自定义视图并覆盖draw()并在其中设置一个标志,例如:

public DemoView extends TextView {
    private boolean mHasDrawn;
    // ...
    @override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mHasDrawn = true;
    }
}

then you can check the mHasDrawn to ensure the view has been drawn. 然后您可以检查mHasDrawn以确保已绘制视图。 However, I think setting a listener is better than this. 但是,我认为设置一个侦听器比这更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM