在 android 上处理输入的问题,即在 hello-gl2 中

发布于 2024-10-21 08:13:23 字数 1509 浏览 2 评论 0原文

如何使输入处理在单独的线程中运行?我已经修改了 hello-gl2 示例,如下所示:

package com.android.gl2jni;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;

import java.io.File;


public class GL2JNIActivity extends Activity {

    GL2JNIView mView;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mView = new GL2JNIView(getApplication());
        setContentView(mView);
        setRequestedOrientation(0);
    }

    @Override protected void onPause() {
        super.onPause();
        mView.onPause();
    }

    @Override protected void onResume() {
        super.onResume();
        mView.onResume();
    }

    //the modified part
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        //return super.onTouchEvent(event);
        mView.queueEvent(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(33);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        return true;
    }

}

这是为了尝试在连续触摸屏幕时减少系统延迟,我已经看到此解决方案在 Android 上的其他 openGL 应用程序中使用,尽管不是与 openGl ES 2.0 一起使用。问题是我的渲染在调用 thread.sleep 时冻结。但它不是应该在一个不影响渲染线程的单独线程中吗?

How to i make the input handling run in a separate thread? I have modified the hello-gl2 example like this:

package com.android.gl2jni;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;

import java.io.File;


public class GL2JNIActivity extends Activity {

    GL2JNIView mView;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mView = new GL2JNIView(getApplication());
        setContentView(mView);
        setRequestedOrientation(0);
    }

    @Override protected void onPause() {
        super.onPause();
        mView.onPause();
    }

    @Override protected void onResume() {
        super.onResume();
        mView.onResume();
    }

    //the modified part
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        //return super.onTouchEvent(event);
        mView.queueEvent(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(33);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        return true;
    }

}

This i to try to make the system lag less when i touch the screen continuously, I have seen this solution being used in other openGL apps on the android, although not with openGl ES 2.0. The problem is that my rendering freezes when call thread.sleep. But isn't it suppose to be in a separate thread that not effect the rendering thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-10-28 08:13:23

将 Thread.sleep(33) 放入第二个线程的执行中不会给你带来任何好处。事实上,如果它与您的 GL 引擎在同一线程中运行,它可能会造成很大的危害,因为每次发生事件时,您都会让引擎停止执行任何操作 33 毫秒(长于 1 帧)。

通常会进行睡眠,以便旧版本的平台不会限制事件(它们传递事件的速度与应用程序消耗事件的速度一样快)。为了真正发生这种情况,您需要在实际传递事件的主线程中进行延迟。

我无法帮助你更多,因为这个 GL2JNIView 东西不是平台的一部分,当我查找它的代码时,我发现的第一个结果没有queueEvent()方法,所以我真的不知道知道那是做什么的。

这让我们想到了最后一点……如果人们实际上没有在平台内部编写代码,那么将东西放入 com.android(和 android)命名空间中是绝对错误的。此命名空间不适合应用程序开发人员。它不适合为应用程序开发人员编写辅助函数的人。用于平台内部实现。使用这些命名空间作为自己的代码的程序可能会跨不同的平台版本或设备,因为它们可能与平台使用的符号发生冲突。

Putting the Thread.sleep(33) inside of execution in the second thread isn't going to gain you anything. In fact it will probably cause a lot of harm, if this is running in the same thread as your GL engine, because you are making your engine stop doing anything for 33ms (longer than 1 frame) every time there is an event.

Usually the sleep is done so that older versions of the platform that don't throttle events (they deliver events as fast as the application will consume them). For that to actually happen, you need to delay in the main thread where the event is actually being delivered.

I can't help you much more than that, because this GL2JNIView thing is not part of the platform, and when I looked for its code the first result I found didn't have a queueEvent() method, so I don't really know what that does.

Which brings us to a last point... it is ABSOLUTELY TOTALLY WRONG for people to be putting stuff in the com.android (and android) namespace if they are not actually writing code inside of the platform. This namespace is NOT for app developers. It is NOT for people writing helper functions for app developers. It is for the internal implementation of the platform. Programs that use these namespaces for their own code CAN and WILL break across different platform versions or devices because they can conflict with symbols used by the platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文