Android Combat Simple Tutorial-the 71st Gun (asynchronous network download network image and image gallery production)

Source: Internet
Author: User

The first is to implement the asynchronous download network picture, the layout file is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/btn_getpic" android:layout_width= "Fill_        Parent "        android:layout_height=" wrap_content "        android:text=" gets the number of pictures "/>    <imageview        android: Id= "@+id/img"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"/></ Linearlayout>


Network request class, inheriting thread:

Package Com.test.demo;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import java.io.InputStream; Import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.handler;import Android.widget.imageview;public class Loadthread extends Thread {private String url;private Handler handler;private ImageView imageview;public loadthread (String URL, Handler Handler, ImageView ImageView) {this.url = Url;this.handler = Han Dler;this.imageview = ImageView;} @Overridepublic void Run () {try {bytearrayoutputstream bytearrayoutputstream = new Bytearrayoutputstream (); URL conn = new URL (URL); HttpURLConnection httpurlconnection = (httpurlconnection) conn.openconnection (); InputStream in = Httpurlconnection.getinputstream (); byte data[] = new Byte[2 * 1024];int len = 0;while (len = in.read (data))! =-1) {Bytea Rrayoutputstream.write (data, 0, Len);} Final Bitmap Bitmap = BitmapfactorY.decodebytearray (Bytearrayoutputstream.tobytearray (), 0,bytearrayoutputstream.tobytearray (). length); Handler.post (New Runnable () {public void run () {imageview.setimagebitmap (bitmap);}}); catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {}}}


The constructor method requires the URL address of the incoming request, the ImageView control and the handler object, which makes it easy to return the result to the UI thread, overwrite the Run method, write the method of the network request in the Run method, and finally call the handler post method. The request results are reflected on the imageview of the UI. I am unfamiliar with the network request IO part of the students can learn this part of the knowledge, writing routines are fixed.

Mainactivity.java:

Package Com.test.demo;import Com.example.testandroid.r;import Android.app.activity;import android.os.Bundle;import Android.os.handler;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button Import Android.widget.imageview;public class Mainactivity extends Activity {private Button btn;private ImageView img; Private String URL = "http://pic.yesky.com/uploadImages/2016/065/03/8VG0Q0HU97X3.jpg";p rivate Handler Handler = new Handler (); @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); btn = (Button) Findviewbyid (r.id.btn_getpic); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {new Loadthread (URL, Handler, IMG). Start ();}); img = (ImageView) Findviewbyid (r.id.img);}}

Note-This app needs to configure network permissions. Configure permissions after you run the project as follows:

Below you can add an up and down button to implement the picture switch, the Main.xml code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <imageview        android:id= "@+id/img"        android:layout_width= "match_parent"        android:layout_height= "400DP        " Android:layout_alignparenttop= "true"/>    <button        android:id= "@+id/btn_pre"        android:layout_ Width= "150DP"        android:layout_height= "wrap_content"        android:layout_below= "@+id/img"        android:text = "Previous"/>    <button        android:id= "@+id/btn_next"        android:layout_width= "150DP"        android: layout_height= "Wrap_content"        android:layout_below= "@+id/img"        android:layout_alignparentright= "true"        android:text= "Next"/></relativelayout>


Mainactivity.java modified as follows:

Package Com.test.demo;import Com.example.testandroid.r;import Android.app.activity;import android.os.Bundle;import Android.os.handler;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button ; Import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity implements  Onclicklistener {private ImageView imageview;private button buttonnext;private button buttonpre;private String urls[] = { "Http://pic.yesky.com/uploadImages/2016/065/03/8VG0Q0HU97X3.jpg", "http://img.sc115.com/hb/yl1/23/ 88160202491142.jpg "," http://pic.yesky.com/uploadImages/2016/065/48/2A05DMJ41LAN.jpg "," http://img4q.duitang.com /uploads/item/201408/12/20140812200853_vvnz2.png "};p rivate Handler Handler = new Handler ();p rivate int i=0;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main) ImageView = (ImageView) Findviewbyid (r.id.img); buttonnext = (Button) Findviewbyid (R.id.btn_next); buttonpre = (Button) Findviewbyid (r.id.btn_pre); Buttonnext.setonclicklistener (this); Buttonpre.setonclicklistener (this); New Loadthread (Urls[0], Handler, ImageView). Start (); @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.btn_next:i++;new loadthread (urls[i%4], handler, ImageView). Start (); Toast.maketext (Mainactivity.this, i%4+ "", Toast.length_short). Show (); Break;case r.id.btn_pre:if (i>0) {i--;} else{i=3;} New Loadthread (urls[i%4], Handler, ImageView). Start (); Toast.maketext (Mainactivity.this, i%4+ "", Toast.length_short). Show (); break;}}}


Note the logical processing of the picture Subscript!

Run the following example:

SOURCE download

Welcome to my blog and platform


Android Combat Simple Tutorial-the 71st Gun (asynchronous network download network image and image gallery production)

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.