Opening--Introduction Picasso
(What is Picasso?) Picasso:a powerfull image Downloading and Caching Library for Android, which is the network picture download and caching framework for the Android platform.
(How is Picasso used?) ) Frame! Now that the cows are able to write this frame, the natural use of fluency. Don't worry, it's simple, but it takes a bit of effort to dig into the source code.
(Why does the Picasso framework appear?) In Android development, it is often necessary to obtain images from the remote server side; We do this with HttpURLConnection (java.net.HttpURLConnection) and Asynctask (Android.os.AsyncTask), but there are other things to consider: Picture caching and download management (??). , so it's wise to encapsulate it as a tool class, or use a third-party framework (Picasso, for example).
Warm-up exercise--httpurlconnection and asynctask realize network picture download
Before using the Picasso framework, Let's start with a warm-up exercise: Use HttpURLConnection (java.net.HttpUrlConnection) and Asynctask (android.os.AsyncTask) to load Web images.
Activity_main.xml file, the vertical layout of the LinearLayout two controls: ImageView and Button;button's Click events trigger a network picture request and load memory (ie: Display picture).
<LinearLayoutxmlns: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=". Mainactivity " > <ImageViewAndroid:id= "@+id/iv_image"Android:layout_width= "200DP"Android:layout_height= "200DP" /> <ButtonAndroid:onclick= "LoadImage"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Loading ..." /></LinearLayout>
Androidmanifest.xml file, add user rights for network requests.
<android:name= "Android.permission.INTERNET"/>
Mainactivity.java Aspects:
PackageCom.fenix.picassodemo;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.view.View;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateImageView Miview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {Miview=(ImageView) Findviewbyid (r.id.iv_image); } Private classImagedownloadtaskextendsAsynctask<string, Void, bitmap>{ImageView IV; PublicImagedownloadtask (ImageView iv) { This. IV =IV; } @Overrideprotected voidOnPreExecute () {Super. OnPreExecute (); } @OverrideprotectedBitmap doinbackground (String ... imgaddress) {URL Imgurl=NULL; HttpURLConnection Connection=NULL; InputStream InputStream=NULL; Bitmap Bitmap=NULL; Try{Imgurl=NewURL (Imgaddress[0]);//Convert a network picture path to a URL instanceConnection=(HttpURLConnection) imgurl.openconnection (); Connection.setdoinput (true); Connection.connect (); InputStream=Connection.getinputstream (); Bitmap=Bitmapfactory.decodestream (InputStream); } Catch(Exception e) {e.printstacktrace (); } finally { if(InputStream! =NULL) { Try{inputstream.close (); } Catch(IOException e) {e.printstacktrace (); } } } returnbitmap; } @Overrideprotected voidOnPostExecute (Bitmap result) {Super. OnPostExecute (Result); Iv.setimagebitmap (result); } } Public voidLoadImage (View v) {NewImagedownloadtask (Miview). Execute ("Http://pic32.nipic.com/20130829/12906030_124355855000_2.png"); }}
Click the button in the layout file to execute LoadImage (), which is the network picture request, and the network request should not be written in the main thread, so you need to use the Asynctask tool (encapsulating handler and thread); That is, execute imagedownloadtask.
Image resources from: Www.baidu.com's picture library (right-click to copy Image address).
Asynctask combined with httpurlconnection, it is true that Web images can be requested, but the problem is: Android applications (from the network picture angle), also such as: Manage image download requests, reasonable use of memory, etc. need to consider (Google engineers- The ultimate experience of the spokesperson). That's why there's so much like a Picasso picture request frame! Of course there are many open source frameworks that need to be familiar (and later).
Picasso framework--first experience
Android Open source Framework--picasso