简体   繁体   中英

Android surfaceview onTouchEvent limiting?

When you have a surfaceView class, used for animation/game/... you have the onTouchEvent() method and the drawing/screen-refreshing method in that same class. Now I want to just sleep the onTouchEvent method, so it doesn't get called +-80 times a second and flooding the cpu (and therefor causing lag). The problem is that if I sleep this class, it also stops drawing.

**Edit88 I'M Trying to stop onTouchEvent() to be called too frequently. my attempt:

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("firstTest",Integer.toString(event.getAction()));   //shows up
if (!sleeper.CurrentlySleeping) {
    Log.d("2ndTest",Integer.toString(event.getAction())); //not showing up
    ~~  some calculations and stuff  ~~

.

public class TouchSleeper extends Thread {
// If Thread is sleeping or not.
public static Boolean CurrentlySleeping;
public TouchSleeper() {
CurrentlySleeping = false;
this.setPriority(MIN_PRIORITY);
}

@Override
public void run() {
try {
    while (true) {
        CurrentlySleeping = true;
        TouchSleeper.sleep(50);
        CurrentlySleeping = false; }
    ~end~

First a piece of code from my SurfaceView class. Second piece of a class/thread, wich keeps changing a boolean value. Note how my first Log.d does return something, and the second one doesn't. This tells me that CurrentlySleeping is never false ? I still have not figured things out... ?

I think it would be simpler not to listen to the onTouch events in the first place instead of blocking out the processing code when the onTouch event does occur, in this case you will still have an overhead of the function getting called (and in this case when you don't want it)

So implement the onTouchListener interface and set it for the view using setOnTouchListener . Now when you don't want to listen to any onTouch events for a view set it to NULL.

Now instead of using sleep, try using an Alarm or post a delayed message to your thread handler to reset the onTouchListener for the view at a later point of time. (Personally, I think it is much neater than sleep :) )

If the class has an onTouchEvent method you cannot prevent it from being called. Just have onTouchEvent return false and do nothing else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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