我有一个应用程序,我有一个视频播放器,带有视频。如果我将手指放在屏幕上超过0.4秒,它将完成此活动,启动录像机活动,并自动开始录制。我的问题是:当我将手指从屏幕上移开时,我可以识别第二个活动,这样会停止录制吗?
当我按住屏幕时调用此方法:
Intent i = new Intent(VideoPlayerActivity.this, VideoRecorderActivity.class);
Bundle extras = new Bundle();
extras.putInt("mode", VideoRecorderModes.SEND_ALL_MODE);
extras.putInt("index", index);
extras.putInt("touches", 2);
i.putExtras(extras);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mPreview.getHolder().removeCallback(VideoPlayerActivity.this);
mPreview = null;
startActivity(i);
VideoPlayerActivity.this.finish();
在相机准备就绪后,我的第二个活动会调用此功能:
touches = getIntent().getExtras().getInt("touches");
if (touches == 1) {
mySurfaceView.setPressed(true);
recordButton.setEnabled(false);
okButton.setEnabled(false);
recordButton.setImageResource(R.drawable.shutter_pressed);
startTime = System.currentTimeMillis();
timer = new Timer();
timer.schedule(new TimerDisplayTask(), 0, 500);
startRecording();
touches = 0;
System.out.println("----------------here----------");
}
这是第二个活动的表面视图的SurfaceView onTouch方法:
mySurfaceView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "event : " + event.getAction() + "||is?: " + isRecording);
Log.i(TAG, "touches:" + touches);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//DO NOTHING
break;
case MotionEvent.ACTION_UP:
if (touches == 1 && isRecording) {
Log.i(TAG, "touch up");
timer.cancel();
timer.purge();
if (isRecording) {
mySurfaceView.setEnabled(false);
Log.i("VideoRecorder blabla", "touche enabled record, ok ,surface: " + recordButton.isEnabled() + "| " + okButton.isEnabled() + "|" + mySurfaceView.isEnabled());
stopRecording();
recordButton.setImageResource(R.drawable.shutter);
touches = 0;
}
}
break;
}
return false;
}
});
答案 0 :(得分:1)
我已经对这个答案做了一些研究,因为我自己也遇到了这个问题。
不幸的是,onTouchEvent似乎不会在它正在观看的活动中发生ACTION_DOWN事件时触发(这是来自观察和测试,请告诉我,如果我错了)。因此,我相信您想要做的路线是不可能的。
我还没有对它进行过测试,但是如果你使用相同的活动并交换出不同的片段,我相信onTouchEvent不会被重置,所以这种方法可能是你问题的可能解决方案。
答案 1 :(得分:0)
我实际上设法使用Fragments,使用FragmentManager作为主要活动,并将onTouchListener实现到Activity中,然后将我需要的命令传递给此活动的两个片段;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (Constants.gestureEnabled) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "current fragment: " + currentFragment);
if (currentFragment.toString().contains("VideoPlayerFragment")) {
if (currentFragment instanceof VideoPlayerFragment) {
((VideoPlayerFragment) currentFragment).onActionDown(event.getX());
}
} else if (currentFragment.toString().contains("VideoRecorderFragment")) {
if (currentFragment instanceof VideoRecorderFragment) {
((VideoRecorderFragment) currentFragment).onActionDown();
}
}
break;
case MotionEvent.ACTION_UP:
if (currentFragment.toString().contains("VideoPlayerFragment")) {
if (currentFragment instanceof VideoPlayerFragment) {
((VideoPlayerFragment) currentFragment).onActionUp();
}
} else if (currentFragment.toString().contains("VideoRecorderFragment")) {
if (currentFragment instanceof VideoRecorderFragment) {
((VideoRecorderFragment) currentFragment).onActionUp();
}
}
break;
default:
break;
}
}
return gDetector.onTouchEvent(event);
}