Android Advanced: ListView performance Optimization Asynchronous load picture makes the sliding effect smooth

Source: Internet
Author: User

ListView is a view that can display a series of items and can be scrolled, each row of item may contain a complex structure, may get some icon information from the network icons, etc., now the network speed to keep the ListView running well scrolling smooth is not done

So this information needs to be loaded asynchronously using multithreading

Classes that implement such a function

[Java]View Plaincopy
  1. Public class Asyncimageloader {
  2. private hashmap<string, softreference<drawable>> Imagecache;
  3. Public Asyncimageloader () {
  4. Imagecache = new hashmap<string, softreference<drawable>> ();
  5. }
  6. Public drawable loaddrawable (final String imageUrl, final imagecallback imagecallback) {
  7. if (Imagecache.containskey (IMAGEURL)) {
  8. softreference<drawable> softreference = Imagecache.get (IMAGEURL);
  9. drawable drawable = Softreference.get ();
  10. if (drawable! = null) {
  11. return drawable;
  12. }
  13. }
  14. final Handler Handler = new Handler () {
  15. @Override
  16. public void handlemessage (Message message) {
  17. Imagecallback.imageloaded ((drawable) message.obj, IMAGEURL);
  18. }
  19. };
  20. New Thread () {
  21. @Override
  22. public Void Run () {
  23. drawable drawable = Loadimagefromurl (IMAGEURL);
  24. Imagecache.put (IMAGEURL, new softreference<drawable> (drawable));
  25. Message message = Handler.obtainmessage (0, drawable);
  26. Handler.sendmessage (message);
  27. }
  28. }.start ();
  29. return null;
  30. }
  31. public static drawable loadimagefromurl (String url) {
  32. // ...  
  33. }
  34. Public interface Imagecallback {
  35. public void imageloaded (drawable imagedrawable, String imageUrl);
  36. }
  37. }

Note that SoftReference is used to cache images, allowing the GC to clean up images in the cache when needed. It works like this:

· Call Loaddrawable (IMAGEURL, Imagecallback), passing in an anonymous implementation of the Imagecallback interface

· If the picture does not exist in the cache, the picture is downloaded from a single thread and passed the Imagecallback callback at the end of the download

· If the picture does exist in the cache, it will return immediately and will not callback Imagecallback

We can then continue to optimize adapter using Viewholder to reduce some of the more time-consuming operations, such as inflate XML and Findviewbyid (), based on the way the 09google i/0 developer Conference mentions

[Java]View Plaincopy
  1. Public class Imageandtextlistadapter extends arrayadapter<imageandtext> {
  2. Private ListView ListView;
  3. private Asyncimageloader Asyncimageloader;
  4. Public imageandtextlistadapter (activity activity, List<imageandtext> imageandtexts, ListView listview) {
  5. super (activity, 0, imageandtexts);
  6. This.listview = ListView;
  7. Asyncimageloader = new Asyncimageloader ();
  8. }
  9. @Override
  10. Public View GetView (int position, View Convertview, ViewGroup parent) {
  11. Activity activity = (activity) getcontext ();
  12. //Inflate the views from XML
  13. View Rowview = Convertview;
  14. Viewcache Viewcache;
  15. if (Rowview = = null) {
  16. Layoutinflater inflater = Activity.getlayoutinflater ();
  17. Rowview = Inflater.inflate (R.layout.image_and_text_row, null);
  18. Viewcache = new Viewcache (Rowview);
  19. Rowview.settag (Viewcache);
  20. } Else {
  21. Viewcache = (Viewcache) rowview.gettag ();
  22. }
  23. ImageAndText ImageAndText = GetItem (position);
  24. //Load the image and set it on the ImageView
  25. String imageUrl = Imageandtext.getimageurl ();
  26. ImageView ImageView = Viewcache.getimageview ();
  27. Imageview.settag (IMAGEURL);
  28. drawable cachedimage = asyncimageloader.loaddrawable (IMAGEURL, new Imagecallback () {
  29. public void imageloaded (drawable imagedrawable, String imageUrl) {
  30. ImageView Imageviewbytag = (ImageView) listview.findviewwithtag (IMAGEURL);
  31. if (imageviewbytag! = null) {
  32. Imageviewbytag.setimagedrawable (imagedrawable);
  33. }
  34. }
  35. });
  36. Imageview.setimagedrawable (Cachedimage);
  37. //Set The text on the TextView
  38. TextView TextView = Viewcache.gettextview ();
  39. Textview.settext (Imageandtext.gettext ());
  40. return rowview;
  41. }
  42. }

Here we do not load after iamge directly set to the corresponding ImageView, but through tag lookup, here we re-use the view here is a ListView reference to find the implementation of the visible callback through tag

[C-sharp]View Plaincopy
    1. ImageView Imageviewbytag = (ImageView) listview.findviewwithtag (IMAGEURL);
    2. if (imageviewbytag! = null) {
    3. Imageviewbytag.setimagedrawable (imagedrawable);
    4. }

This reduces the use of Findviewbyid through Viewcatch.

[C-sharp]View Plaincopy
  1. Public class Viewcache {
  2. private View Baseview;
  3. private TextView TextView;
  4. private ImageView ImageView;
  5. Public Viewcache (View baseview) {
  6. This.baseview = Baseview;
  7. }
  8. Public TextView Gettextview () {
  9. if (TextView = = null) {
  10. TextView = (TextView) Baseview.findviewbyid (R.id.text);
  11. }
  12. return titleview;
  13. }
  14. Public ImageView Getimageview () {
  15. if (ImageView = = null) {
  16. ImageView = (ImageView) Baseview.findviewbyid (r.id.image);
  17. }
  18. return imageView;
  19. }
  20. }

Summary : Here is the main three-point optimization

    • Bunch on a single line load picture
    • reusing a row in a list
    • the View in the cache line

Transferred from: http://blog.csdn.net/wanglong0537/article/details/6334005

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.