Asynchronous loading of network images in Android

Source: Internet
Author: User

 

Recently, I am trying to use android to develop a mobile client of Sina. I want to learn about Android Application Development and take notes.

This section describes some problems in the use of listview.

I retrieve data from the Sina server, including text and images, and load images through the image URL. However, in the android SDK, listview does not support loading network images through URLs by default, you need to override the method in the SDK's dictation class. We know that listview is generally used in combination with listactivity. during use, we need to input simpleadatper object to bind the data. by reading the simpleadapter source code, we found that, as long as we rewrite the implementation of the seven methods setviewimage, we can achieve the goal.

At first, I changed it like this:

Public void setviewimage (imageview V, string value) {<br/> bitmap BMP = bitmapfactory. decodestream (new URL (value ). openstream); <br/> v. setimagebitmap (BMP); <br/>} 

 

However, during the operation, the interface is easily killed and cannot be moved. The user experience is not good.

The speed at which the mobile client accesses the network is obviously not comparable to that of the PC. Even a small image file may take a long time, what's more, a list contains many images to be downloaded. It's not surprising that they are not dead.

So I thought about downloading images asynchronously, and then rewrite the setviewimage () function:

Public void setviewimage (imageview V, string value) {<br/> New imagedownloadtask(cmd.exe cute (value, V ); <br/>}< br/> private class imagedownloadtask extends asynctask <object, object, bitmap >{< br/> private imageview = NULL; <br/> @ override <br/> protected bitmap doinbackground (object... params) {<br/> // todo auto-generated method stub <br/> bitmap BMP = NULL; <br/> imageview = (imageview) Params [1]; <br/> try {<br/> BMP = bitmapfactory. decodestream (new URL (string) Params [0]). openstream (); <br/>} catch (malformedurlexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> return BMP; <br/>}</P> <p> protected void onpostexecute (Bitmap result) {<br/> imageview. setimagebitmap (result); <br/>}< br/>} 

Now, the user can see the displayed text information first. The image can be downloaded in multiple threads while the user browses the text, without affecting the user experience.

:

 

 

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.