Let's take a look at the registered recording interface of the To circle (QQ, and most other software is similar):
(⊙o⊙) ... It took some effort to achieve this effect,
The main difficulties are as follows:
1, follow the volume change of the microphone.
The microphone begins to feel the most painful part and is completely unaware of where to start. First directly with the picture is certainly not possible, want to achieve my final effect to achieve the need for 12 pictures, this too account for the resources directly GG. And then think of the mask implementation, but careful observation can be found that the microphone round progress bar is transparent around, that is, if the mask, unless the perfect coincidence (coincident will have to consider the screen adaptation problem, this is a big problem). The last thing I came up with was a little trickery: using a vertical progress bar to implement the microphone's progress section, and then the following Y-shape directly with canvas (in order to make it look like a microphone cost a lot of code to calculate ...). This looks similar, and avoids the screen fit problem, and does not need to load so many pictures and then turn around. But this is certainly not the best way, so kneel for more elegant implementation of the method !!!
2, the circle of Blue arc timing part.
3, gesture judgment.
So a summary of the feeling 1:2 and 3 added together is more difficult.
Implementation process:
The first is the interface implementation :
Activity_register.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " > < Span style= "White-space:pre" ></span><pre name= "code" class= "HTML" > <!--... Unimportant part ...-
<!--recording displays UI layer--<include android:id= "@+id/layout_register_popup" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" layout= "@layout/layout_recode_popwindow" android:gravity= "Center" android: Visibility= "Gone"/></relativelayout>
Unimportant parts are not posted, press to complete registration will appear black recording box layout in the <include> tag reference. This is temporarily set to not visible.
The following is a black recording frame layout:
Layout_recode_popwindow.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" Match_parent "> <relativelayout android:layout_centerhorizontal= "true" android:layout_centervertical= "true" Andro Id:layout_width= "180DP" android:layout_height= "180DP" android:background= "@drawable/shape_register_recoderla Yout "android:orientation=" vertical "> <com.whale.nangua.toquan.view.recodepopwindowcircle A Ndroid:id= "@+id/circleview_register_microphone" android:layout_width= "100DP" android:layout_height= " 100DP "android:layout_centerhorizontal=" true "android:layout_margintop=" 20DP "Android:bac kground= "@drawable/shape_register_recodepopwindow" ></com.whale.nangua.toquan.view.recodepopwindowcircle& Gt <linearlayout Android:layout_width= "Wrap_content" android:layout_height= "50DP" Android:layout_alignparentbot Tom= "true" android:layout_centerhorizontal= "true" > <textview Android:layout_mar Ginbottom= "10DP" android:paddingleft= "10DP" android:paddingright= "10DP" Andro Id:id= "@+id/tv_register_show" android:layout_width= "match_parent" android:layout_height= "mat Ch_parent "android:gravity=" center "android:text=" finger stroke, Cancel send "Android:textcolo R= "@android: Color/white" android:textsize= "16sp"/> </LinearLayout> </RELATIVELAYOUT&G T;</relativelayout>
The implementation results are as follows:
Code Implementation :
From the interface above you can see that there is a custom View:RecodePopWindowCircle.java
Package Com.whale.nangua.toquan.view;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.rectf;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.progressbar;import android.widget.relativelayout;import com.whale.nangua.toquan.r;/** * Created by Nangua on 2016/7/29. */public class Recodepopwindowcircle extends Relativelayout {ProgressBar progressbar_register_recode;//progress bar int Widt H Control total height int height; The total width of the control is Boolean is_show_recoding = true; The default setting is true for float scale = this.getresources (). Getdisplaymetrics (). density; Get pixel public recodepopwindowcircle (context context, AttributeSet Attrs) {Super (context, attrs); Resolves the layout defined in the XML in the constructor. Layoutinflater.from (context). Inflate (R.layout.layout_recode_cirClepopwindow, this, true); Init (); } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T, R, b); Progressbar_register_recode = (ProgressBar) This.findviewbyid (R.id.progressbar_register_recode); width = getwidth (); Height = getheight (); } private void Init () {} @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); Paint paint = new paint (); Brush if (is_show_recoding) {progressbar_register_recode.setvisibility (view.visible); Draw the Y-shape below the microphone, and move the whole down by 5 pixels 5*scale Paint.setcolor (color.white); Paint.setstrokewidth (6); Width Paint.setantialias (true); Anti-aliasing Paint.setstyle (Paint.Style.STROKE); Set Hollow RECTF oval = new RECTF (); RECTF object int xandy = WIDTH/2; int r = (int) (8 * scale); progress bar Bottom radius int space = (int) (5 * scale); The interval between the arc and the bottom progress bar oval.left = xandy-r-space; Left oval.top = xandy-2 * r-space + 5 * scale; Top Oval.right = Xandy + R + space; Right oval.bottom = Xandy + space + 5 * scale; To draw an arc Canvas.drawarc (oval, A, +, false, paint); Draw line int linelength = (int) (ten * scale); Canvas.drawline (Xandy, Xandy + space + 5 * scale, Xandy, Xandy + space + linelength + 5 * scale, paint); Draw arcs of dots//Because the position calculation is not accurate, so make some fine tuning int pointx = (int) (Math.Cos (Xandy) + R + space); Paint.setstyle (Paint.Style.FILL); Set solid//Right edge dot canvas.drawcircle (Xandy + pointx-1 * scale, Xandy + 2 * scale, 3, paint); Left edge Dot canvas.drawcircle (xandy-pointx + 1 * scale, Xandy + 2 * scale, 3, paint); Straight Bottom Edge dot Canvas.drawcircLe (Xandy, Xandy + space + linelength + 5 * scale, 3, paint); }//Otherwise show trash interface else {progressbar_register_recode.setvisibility (view.invisible); Bitmap Bitmap = Bitmapfactory.decoderesource (This.getcontext (). Getresources (), R.drawable.ic_trash); Canvas.drawbitmap (Bitmap,null, New Rect ((int) (WIDTH/2-20*scale), (int) (HEIGHT/2-25*scale), (int) (WIDTH/2 + 20*scale), (int) (HEIGHT/2 + 25*scale)), null); }//Draw the periphery of the Blue recording arc Paint.setcolor (Color.parsecolor ("#0CA6D9")); Paint.setstrokewidth (2); Width Paint.setantialias (true); Anti-aliasing Paint.setstyle (Paint.Style.STROKE); Set Hollow Canvas.drawarc (new RECTF (0 + 2, 0 + 2, width-2, height-2), -90, Endarc, false, paint); }/** * Sets whether to display the recording microphone in the recording */public void setisshowrecoding (Boolean is_show_recoding) {this.is_show_recod ING = is_show_recoding; Postinvalidate (); }//Set time arc end angle public void Setendarc (int endarc) {this.endarc = Endarc; Postinvalidate (); }//Set Progress public void setprogress (int progress) {progressbar_register_recode.setprogress (progress); } int endarc = 0; Arc End Angle}The implementation of the process note is clear, and here is a summary, mainly in the attempt to draw the "handle" of the Y-shaped microphone, as well as the peripheral blue recording arc, and provides a setup method to change the view in the activity.
Here again the Y-shape three points in the white circle, to make it more Q-bomb ...
Finally, the control code is implemented:
Registeractivity.java
Package Com.whale.nangua.toquan;import Android.app.activity;import Android.media.mediaplayer;import Android.os.bundle;import Android.os.handler;import Android.view.motionevent;import Android.view.View;import Android.widget.button;import Android.widget.imagebutton;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.textview;import Com.whale.nangua.toquan.view.recodepopwindowcircle;import Com.whale.nangua.toquan.voice.soundmeter;import java.io.ioexception;/** * Created by Nangua on 2016/7/26. */public class Registeractivity extends Activity implements View.onclicklistener, Radiogroup.oncheckedchangelistene R, Soundmeter.onstartrecoder {//Gender selected RadioButton private RadioButton Radiobtn_register_man; Private Radiogroup Radiogroup_register_sexcheck; Private View Layout_register_popup; Play Recording button Private button btn_register_play; Recording button private ImageButton imgbtn_register_recode; Private Recodepopwindowcircle Circleview_regisTer_microphone; Private long Startvoicet; The time to start recording private String voicename; Audio name//recording component private Soundmeter msensor; Private Handler Mhandler = new Handler (); Private Runnable Amptask = new Runnable () {public void run () {Double amp = msensor.getamplitude (); Get the audio graph Updatedisplay (AMP); Mhandler.postdelayed (Amptask, poll_interval); } }; private int endarc = 0; Private Runnable Updatearctask = new Runnable () {@Override public void run () {Endarc + = 1; Circleview_register_microphone.setendarc (ENDARC); Update Time Circle mhandler.postdelayed (Updatearctask, update_arc_interval); } }; Private Runnable Msleeptask = new Runnable () {public void run () {stop (); } }; Recording delay private static final int poll_interval = 300; Time ARC update delay, default one second private static final int update_arc_interval = 200; Audio Cue text TextView Tv_regiSter_show; Whether to cancel sending private Boolean if_cancle_send = false; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_register); Initview (); } float scale; pixel density int screenheight; Screen height private void Initview () {screenheight = This.getwindowmanager (). Getdefaultdisplay (). GetHeight (); Screen height scale = this.getresources (). Getdisplaymetrics (). density; Initialize the recording prompt text Tv_register_show = (TextView) Findviewbyid (r.id.tv_register_show); Initialize Play recording button Btn_register_play = (button) Findviewbyid (R.id.btn_register_play); Btn_register_play.setonclicklistener (this); Initialize the recording component msensor = new Soundmeter (); Msensor.setonstartrecodercallback (this); Circleview_register_microphone = (recodepopwindowcircle) Findviewbyid (R.id.circleview_register_microphone); Initialize gender selection rbtn Radiobtn_register_man = (RadiobuttON) Findviewbyid (R.id.radiobtn_register_man); Radiobtn_register_man.setchecked (TRUE); Radiogroup_register_sexcheck = (radiogroup) Findviewbyid (R.id.radiogroup_register_sexcheck); Radiogroup_register_sexcheck.setoncheckedchangelistener (this); Layout_register_popup = Findviewbyid (R.id.layout_register_popup); Layout_register_popup.setvisibility (view.invisible); Imgbtn_register_recode = (ImageButton) Findviewbyid (R.id.imgbtn_register_recode); Imgbtn_register_recode.setontouchlistener (New View.ontouchlistener () {@Override public boolean OnTo Uch (View V, motionevent event) {int Y = (int) Event.getrawy (); Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:btn_register_play.set Visibility (view.visible); Layout_register_popup.setvisibility (view.visible); Circleview_register_microphone.setendarc (0);//FirstStart time Arc Startvoicet = System.currenttimemillis (); Voicename = Startvoicet + ". Amr"; /** * Start Recording method, incoming audio name is event +.AMR */Start (voicename); Break Case MotionEvent.ACTION_UP:layout_register_popup.setVisibility (View.gone); Endarc = 0; Stop (); If you cancel send if (if_cancle_send) {//todo Cancel send} If you send else {//todo send} Break Case Motionevent.action_move://position is determined within the box range if (Y <= SCREENHEIGHT/2 + 9 0 * scale) {tv_register_show.settext ("finger release, Cancel send"); Tv_register_show.setbackground (Getresources (). getdrawable (R.drawable.shape_register_recodetv)); Circleview_register_microphone.setisshowrecoding (FALSE); } else {Tv_register_show.settext ("Swipe on finger, cancel send"); Tv_register_show.setbackground (NULL); Circleview_register_microphone.setisshowrecoding (TRUE); } break; } return true; } }); } private void Stop () {mhandler.removecallbacks (msleeptask); Mhandler.removecallbacks (Amptask); Mhandler.removecallbacks (Updatearctask); Msensor.stop (); Circleview_register_microphone.setprogress (0); }/** * Update display audio high-low graph * * @param signalema */private void Updatedisplay (double signalema) {int temp = 100/12; switch ((int) signalema) { Case 0:circleview_register_microphone.setprogress (temp); Break Case 1:circleview_register_microphone.setprogress (2 * temp); Break Case 2:circleview_register_microphone.setprogress (3 * temp); Break Case 3:circleview_register_microphone.setprogress (4 * temp); Break Case 4:circleview_register_microphone.setprogress (5 * temp); Break Case 5:circleview_register_microphone.setprogress (6 * temp); Break Case 6:circleview_register_microphone.setprogress (7 * temp); Break Case 7:circleview_register_microphone.setprogress (8 * temp); Break Case 8:circleview_register_microphone.setprogress (9 * temp); Break Case 9: Circleview_register_microphone.setprogress (* temp); Break Case 10:circleview_register_microphone.setprogress (one * temp); Break Case 11:circleview_register_microphone.setprogress (* temp); Break Default:break; }}/** * Start recording * * @param name */private void Start (String name) {Msensor.start (name); Mhandler.postdelayed (Amptask, poll_interval); Mhandler.postdelayed (Updatearctask, update_arc_interval); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.btn_register_play: MediaPlayer MediaPlayer = new MediaPlayer (); try {mediaplayer.setdatasource (Soundfilepath); Mediaplayer.prepare (); } catch (IOException e) {e.printstacktrace (); } mediaplayer.start (); Break }} string soundfilepath;//recording file path @Override public void Setvoicepath (string path) {Soundfilepath = Pat H }/** * Gender selection Change Monitoring method * * @param group * @param checkedid */@Override public void Oncheckedcha Nged (radiogroup Group, int checkedid) {switch (checkedid) {case R.id.radiobtn_register_women: TODO chose a male break; Case R.id.radiobtn_register_man://todo opted for female break; } }}
The main is the Mediarecoder class of various calls, relatively simple will not repeat.
The result of the final implementation is as follows:
In general, the function is relatively good to achieve, is the current experience is not too much to do more laborious.
Need to stay in the source of comments ha ~
Continue to Refuel ~
Android full imitation to circle recording interface implementation