我有个按钮。当用户按住按钮时,我想要一个视频被记录下来。当用户释放按钮时,我想添加一些代码来处理视频并停止录制,但是当用户释放了按钮并执行onLongClickListener时,如何检测到呢?
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;
}
});发布于 2016-02-25 01:22:08
为了你的目的,我有一个现成的片段,看看它,https://gist.github.com/0x0af/013c4e7a90a481e04f77#file-snippet-java。
基本上,您所做的是实现View.OnTouchListener()并等待MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP
更新:使用计时器确定动作是否是长按压。
发布于 2016-02-25 01:06:03
查看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;
}
});
}https://stackoverflow.com/questions/35616277
复制相似问题