问题描述:按住录音按钮滑动时,超出取消录音范围后返回的dialog视图闪一下后又自动变成录音状态的视图,录音过短也是这样闪一下又变回最初的dialog视图,不知道哪里逻辑错了造成他又掉最初的显示视图。(字数限制所以删掉了一鞋方法) RecordarButton 类↓ package com.example.administrator.myapplication.recorderchat.customview; public class RecordarButton extends Button implements AudioManager.OnAudioPrepareWellListener { private Context mContext; //正在录音 private final int RECORDARING = 1; //取消录音 private final int WANT_TO_CANCEL = -1; //等待录音 private final int RECORDAR_NORMAL = 0; //当前状态 private int currState = RECORDARING; //是否正在录音 是否完成录音准备 private boolean isRecordar, isReady; //Y轴上滑取消距离 private final int DISTANCE_Y_CANCEL = 50; private DialogManager mDialogManager; private AudioManager mAudioManager; private String dir; private final int AUDIO_PREPARED = 1; private final int AUDIO_VOLUME = 2; private final int AUDIO_CANCEL = 3; private final int MAXVOLUME = 7; private float totalTime; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case AUDIO_PREPARED: mDialogManager.show(); new Thread(new GetVolumeRunnable()).start(); break; case AUDIO_VOLUME: mDialogManager.updateVolume(mAudioManager.getVolume(MAXVOLUME)); break; case AUDIO_CANCEL: mDialogManager.dismiss(); break; } } }; private OnAudioRecorderDoneListener mListener; public interface OnAudioRecorderDoneListener { void OnAudioRecorderDone(float totalTime, String filePath); } public void setOnAudioRecorderDoneListener(OnAudioRecorderDoneListener listener) { mListener = listener; } public RecordarButton(Context context) { this(context, null); } public RecordarButton(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; dir = Environment.getExternalStorageDirectory() + "/store_temp_audios"; mAudioManager = new AudioManager(dir); mDialogManager = new DialogManager(context); mAudioManager.setOnAudioPrepareWellListener(RecordarButton.this); setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { //长按弹出dialog窗口 // isReady = true; // mAudioManager.setOnAudioPrepareWellListener(RecordarButton.this); // mAudioManager.prepareAudio(); // isRecordar = true; isReady = true; mAudioManager.prepareAudio(); isRecordar = true; return true; } }); } /** * 每0.1秒获取一下当前音频振幅,累加时间 */ class GetVolumeRunnable implements Runnable { @Override public void run() { while (isRecordar) { try { Thread.sleep(100); totalTime += 0.1f; mHandler.sendEmptyMessage(AUDIO_VOLUME); } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 实现音频准备完毕的回调方法 */ @Override public void audioPrepareWell() { mHandler.sendEmptyMessage(AUDIO_PREPARED); } /** * 重写onTouchEvent事件 * * @param event * @return */ @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(RECORDARING); break; //如果正在录音 判断用户滑动意图 case MotionEvent.ACTION_MOVE: if (isRecordar) { if (wangToCancel(x, y)) { //如果想取消 改变按钮状态为取消 changeState(WANT_TO_CANCEL); } else { //如果不取消 改变按钮状态为录音状态 changeState(RECORDARING); } } break; //当用户松开手指 将状态和标记都置回最初 case MotionEvent.ACTION_UP: if (!isReady) { reset(); break; } if (!isRecordar || totalTime < 1) { mDialogManager.tooShort(); mAudioManager.cancelAudio(); mHandler.sendEmptyMessageDelayed(AUDIO_CANCEL, 1300); } else if (currState == RECORDARING) { //正常录制完成 mAudioManager.release(); mDialogManager.dismiss(); } else if (currState == WANT_TO_CANCEL) { mAudioManager.cancelAudio(); mDialogManager.dismiss(); } reset(); break; } return super.onTouchEvent(event); } /** * 执行完后重置工作 */ private void reset() { isRecordar = isReady = false; totalTime = 0; changeState(RECORDAR_NORMAL); } /** * 判断用户移动的意图 * * @param x * @param y * @return */ private boolean wangToCancel(int x, int y) { if (x < 0 || x > getWidth() || y < -DISTANCE_Y_CANCEL || y > getHeight() + DISTANCE_Y_CANCEL) return true; return false; } /** * 根据当前状态 改变按钮样式 * * @param state */ private void changeState(int state) { if (currState != state) { currState = state; switch (currState) { case RECORDAR_NORMAL: setBackgroundResource(R.drawable.recordar_btn_shape_normal); setText(R.string.recordar_normal); break; case RECORDARING: setBackgroundResource(R.drawable.recordar_btn_shape_selected); setText(R.string.recordar_down); mDialogManager.recordaring(); break; case WANT_TO_CANCEL: setBackgroundResource(R.drawable.recordar_btn_shape_selected); setText(R.string.recordar_cancel); mDialogManager.wantToCancel(); break; } } } }
米沙之国
相关分类