Asynchronous image loading using ImageLoader

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!

1. Download the latest package from the official website , add universal-image-loader-1.8.4.jar to the libs of your project , pay attention to importing the project, click on your project, right-click - select build path - configure build path - — add jars , select the package in libs under your project .
 
2. Configure the Manifest file, because you want to make network requests and local caching , add network requests and permissions to access external storage
 
<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
 
3.
p ublic class MyApplication extends Application {

    @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);

}

}

配置完成后,我们就可以在任何想用它的地方使用了。具体使用如下:

1.   ImageLoader.getInstance().displayImage(url, ImageView); 

2.   DisplayImageOptions  options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_stub)             //加载开始默认的图片      
            .showImageForEmptyUri(R.drawable.ic_empty)     //url爲空會显示drawable里面的图片
            .showImageOnFail(R.drawable.ic_error)          //加载图片出现问题,会显示此图片
            .cacheInMemory()                               //缓存用
            .cacheOnDisc()                                 //缓存用

        .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 

 

        .bitmapConfig(Bitmap.Config.RGB_565)           // 防止内存溢出的,图片太多用这个

        .displayer(new RoundedBitmapDisplayer(5))      //图片圆角显示,值为整数
            .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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326677078&siteId=291194637