Android-在invalidate()上重绘后的侦听器 [英] Android - listener after redrawing on invalidate()
问题描述
在我要求视图无效后,我希望在视图完成重绘后收到通知.如此答案中所述,invalidate()
方法不会立即调用视图的onDraw()
UI,而是安排在主线程空闲后执行的消息队列中重画.
I'd like to be notified after the view finishes redrawing after I ask it to invalidate. As said in this answer, the invalidate()
method doesn't call a View's onDraw()
the UI immediately, but schedules the repaint in a message queue which is executed after when the main thread is idle.
我想显示一个进度对话框,进行一些UI修改,然后在正确绘制视图时关闭该对话框.有什么技巧可以知道何时绘制视图?也许是通过子类化视图,覆盖onDraw()
方法?
I'd like to show a progress dialog, do some UI modifications and then dismiss the dialog when the view is drawn properly. Is there some trick that I can do to know when the View was drawn? Maybe by subclassing the view, overriding the onDraw()
method?
推荐答案
我认为您已经回答了自己的问题.为什么不呢?
I think you answered your question yourself. Why not:
public class DrawListenerView extends View{
private Callback callback;
public DrawListenerView(Callback callback){
this.callback = callback;
}
@Override
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
//add your method here you want to call
//Or use a Callback-pattern
callback.finish();
}
}
public interface Callback(){
public void finish();
}
如果您查看来源:
使整个视图无效.如果该视图可见,则将来会在某个时候调用onDraw(android.graphics.Canvas).必须从UI线程调用此方法.要从非UI线程进行调用,请调用postInvalidate().
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will >be called at some point in the future. This must be called from a UI thread. To call from >a non-UI thread, call postInvalidate().
尝试一下:)
可能您想使用回调来处理此问题.
probably you want to use a Callback to handle this.
这篇关于Android-在invalidate()上重绘后的侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!