项目作者: wotomas

项目描述 :
Voice Record Button that has ripple effect with users voice
高级语言: Java
项目地址: git://github.com/wotomas/VoiceRipple.git
创建时间: 2017-08-24T09:27:47Z
项目社区:https://github.com/wotomas/VoiceRipple

开源协议:Apache License 2.0

下载


VoiceRipple

API Download

About

Voice Record Button that has ripple effect with users voice. Calculation of decibels from max amplitude by using the following:

  1. power_db = 20 * log10(amp / amp_ref)

Main method consists the following:

  1. if (THRESHOLD >= 0) {
  2. if (rippleRadius - THRESHOLD >= powerDb + MIN_RADIUS || powerDb + MIN_RADIUS >= rippleRadius + THRESHOLD) {
  3. rippleRadius = powerDb + MIN_RADIUS;
  4. backgroundRadius = (int) (rippleRadius * backgroundRippleRatio);
  5. } else {
  6. // if decreasing velocity reached 0, it should simply match with ripple radius
  7. if (((backgroundRadius - rippleRadius) / rippleDecayRate) == 0) {
  8. backgroundRadius = rippleRadius;
  9. } else {
  10. backgroundRadius = backgroundRadius - ((backgroundRadius - rippleRadius) / rippleDecayRate);
  11. }
  12. }
  13. invalidate();
  14. }

The approximated decibel power of the sound is used to animate the button’s ripple effect.

Demo

Implement

  1. repositories {
  2. jcenter()
  3. }
  4. dependencies {
  5. compile "info.kimjihyok:voice-ripple-record-button:${voice-ripple-button-version}"
  6. }

XML Setup

  1. <info.kimjihyok.ripplelibrary.VoiceRippleView
  2. android:id="@+id/voice_ripple_view"
  3. android:layout_width="200dp"
  4. android:layout_height="200dp"
  5. app:rippleColor="@color/colorPrimary"></info.kimjihyok.ripplelibrary.VoiceRippleView>
  1. voiceRipple = (VoiceRippleView) findViewById(R.id.voice_ripple_view);
  2. // set view related settings for ripple view
  3. voiceRipple.setRippleColor(ContextCompat.getColor(this, R.color.colorPrimary));
  4. voiceRipple.setRippleSampleRate(Rate.LOW);
  5. voiceRipple.setRippleDecayRate(Rate.HIGH);
  6. voiceRipple.setBackgroundRippleRatio(1.4);
  7. // set recorder related settings for ripple view
  8. voiceRipple.setMediaRecorder(new MediaRecorder());
  9. voiceRipple.setOutputFile(audioFile.getAbsolutePath());
  10. voiceRipple.setAudioSource(MediaRecorder.AudioSource.MIC);
  11. voiceRipple.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  12. voiceRipple.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  1. // set inner icon for record and recording
  2. voiceRipple.setRecordDrawable(ContextCompat.getDrawable(this, R.drawable.record), ContextCompat.getDrawable(this, R.drawable.recording));
  3. voiceRipple.setIconSize(30);
  4. // change recording status when clicked
  5. voiceRipple.setOnClickListener(new View.OnClickListener() {
  6. @Override
  7. public void onClick(View view) {
  8. if (voiceRipple.isRecording()) {
  9. voiceRipple.stopRecording();
  10. } else {
  11. try {
  12. voiceRipple.startRecording();
  13. } catch (IOException e) {
  14. Log.e(TAG, "startRecording() error: ", e);
  15. }
  16. }
  17. }
  18. });

Stop and Destory

  1. // It is required to stop VoiceRippleView at onStop and to destory at onDestory to prevent memory leak and unexpected
  2. @Override
  3. protected void onStop() {
  4. super.onStop();
  5. voiceRipple.onStop();
  6. }
  7. @Override
  8. protected void onDestroy() {
  9. super.onDestroy();
  10. voiceRipple.onDestroy();
  11. }