@Override public void handleMessage(android.os.Message msg) { RecordVoiceBtn controller = mController.get(); if (controller != null) { int restTime = msg.getData().getInt("restTime", -1); // 若restTime>0, 进入倒计时 if (restTime > 0) { controller.mTimeUp = true; android.os.Message msg1 = controller.mVolumeHandler.obtainMessage(); msg1.what = 60 - restTime + 1; Bundle bundle = new Bundle(); bundle.putInt("restTime", restTime - 1); msg1.setData(bundle); // 创建一个延迟一秒执行的HandlerMessage,用于倒计时 controller.mVolumeHandler.sendMessageDelayed(msg1, 1000); controller.mRecordHintTv.setText( String.format( controller.mContext.getString(R.string.rest_record_time_hint), restTime)); // 倒计时结束,发送语音, 重置状态 } else if (restTime == 0) { controller.finishRecord(); controller.setPressed(false); controller.mTimeUp = false; // restTime = -1, 一般情况 } else { // 没有进入倒计时状态 if (!controller.mTimeUp) { if (msg.what < CANCEL_RECORD) { controller.mRecordHintTv.setBackgroundColor(Color.TRANSPARENT); controller.mRecordHintTv.setText( controller.mContext.getString(R.string.move_to_cancel_hint)); } else { controller.mRecordHintTv.setBackgroundColor(Color.RED); controller.mRecordHintTv.setText( controller.mContext.getString(R.string.cancel_record_voice_hint)); } // 进入倒计时 } else { if (msg.what == CANCEL_RECORD) { controller.mRecordHintTv.setText( controller.mContext.getString(R.string.cancel_record_voice_hint)); if (!mIsPressed) controller.cancelRecord(); } } // if (msg.what >=0 && msg.what < 5){ // controller.mChatView.setWaveHeight(msg.what); // } if (controller.mVolumeIv != null) { controller.mVolumeIv.setImageResource(res[msg.what]); } // if (msg.what <=3) { // // controller.mChatView.getRecordAnimImage().setImageResource(recordRes[msg.what]); // } else { // // controller.mChatView.getRecordAnimImage().setImageResource(recordRes[3]); // } } } }
@Override public boolean onTouchEvent(MotionEvent event) { this.setPressed(true); int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: // mChatView.startRecordAnim(); this.setText(mContext.getString(R.string.send_voice_hint)); mIsPressed = true; time1 = System.currentTimeMillis(); mTouchY1 = event.getY(); // 检查sd卡是否存在 sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if (sdCardExist) { if (isTimerCanceled) { timer = createTimer(); } timer.schedule( new TimerTask() { @Override public void run() { android.os.Message msg = myHandler.obtainMessage(); msg.what = START_RECORD; msg.sendToTarget(); } }, 500); } else { MUIToast.toast(this.getContext(), R.string.sdcard_not_exist_toast); this.setPressed(false); this.setText(mContext.getString(R.string.record_voice_hint)); mIsPressed = false; return false; } break; case MotionEvent.ACTION_UP: // mChatView.stopRecordAnim(); this.setText(mContext.getString(R.string.record_voice_hint)); mIsPressed = false; this.setPressed(false); mTouchY2 = event.getY(); time2 = System.currentTimeMillis(); if (time2 - time1 < 500) { cancelTimer(); return true; } else if (time2 - time1 < 1000) { cancelRecord(); } else if (mTouchY1 - mTouchY2 > MIN_CANCEL_DISTANCE) { cancelRecord(); } else if (time2 - time1 < 60000) finishRecord(); break; case MotionEvent.ACTION_MOVE: mTouchY = event.getY(); // 手指上滑到超出限定后,显示松开取消发送提示 if (mTouchY1 - mTouchY > MIN_CANCEL_DISTANCE) { this.setText(mContext.getString(R.string.cancel_record_voice_hint)); mVolumeHandler.sendEmptyMessage(CANCEL_RECORD); // mChatView.stopRecordAnim(); // mChatView.setWaveHeight(5); if (mThread != null) mThread.exit(); mThread = null; } else { this.setText(mContext.getString(R.string.send_voice_hint)); if (mThread == null) { mThread = new ObtainDecibelThread(); mThread.start(); } } break; case MotionEvent.ACTION_CANCEL: // 当手指移动到view外面,会cancel this.setText(mContext.getString(R.string.record_voice_hint)); // mChatView.stopRecordAnim(); cancelRecord(); break; } return true; }