2016-02-25 112 views
0

我有一個按鈕。當用戶按住按鈕時,我想要錄製視頻。當用戶釋放按鈕時,我想添加一些代碼來處理視頻並停止錄製,但是如何檢測用戶何時釋放了按鈕並執行了onLongClickListener發佈onLongClickListener Android

snap.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      try { 
       initRecorder(mCameraView.getHolder().getSurface()); 
       mMediaRecorder.start(); 
       try { 
        Thread.sleep(10 * 1000); // This will recode for 10 seconds, if you don't want then just remove it. 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       finish(); 
       return true; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return false; 
     } 
    }); 
+0

也許你需要註冊觸摸,而不是長按聆聽者。看看這個https://stackoverflow.com/questions/3784514/capture-button-release-in-android – MoGa

+0

我已經有一個onClickListener實現的按鈕,它需要一個點擊圖片,並打算錄製視頻長時間點擊,我已經實現了圖片功能,現在我正在試圖實現視頻。 ontouch監聽器會干擾onclick監聽器嗎? – Alk

回答

1

我有一個現成片段爲你的目的,看看它https://gist.github.com/0x0af/013c4e7a90a481e04f77#file-snippet-java

基本上,你要做的就是實現View.OnTouchListener(),並等待MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP

更新:使用一個定時器,以確定是否行動是長按

+0

onTouchListener會以任何方式干擾我已經爲按鈕設置的onClickListener嗎?我需要2個按鈕功能,一個是用戶只需點擊按鈕(已經用onClickListener實現)拍攝照片,另一個是帶有保持/釋放事件的視頻錄製。 – Alk

+0

我建議測量印刷時間 - 這應該告訴你,如果您的印刷機很長 – xAF

+0

請您用一個簡短的例子來詳細說明。 – Alk

0

查看GestureDetector,您可以在其中檢測LongPress並分析onTouchEvent。這裏良好的信息Detecting a long press with Android

我用它以下列方式:

定義GestureDetector例如:

private class LongPressGestureDetector extends GestureDetector { 
    private boolean longPressDetected = false; 

    public LongPressGestureDetector(Context context, OnGestureListener listener) { 
     super(context, listener); 
    } 
} 

,然後使用它:

gestureDetector = new LongPressGestureDetector(holder.rootView.getContext(), 
      new GestureDetector.SimpleOnGestureListener() { 
       @Override 
       public void onLongPress(MotionEvent event) { 
        gestureDetector.longPressDetected = true; 
       } 
      }); 

    //set the content touch listener 
    holder.rootView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      gestureDetector.onTouchEvent(event); 
      if (gestureDetector.longPressDetected) { 
       Log.d(getClass().getSimpleName(), "Handle longPress touch event."); 
       gestureDetector.longPressDetected = false; 
       return true; 
      } 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: { 
        // handle MotionEvent.ACTION_DOWN 
        break; 
       } 
       case MotionEvent.ACTION_UP: { 
        // handle MotionEvent.ACTION_UP 
        break; 
       } 
       case MotionEvent.ACTION_CANCEL: { 
        // handle MotionEvent.ACTION_CANCEL 
        break; 
       } 
      } 
      return true; 
     } 
    }); 
} 
+0

您是否建議在我的onLongClick偵聽器中添加GestureDetector onTouchEvent方法?我有點困惑,請你提供一個例子,說明如何使用它來檢測用戶是否不再按住按鈕? – Alk

+0

我用一個例子更新了我的答案。 – liminal

+0

我也有一個爲這個按鈕定義的onClickListener,當用戶只需點擊按鈕時它會拍攝一張照片。這個代碼是否會干擾我的理解,onTouchListener和onClickListener會被同時觸發? – Alk