0

I implemented my own camera to capture image and video. I want to perform two actions on the same button.

  1. When the button is clicked I want to execute code for image capture.
  2. When I continuously press the button I want to start recording and when I release the button I want to stop recording.

Suppose I have 3 methods for above task namely captureImage(), startVideo() and stopVideo().

How to implement the above two actions on the same button? When should I call above three methods?

I tried using onClick, ACTION_DOWN and ACTION_UP, however, in this case onClick never gets called. Always ACTION_DOWN and ACTION_UP gets called.

4

3 回答 3

1

这就是我解决它的方法。在 ACTION_DOWN 1 秒后开始视频录制。在 ACTION_UP 检查您是否正在捕获视频,然后停止捕获,否则捕获图像并取消视频捕获处理程序。

private Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        isImage = false;
        startRecording();
    }
};


mCaptureButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // Start recording after 1 sec
                    isImage = true;
                    mHandler = new Handler();
                    mHandler.postDelayed(mRunnable, 1000);
                    break;
                case MotionEvent.ACTION_UP:
                    // If video recording was started after 1 sec delay then stop recording
                    // otherwise capture image
                    if(isImage) {
                        // Cancel handler for video recording
                        mHandler.removeCallbacks(mRunnable);
                        // Capture image
                        mCamera.takePicture(null, null, mPicture);
                    } else {
                        // Stop video recording
                        stopRecording();

                    }
                    break;
            }
            return true;
        }
    });
于 2015-09-02T06:06:43.520 回答
0

你可以使用onTouchListener

这将返回 MotionEvent 以及所需的事件 ACTION_UP ACTION_DOWN ...

于 2015-08-19T18:59:23.833 回答
0

使用 OnTouchListener 和 OnClickListener :

View v = find....;
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //capture();
        }
    });
    v.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getAction();

            if(action==MotionEvent.ACTION_DOWN)startRecording();
            if(action==MotionEvent.ACTION_UP)stopRecording();
            return true;//return true to avoid onClick...
        }
    });
}
于 2015-08-19T19:18:01.690 回答