简体   繁体   English

保护View免受连续触摸事件的影响

[英]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. 我正在构建像vine这样的相机应用程序,只要您在屏幕上保持手指就可以在其中录制视频。

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. 我正在使用动作事件ACTION_UPACTION_DOWN来检测用户是否将手指放在屏幕上或释放手指。

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;
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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