我正在尝试像SnapChat这样的实现,用户点击它捕获照片的按钮,当用户按住按钮视频录制时。
我正在使用CWAC-Camera Library的CamerView进行相机实现。
这是在我的onCreate方法中,我为 TakingVideo
分配了 CLickListeners 和布尔按钮 btnTakePhoto.setOnLongClickListener(takeVideoListener);
btnTakePhoto.setOnTouchListener(touchListener);
TakingVideo = false;
以下是OnlongClickListeners和OnTouchListeners的方法;
private View.OnLongClickListener takeVideoListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
TakingVideo = true;
try {
if (cameraView.record()) {
Toast.makeText(TakePhotoActivity.this,"Is Recording",Toast.LENGTH_SHORT).show();
} else {
//TODO: show an error to the user
try {
cameraView.stopRecording();
Toast.makeText(TakePhotoActivity.this,"Stopped Recording",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
};
private View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (TakingVideo) {
Toast.makeText(TakePhotoActivity.this,"Finished Recording",
Toast.LENGTH_SHORT).show();
TakingVideo = false;
TakenVideo.start();
try {
cameraView.stopRecording();
Toast.makeText(TakePhotoActivity.this,"Stop Recording",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(TakePhotoActivity.this,"Error Stop Recording",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
return false;
}
};
按钮也被分配到Take Photo OnClick
@OnClick(R.id.btnTakePhoto)
public void onTakePhotoClick() {
btnTakePhoto.setEnabled(false);
cameraView.takePicture(true, true);
animateShutter();
}
到目前为止,TouchListener工作正常但onLongClick Listener无效。
的链接