点击按钮就会报错,不知道为什么
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
// TODO
changeState(STATE_RECORDING);
break;
case MotionEvent.ACTION_MOVE:
if (isRecording) {
// 根据x,y的坐标,判断是否想要取消
if (wantToCancle(x, y)) {
changeState(STATE_WANT_TO_CANCEL);
} else {
changeState(STATE_RECORDING);
}
}
break;
case MotionEvent.ACTION_UP:
if (!mReady)
{
reset();
return super.onTouchEvent(event);
}
if (!isRecording || mTime < 0.6f) {
mDialogManager.tooShort();
mAudioManager.cancel();
mHandler.sendEmptyMessageDelayed(MSG_DIALOG_DIMISS, 1300);
}
else if (mCurState == STATE_RECORDING) {// 正常录制结束
// release
mDialogManager.dimissDialog();
mAudioManager.release();
// callbackToAct
if (mListener != null) {
mListener.onFinish(mTime,
mAudioManager.getCurrentFilePath());
}
} else if (mCurState == STATE_WANT_TO_CANCEL) {
// cancle
mDialogManager.dimissDialog();
mAudioManager.cancel();
}
reset();
break;
}
return super.onTouchEvent(event);
}
// 恢复状态及标志位
private void reset() {
// TODO Auto-generated method stub
isRecording = false;
mReady = false;
mTime = 0;
changeState(STATE_NORMAL);
}
private boolean wantToCancle(int x, int y) {
// TODO Auto-generated method stub
if (x < 0 || x > getWidth()) {
return true;
}
if (y < -DISTANCE_Y_CANCLE || y > getHeight() + DISTANCE_Y_CANCLE) {
return true;
}
return false;
}
private void changeState(int state) {
// TODO Auto-generated method stub
if (mCurState != state) {
mCurState = state;
switch (state) {
case STATE_NORMAL:
// setBackgroundResource(R.drawable.btn_)
setText(R.string.str_recoder_normal);
break;
case STATE_RECORDING:
setText(R.string.str_recoder_recording);
if (isRecording) {
mDialogManager.recording();
}
break;
case STATE_WANT_TO_CANCEL:
setText(R.string.str_recoder_want_cancle);
mDialogManager.wantToCancle();
break;
}
}
}
}