Notepad for Android Projects-----View images and play recordings

Source: Internet
Author: User

This article is my own study notes, welcome reprint. But please specify the source:http://blog.csdn.net/jesson20121020

Today, you can view the image and the recording function, click on the image when editing or browsing the notes. Open a self-defined activity(and, of course, the ability to call the system's Tukulai view) to see the original picture of the added picture. Instead of a scaled image, you can use your own activity to view the audio file. The function of playing recording is realized.

It can also be seen that we first create two of activity. Of course, the layout files are also indispensable. For example, the following:

Activity_show_picture.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical"     >        <imageview         android:id= "@+id/iv_showpic"        android:layout_width= " Match_parent "        android:layout_height=" match_parent "        android:scaletype=" Fitcenter "        /></ Linearlayout>
Showpicture.java

public class Showpicture extends Activity {private ImageView img; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (window.feature_ Custom_title); Setcontentview (r.layout.activity_show_picture); GetWindow (). Setfeatureint (Window.feature_custom_ Title, r.layout.title_add);//Set title TextView Tv_title = (TextView) Findviewbyid (r.id.tv_title); Tv_title.settext ("View Picture" ); Button Bt_back = (button) Findviewbyid (r.id.bt_back); Bt_back.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {ShowPicture.this.finish ();}}); Button Bt_del = (button) Findviewbyid (R.id.bt_save); Bt_del.setbackgroundresource (r.drawable.paint_icon_delete); img = (ImageView) Findviewbyid (r.id.iv_showpic); Intent Intent = This.getintent (); String Imgpath = Intent.getstringextra ("Imgpath"); Bitmap BM = bitmapfactory.decodefile (Imgpath); Img.setimagebitmap (BM);}} 
The main idea is to use ImageView to display a picture of the specified path. The path was passed in from the previous activity.

Here's the listener event. Just realized the function of return, as for, zoom out the picture. Rotate the picture. The next section will be implemented again. Activity_show_record.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 "android:background=" @drawable/          BG "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Match_parent "          android:orientation= "vertical" android:gravity= "center" android:layout_centerinparent= "true" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_c          Ontent "android:orientation=" horizontal "android:gravity=" center "android:layout_margin=" 5DP "        > <imageview android:id= "@+id/iv_record_wave_left" android:layout_width= "Wrap_content"        android:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_margin= "5DP" Android:background= "@anim/Record_wave_left "/> <imageview android:id=" @+id/iv_microphone "Android:layout_width=" W Rap_content "android:layout_height=" wrap_content "android:layout_gravity=" center "android:src=" @drawa Ble/record_microphone_icon "android:layout_margin=" 5DP "/> <imageview android:id=" @+id /iv_record_wave_right "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Andro        id:layout_gravity= "center" android:layout_margin= "5DP" android:background= "@anim/record_wave_right" /> </LinearLayout> <textview android:id= "@+id/tv_recordtime" android:l Ayout_width= "Match_parent" android:layout_height= "wrap_content" android:textcolor= "#499df7" android:t Extsize= "20SP" android:text= "00:00:00" android:gravity= "center" android:layout_margin= "5DP"/&    Gt </linearlayout> </RelativeLayout> 
Showrecord.java

public class ShowRecord extends Activity {private String audiopath;private int isplaying = 0;private animationdrawable ad_ Left,ad_right;private Timer mtimer;//Voice Operation object Private MediaPlayer MPlayer = null;private ImageView iv_record_wave_left,iv _record_wave_right,iv_microphone;private TextView tv_recordtime; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Requestwindowfeature ( Window.feature_custom_title); Setcontentview (R.layout.activity_show_record); GetWindow (). Setfeatureint ( Window.feature_custom_title, R.layout.title_add);//Set caption TextView Tv_title = (TextView) Findviewbyid (r.id.tv_title); Tv_title.settext ("View Recording"); Button Bt_back = (button) Findviewbyid (r.id.bt_back); Bt_back.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {if (isplaying = = 1) {mplayer.stop (); Mplayer.release ();} ShowRecord.this.finish ();}}); Button Bt_del = (button) Findviewbyid (R.id.bt_save); Bt_del.setbackgroundresource (R. drawable.paint_icon_delete); Intent Intent = This.getintent (); audiopath = Intent.getstringextra ("audiopath"); iv_ Microphone = (ImageView) Findviewbyid (R.id.iv_microphone); Iv_microphone.setonclicklistener (new Clickevent ()); iv_ Record_wave_left = (ImageView) Findviewbyid (r.id.iv_record_wave_left); iv_record_wave_right = (ImageView) Findviewbyid (r.id.iv_record_wave_right); ad_left = (animationdrawable) iv_record_wave_left.getbackground ();//ad_ left = (animationdrawable) iv_record_wave_left.getdrawable (); ad_right = (animationdrawable) iv_record_wave_ Right.getbackground ();//ad_right = (animationdrawable) iv_record_wave_right.getdrawable (); tv_recordTime = (TextView ) Findviewbyid (r.id.tv_recordtime);} Final Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {Case 1:string time [] = Tv_recordtime.gettext (). toString (). Split (":"); int hour = Integer.parseint (time[0]); int minute = Integer.parseint ( TIME[1]); int second = Integer.parseint (time[2]); if (second <) {second++;} else if (second = = && minute <) {Minute++;second = 0;} if (second = = && minute = = && Hour < 98) {Hour++;minute = 0;second = 0;} Time[0] = hour + ""; time[1] = minute + ""; time[2] = second + "";//Adjust the format to display on the screen if (second <) time[2] = "0" + second;if (mi Nute <] time[1] = "0" + minute;if (Hour <) time[0] = "0" + hour;//shown in TextView tv_recordtime.settext (time[0]+ ":" +t ime[1]+ ":" +time[2]); break;}}; Class Clickevent implements onclicklistener{@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub// Try to hear if (isplaying = = 0) {isplaying = 1;mplayer = new MediaPlayer (); Tv_recordtime.settext ("00:00:00"); Mtimer = new Timer (); Mplayer.setoncompletionlistener (New Mediacompletion ()); try {mplayer.setdatasource (audiopath); MPlayer.prepare (); Mplayer.start ();} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (SecurityException e) { TODO auto-generated catch Blocke.printstacktrace ();} catch (IllEgalstateexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-generat Ed catch Blocke.printstacktrace ();} Mtimer.schedule (New TimerTask () {@Overridepublic void run () {Message message = new Message (); message.what = 1;handler.sen dmessage (Message),}}, 1000,1000);//Play Animation Ad_left.start (); Ad_right.start ();} End Audition else{isplaying = 0;mplayer.stop (); Mplayer.release (); mPlayer = Null;mtimer.cancel (); mtimer = null;//Stop animation ad_ Left.stop (); Ad_right.stop ();}}} Class Mediacompletion implements oncompletionlistener{@Overridepublic void Oncompletion (MediaPlayer MP) { Mtimer.cancel (); mtimer = null;isplaying = 0;//Stop animation ad_left.stop (); Ad_right.stop (); Toast.maketext (Showrecord.this, "Play Done", Toast.length_short). Show (); Tv_recordtime.settext ("00:00:00");}}
When viewing the recording, the display processing of the playback time is used. and animation playback and stop, a little bit more complex, these in the previous "Join the recording" section is described.


With these two activities, the rest of the work is to start the two activity in the event of clicking on the picture or recording. But there is a problem, how to infer in the edittext of the picture-and-text mixed row is the image clicked. The recording, or the text?? This requires identifying those images from the edittext. Those are the words, and then further to the picture analysis exactly what is clicked is that picture, thus realizes the view detailed picture and the recording function, details such as following:

1. Record the location and source path of each picture in the EditText

In order to be able to view the original image and audio files at any time while editing and browsing, use a list to remember the location and path of each newly added image or recording after each addition, assuming that you are browsing the notes that already exist in the database. At the same time that the data is loaded, the same ListView is used to remember the location and path of all the pictures and recordings.

The main code such as the following:

Record the picture in the EditText. Used to infer when clicked is that one picture private list<map<string,string>> imglist = new arraylist<map<string,string> > ();

Each time you click the Memo list item. Go to view notes. Record the location and path of all pictures and recordings at the same time as the data is loaded. In detail, add the following code to the Loaddate () method:

Use list to record the location of the recording and the path where it is located. For Click event map<string,string> Map = new hashmap<string,string> (); Map.put ("Location", M.start () + "-" +m.end ()); Map.put ("path", path); Imglist.add (map);
Similarly. Also add the corresponding code after each addition to the image recording, and add the following code to the Insertbitmap () function, for example:

Use list to record the location of the recording and the path where it is located. For click event        map<string,string> Map = new hashmap<string,string> ();        Map.put ("Location", selectionindex+ "-" + (Selectionindex+spannablestring.length ()));        Map.put ("path", Imgpath);        Imglist.add (map);

2. Add a click event to EditText

Private Lineedittext et_notes; Et_notes.setonclicklistener (New Textclickevent ());

3. infer whether you clicked on a picture or plain text

To infer whether the click is a picture or plain text, the Spanned,imagespan is used. The main idea is to infer whether the location of the current click is within the position of the picture, and the main code is as follows:

spanned s = Et_notes.gettext (); imagespan[] Imagespans;imagespans = S.getspans (0, S.length (), imagespan.class); int SelectionStart = Et_notes.getselectionstart (); for (Imagespan span:imagespans) {int start = S.getspanstart (span); int end                                     = S.getspanend (span);//Find picture if (SelectionStart >= start && SelectionStart < end) { ... ...                                 }}
Hit the picture, the next thing is to infer that the click is the Picture it?

4. Decision click on the details of that picture

This uses the position and path of the image that was recorded in the first step, and it is clear that the position is used to infer the image that was clicked. The main code such as the following:

Find which picture is currently clicked//system.out.println (start+ "-----------" +end); String path = null;for (int i = 0;i < Imglist.size (); i++) {Map map = Imglist.get (i);//Found if (Map.get ("location"). Equals (S tart+ "-" +end)) {path = Imglist.get (i). Get ("path"); break;}}

5. Infer whether it is a recording or a picture, initiate the corresponding activity, and pass the path

There are two ways to view pictures, one is to call the system's gallery to open the picture, and the other is to define it yourself, here. I have come true. Open the recording with your own defined activity, such as the following:

Then infer whether the current picture is a recording. If the recording is recorded, then jumps to the activity of the audition recording, if not, jumps to the View Picture interface//recording, then jumps to the audition recording Activityif (Path.substring (Path.length () -3, Path.length ()) . Equals ("Amr")) {Intent Intent = new Intent (addactivity.this,showrecord.class); Intent.putextra ("audiopath", Path); StartActivity (intent);} Image. Then jump to view the image of the interface else{//there are two ways to view the picture. The first is to directly call the system's gallery to view the picture. The other is to define the activity//call System Gallery to view the picture/*intent Intent = new Intent (Intent.action_view); File File = new file (path); Uri uri = uri.fromfile (file), Intent.setdataandtype (URI, "image/*"), *///uses its own definition activityintent intent = new Intent ( Addactivity.this,showpicture.class); Intent.putextra ("Imgpath", Path); startactivity (intent);}
Above. The 3,4,5 step is actually implemented in the listener that was clicked. Complete code such as the following:

       //Set Listener class Textclickevent implements onclicklistener{@Overridepublic void for Eidttext OnClick (View v) {spanned s = et_notes.gettext (); imagespan[] Imagespans;imagespans = S.getspans (0, S.length (), imagespan.class); int selectionstart = Et_notes.getselectionstart (); for (Imagespan span:imagespans) {int start = S.getspanstart (span); int end = S.getspanend (span);//Find picture if (SelectionStart >= start && SelectionStart < End) {//bitmap Bitmap = ((bitmapdrawable) span.getdrawable ()). Getbitmap ();//Find the picture that is currently clicked//system.out.println ( start+ "-----------" +end); String path = null;for (int i = 0;i < Imglist.size (); i++) {Map map = Imglist.get (i);//Found if (Map.get ("location"). Equals (S tart+ "-" +end)) {path = Imglist.get (i). Get ("path"); break;}} Then infer whether the current picture is a recording. Assumed to be a recording. Then jumps to the activity of the audition recording, if not, jumps to the View Picture interface//recording, jumps to the audition recording the Activityif (Path.substring (Path.length () -3, Path.length ()). Equals ("Amr")) {Intent Intent = new Intent (addactivity.this,showrecord.class); Intent.putextra ("Audiopath", pATH); startactivity (intent);} Picture, then jump to view the image of the interface else{//there are two ways to view the picture, the first is to directly call the system's library to view the picture, the other is to define their own activity//call system Library View picture/*intent Intent = new Intent ( Intent.action_view); File File = new file (path); Uri uri = uri.fromfile (file), Intent.setdataandtype (URI, "image/*"), *///uses its own definition activityintent intent = new Intent ( Addactivity.this,showpicture.class); Intent.putextra ("Imgpath", Path); startactivity (intent);}} else//assumes that you clicked blank out or text. The focus is to open the soft keyboard imm.showsoftinput (et_notes, 0);}}

So far. The ability to view pictures and play recordings is realized.








Notepad for Android Projects-----View images and play recordings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.