When we load a large number of network pictures, when the method is not appropriate, the phenomenon of memory overflow often occurs, causing the program to crash, and some problems such as slow loading of pictures. ImageLoader image loading core class, internally uses thread pool to load images , and flexibly changes the basic configuration of ImageLoader, including the maximum number of threads, cache mode, image display options, etc.; image asynchronous loading cache mechanism, including memory cache (soft reference) and local cache; Implement monitoring and event processing for the loading process; be able to configure the display options for loading images, including image rounding processing and loading completion display animations, which can basically solve the above problems!
@Override
public void onCreate() {
super.onCreate();
// Configuration without cache:
ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(getApplicationContext())
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.enableLogging() // Not necessary in common
.build();
// Configuration with cache:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory ()
.cacheOnDisc()
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(defaultOptions)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.enableLogging()
.build();
ImageLoader.getInstance().init(config);
}
}
Once configured, we can use it anywhere we want. The specific use is as follows:
1. ImageLoader.getInstance().displayImage(url, ImageView);
2. DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub) //Load and start the default
image.showImageForEmptyUri(R.drawable.ic_empty) //If the url is empty , the picture in the drawable will be displayed.showImageOnFail ( R.drawable.ic_error) //There is a problem loading the picture, this picture will be displayed.cacheInMemory () //cache use.cacheOnDisc() // cache use
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_565) // To prevent memory overflow, use this for too many pictures
.displayer(new RoundedBitmapDisplayer(5)) //The image is displayed with rounded corners, the value is
an integer.build();
ImageLoader.getInstance().displayImage(url, imageView,options);
3.ImageLoader.getInstance().loadImage(url,new SimpleImageLoadingListener(){
public void onLoadingComplete(String imageUri, android.view.View view, android.graphics.Bitmap loadedImage) {
imageView.setImageBitmap(loadedImage); //imageview控件对象
};
public void onLoadingFailed(String imageUri, android.view.View view, com.nostra13.universalimageloader.core.assist.FailReason failReason) {
Toast.makeText(ShowOneImage.this,"加载失败", Toast.LENGTH_LONG).show();
};
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
Reference document: http://my.oschina.net/u/1858156/blog/338854