简体   繁体   中英

Protect View from continuous touch events

I'm building a camera app like vine, where you record a video as long as you hold your finger on the screen.

My problem: If someone taps quickly and continuously on the screen the camera logic receives too many events. It constantly tries to write files, close the recorder, open a new connection and so on.

The camera seems very fragile when it has to start and stop recording too often in a too short timeframe.

I tried setting a flag that prevents new motion events from accessing the camera while it is still busy working on a previous operation. It's ugly but it works. The main problem is, that the motion events seem to cue up and get fired after one another causing way too many events.

I'm using the motion events ACTION_UP and ACTION_DOWN to detect if a user placed the finger on the screen or releases it.

Is there a good way to disable caputuring motion events during the time the camera processes its preparation and release?

Try This:

//Class Variable
Timer timer;

 public boolean onTouch(View view, MotionEvent event) {
        // Check event type

        switch (event.getAction()) {

        // Finger down
        case MotionEvent.ACTION_DOWN:
              timer = new Timer();
              timer.schedule(recordingfunc(),1000);
                break;
        case MotionEvent.ACTION_UP:
           if(timer!=null){  
                  timer.cancel();
}
                      break;
     }
}

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