/** 复写onTouchEvent */
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
changeState(STATE_RECORDING);
break;
case MotionEvent.ACTION_MOVE:
if (isRecording) {// 是否在录音
/* 根据x,y的坐标来判断是否想要取消 */
if (wantToCancel(x, y)) {
changeState(STATE_WANT_TO_CANCEL);
} else {
changeState(STATE_RECORDING);
}
}
break;
case MotionEvent.ACTION_UP:
/*
* 五种情况 1、down以后还没触发longclick 2、down以后触发longClick还没有prepaer完毕就松开了
* 3、录音时间很短,没有达到我们设定的值 4、 5、
*/
if (!mReady) {
reset();
super. onTouchEvent(event);
}
if (!isRecording || mTime < 0.6f) {
mDialogManager.tooShort();
mAudioManager.cancel();
mHandler.sendEmptyMessageDelayed(MSG_DIALOG_DIMISS, 1300);
}else if (mCurState == STATE_RECORDING) {//正常录制结束
mDialogManager.dimissDialog();
mAudioManager.release();
if (mListener!=null) {
mListener.onFinish(mTime, mAudioManager.getCurrentFilePath());
}
} else if (mCurState == STATE_WANT_TO_CANCEL) {
mDialogManager.dimissDialog();
mAudioManager.cancel();
}
reset();
break;
}
return super.onTouchEvent(event);
}
/** 恢复状态以及标志位 */
private void reset() {
isRecording = false;
mReady = false;
mTime = 0;
changeState(STATE_NORMAL);
}
private boolean wantToCancel(int x, int y) {
if (x < 0 || x > getWidth()) {
return true;
}
if (y < -DISTANCE_Y_CANCEL || y > getHeight() + DISTANCE_Y_CANCEL) {
return true;
}
return false;
}