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() {
开发者_JAVA百科 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?
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.
精彩评论