Android Listview Soft reference softreference loading picture asynchronously

Source: Internet
Author: User

First of all, the Android system loads a lot of picture system memory overflow 3 solution:

(1) When loading images from the network or locally, only the thumbnails are loaded. This method does not occupy a lot of memory, but its fatal disadvantage is that because the load is the thumbnail, so the picture distortion is more serious, for the image quality requirements of high application, you can use the following method

/**

* Follow the picture path to load

* @param path to the path image resource

* @param scalsize Zoom in multiples

* @return

*/

public static Bitmap Loadresbitmap (String path, int scalsize) {

Bitmaofactory.options Options = new Bitmapfactory.options ();

Options.injustdecodebounds = false;

Options.insamplesize = scalsize;

Bitmap bmp = Bitmapfactory.decodefile (path, options);

Reuturn bmp;

}

(2) The use of Java Soft reference, image caching, will often need to load the picture, stored in the cache, to avoid repeated loading.

(3) Timely destruction of bitmap objects that are no longer used.

if (bitmap! = null && b!itmap.isrecycled ()) {

Bitmap.recycle ();

bitmap = null; Recycle () is a lengthy process, set to NULL, and then the last call to System.GC (), the effect is much better.

System.GC ();

}

Well, the above is the topic of knowledge supplement, the following is the main content of this blog. Hashmap<string, softreference<drawable>> Imagecache about softreference This class how much know some mechanisms, just in the use of the stage (this is not enough).

Mechanism: In short, she will help us to manage memory, prevent memory overflow, the other point is the equivalent of map, temporarily cache some pictures drawable let us directly reference, good to solve the oom exception.

Implementing Code Snippets:

[Java]
Package Com.Tianyou.Mobile.Common;
 
import java.io.IOException;
import Java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import Java.net.URL;
import Java.util.HashMap;
 
import com. Tianyou.Mobile.Util.MyUtil;
 
import Android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import Android.os.Handler;
import android.os.Message;
 
/***
* Implementation of loading picture cache asynchronously
 * 
* @author Jia
 * 
 */ 
Public class Asyncimageloader {
//Soft reference
private hashmap<string, softreference<drawable>> Imagecache;
 
Public Asyncimageloader () {
Imagecache = new hashmap<string, softreference<drawable>> ();
    } 
 
    /***
* Download Image
     * 
* @param imageUrl
* Image Address
* @param imagecallback
* Callback Interface
* @return
     */ 
Public drawable loaddrawable (Final String imageUrl,
final Imagecallback Imagecallback) { 
if (Imagecache.containskey (IMAGEURL)) {
softreference<drawable> softreference = Imagecache.get (IMAGEURL);
drawable drawable = Softreference.get ();
if (drawable! = null) {
return drawable;
            } 
        } 
final Handler Handler = new Handler () {
Public void handlemessage (Message message) {
imagecallback.imageloaded ((drawable) message.obj, IMAGEURL);
            } 
        };
//Open thread download picture
New Thread () {
@Override
Public void Run () {
drawable drawable = Loadimagefromurl (IMAGEURL);
//Save the downloaded picture to the cache
Imagecache.put (IMAGEURL, New softreference<drawable> (drawable));
Message message = Handler.obtainmessage (0, drawable);
handler.sendmessage (message);
            } 
}.start ();
return null;
    } 
 
    /***
* Download the image according to the URL (here to determine, first go to the local SD, no then download according to the URL, and then return the drawable)
     * 
* @param url
* @return
     */ 
Public static drawable Loadimagefromurl (String imageURL) {
 
Bitmap Bitmap = Myutil.getbitmap (ImageURL, +);
drawable drawable = new bitmapdrawable (bitmap);
return drawable;
 
    } 
 
//Callback interface
Public interface Imagecallback {
Public void imageloaded (drawable imagedrawable, String imageUrl);
    } 
 

above this kind of stone is used to see, most of them are realized, I now want to speak a little, but also puzzled me a little bit (callback interface application) I am not good, so stuck here,
This method is called in the GetView method in the ListView:

Code snippet:

[Java]
Public drawable getdrawable (Asyncimageloader asyncimageloader,
String imageUrl, final ImageView ImageView) { 
drawable drawable = asyncimageloader.loaddrawable (IMAGEURL,
New Imagecallback () {
@Override
Public void imageloaded (drawable imagedrawable,
String ImageUrl) { 
if (imagedrawable! = null)
imageview.setimagedrawable (imagedrawable);
Else
Imageview.setimageresource (r.drawable.u6_normal);
                    } 
                });
return drawable;
    } 
This method functions: To obtain the software in the image, in fact, in the first time we entered the ListView, the soft application is not working, just to download the image saved to the SD card and soft reference. This is the way to do these operations, for the parameter ImageView is to set the acquired drawable in.
performed smoothly: first we call the Loaddrawable method, and then find the soft reference in which there is no drawable, no then thread download, After the download OK will execute handlemessage in imagecallback.imageloaded ((drawable) message.obj, IMAGEURL), and then execute we have implemented this interface.

To execute a code snippet in Getiview:


[Java]
drawable drawable = getdrawable (Asyncimageloader, image_l,
Holder.iv_image);

if (drawable!=null)
Holder.iv_image.setImageDrawable (drawable);
When we slipped, and then on the slide when the soft application played an effect, efficiency quickly oh, can and Sina hired the United States, hehe, jokes, to optimize the place still many.

PS: Second, refer to http://www.2cto.com/kf/201303/194546.html

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.