MyRecorder(仿微信,录制音频并发送功能)
①布局实现(activity_main.xml)
布局采用线性布局,上面使用的一个ListView,下面使用的是一个自定义的Button(会在下面进行介绍)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.yitong.myrecorder.MainActivity">
<ListView
android:id="@+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ebebeb"
android:dividerHeight="10dp"
android:divider="@null"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<com.yitong.myrecorder.view.AudioRecorderButton
android:id="@+id/main_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:minHeight="0dp"
android:padding="6dp"
android:text="@string/str_recoder_normal"
android:textSize="20sp"
android:textColor="#727272"
android:background="@drawable/btn_recorder_normal"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc"/>
</FrameLayout>
</LinearLayout>
相关使用的string值(需要添加到value/string中):
<string name="str_recoder_normal">按住说话</string>
<string name="str_recorder_recording">松开结束</string>
<string name="str_recorder_want_cancel">松开手指,取消发送</string>
<string name="str_dialog_want_cancel">手指上滑,取消发送</string>
<string name="str_dialog_want_send">手指上滑,取消发送</string>
<string name="str_dialog_time_short">录音时间过短</string>
②我们分析一下自定Button的几种状态:
1.正常状态 (在初次显示,即没有点击的时候显示的状态,显示的文本为“按住说话”)
2.录音状态 (当手指按在Button上时,即为录音状态,显示的文本为“松开结束”)
3.取消状态 (当手指上滑,此时若松开手指,便取消发送,即为取消状态,显示的文本为“松开手指,取消发送”)
③当录音状态时,在View上有一个Dialog的提示,首先我们先自定义这个Dialog的布局: