从一个单独的线程调用requestLayout [英] Calling requestLayout from a separate thread

查看:233
本文介绍了从一个单独的线程调用requestLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这比线程的GLSurfaceView子类(其中包含渲染)制成一个单独的线程运行GLSurfaceView.Renderer一个OpenGL游戏循环(更新+绘制)。

I have an OpenGL game loop (update+draw) on a GLSurfaceView.Renderer that runs on a separate thread than the thread that the GLSurfaceView subclass (which contains the renderer) was made.

有时,更新循环逻辑判定,嗯,我们需要将视图调整到一半大小。然后,它调用GLSurfaceView视图的requestLayout,我的onMeasure功能将采取调整大小的照顾。

Sometimes, the update loop logic decides, "Hmm, we need to resize the view to half size". It then calls the GLSurfaceView view's requestLayout, and my onMeasure function would take care of the resizing.

然而,事实证明,我不应该从较做出的视图层次结构原来的线程以外的任何其他线程调用requestLayout。我得到了以下异常:

However, it turns out I'm not supposed to call requestLayout from any other thread other than the original thread that made the view hierarchy. I got the following exception:

E/AndroidRuntime( 3693): FATAL EXCEPTION: GLThread 3424
E/AndroidRuntime( 3693): Process: com.android.gl2jni, PID: 3693
E/AndroidRuntime( 3693): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
E/AndroidRuntime( 3693):        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
E/AndroidRuntime( 3693):        at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
E/AndroidRuntime( 3693):        at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
E/AndroidRuntime( 3693):        at android.view.View.invalidate(View.java:10935)

<snip>

我试着用postInvalidate,但我想我需要的是postRequestLayout更接近于()。然而,仅是requestLayout()。

I tried using postInvalidate, but I think what I need is something closer to postRequestLayout(). However, there's only requestLayout().

我怎么办触发requestLayout,不知何故?

How do I trigger requestLayout, somehow?

我不知道,如果这复杂的事情,但观点是无论运行输入法服务拥有。是的,这是一个基于OpenGL Android键盘,但这是另一个很长的故事,可能不会是这个问题太重要。

I'm not sure if this complicates things, but the view is owned by whatever runs the Input Method Service. Yep, it's an OpenGL based android keyboard, but that's another long story that might not be too relevant to this issue.

推荐答案

您需要后() A 的Runnable 与任何UI相关的行动,主线程。

You need to post() a Runnable with any UI-related actions to the main thread.

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        // do UI work
        yourview.requestLayout();
    }
});

要注意的是,code你把运行里面()方法不会立即完成是非常重要的。它被添加到该主线程进程和将在未来得到执行某个的队列。

It's important to note that the code you put inside that run() method will not complete immediately. It gets added to a queue that the main thread processes and will get executed "sometime" in the future.

这篇关于从一个单独的线程调用requestLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆