1. 程式人生 > >Image-Universal-Loader

Image-Universal-Loader

sfm android app 線程 class pan sof roi disk

ImageLoader簡介和使用方法

1.功能概要     
(1).使用多線程加載圖片
(2).靈活配置ImageLoader的基本參數,包括線程數、緩存方式、圖片顯示選項等;
(3).圖片異步加載緩存機制,包括內存緩存及SDCard緩存;
(4).采用監聽器監聽圖片加載過程及相應事件的處理;
(5).配置加載的圖片顯示選項,比如圖片的圓角處理及漸變動畫。

2.簡單實現

在使用ImageLoader的實例之前,你需要初始化該配置,否則會報初始化錯誤。一般我們直接寫在application中初始化。
       
public class MyApplication extends Application {

    @Override
    public void onCreate() {

        ImageLoaderConfiguration loaderConfiguration = new ImageLoaderConfiguration.Builder(this).build();
//        獲得實例
        ImageLoader imageLoader = ImageLoader.getInstance();
//        初始化
        imageLoader.init(loaderConfiguration);

    }
}
在初始化配置完成後,在清單中寫入你自定義的application,並寫入訪問權限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xlistview">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:name=".Application.MyApplication"
/**代碼中的使用
             * 第一個參數,是圖片的url
             * 第二個參數是imageView控件
             */
ImageLoader.getInstance().displayImage(data_list.get(position).getPic_url(), holder.img);
常用參數
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .threadPoolSize(3)     //設置開啟線程池數
                .memoryCacheSize(10*1024)    //設置內存的緩存大小
                .memoryCacheExtraOptions(480, 800)  //內存緩存圖片的最大寬高 
                .showImageOnLoading(R.mipmap.ic_launcher)   //在圖片沒有加載進來之前顯示的圖片
                .showImageForEmptyUri(R.drawable.ic_launcher)//設置圖片Uri為空或是錯誤的時候顯示的圖片 
                .showImageOnFail(R.drawable.error)  //設置圖片加載/解碼過程中錯誤時候顯示的圖片
                .cacheInMemory(true)//設置下載的圖片是否緩存在內存中
                .cacheOnDisk(true)//設置下載的圖片是否緩存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)//設置圖片的解碼類型/默認是ARGB_8888,使用RGB_565會比使用ARGB_8888少消耗2倍的內存
                .displayer(new RoundedBitmapDisplayer(20))//不推薦用!!!!是否設置為圓角,弧度為多少//他會創建新的ARGB_8888格式的Bitmap對象;  
                .displayer(new FadeInBitmapDisplayer(100))//圖片加載好後漸入的動畫時間,可能會出現閃動
                .diskCacheSize(50 * 1024 * 1024)// 緩沖大小  
                .build();   //構建完成
        //得到ImageLoader對像
        ImageLoader imageLoader = ImageLoader.getInstance();
        //進行初使化
        imageLoader.init(configuration);

ImageLoader簡介和使用方法

1.功能概要     
(1).使用多線程加載圖片
(2).靈活配置ImageLoader的基本參數,包括線程數、緩存方式、圖片顯示選項等;
(3).圖片異步加載緩存機制,包括內存緩存及SDCard緩存;
(4).采用監聽器監聽圖片加載過程及相應事件的處理;
(5).配置加載的圖片顯示選項,比如圖片的圓角處理及漸變動畫。

2.簡單實現

在使用ImageLoader的實例之前,你需要初始化該配置,否則會報初始化錯誤。一般我們直接寫在application中初始化。
       
public class MyApplication extends Application {

    @Override
    public void onCreate() {

        ImageLoaderConfiguration loaderConfiguration = new ImageLoaderConfiguration.Builder(this).build();
//        獲得實例
        ImageLoader imageLoader = ImageLoader.getInstance();
//        初始化
        imageLoader.init(loaderConfiguration);

    }
}
在初始化配置完成後,在清單中寫入你自定義的application,並寫入訪問權限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xlistview">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:name=".Application.MyApplication"
/**代碼中的使用
             * 第一個參數,是圖片的url
             * 第二個參數是imageView控件
             */
ImageLoader.getInstance().displayImage(data_list.get(position).getPic_url(), holder.img);
常用參數
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .threadPoolSize(3)     //設置開啟線程池數
                .memoryCacheSize(10*1024)    //設置內存的緩存大小
                .memoryCacheExtraOptions(480, 800)  //內存緩存圖片的最大寬高 
                .showImageOnLoading(R.mipmap.ic_launcher)   //在圖片沒有加載進來之前顯示的圖片
                .showImageForEmptyUri(R.drawable.ic_launcher)//設置圖片Uri為空或是錯誤的時候顯示的圖片 
                .showImageOnFail(R.drawable.error)  //設置圖片加載/解碼過程中錯誤時候顯示的圖片
                .cacheInMemory(true)//設置下載的圖片是否緩存在內存中
                .cacheOnDisk(true)//設置下載的圖片是否緩存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)//設置圖片的解碼類型/默認是ARGB_8888,使用RGB_565會比使用ARGB_8888少消耗2倍的內存
                .displayer(new RoundedBitmapDisplayer(20))//不推薦用!!!!是否設置為圓角,弧度為多少//他會創建新的ARGB_8888格式的Bitmap對象;  
                .displayer(new FadeInBitmapDisplayer(100))//圖片加載好後漸入的動畫時間,可能會出現閃動
                .diskCacheSize(50 * 1024 * 1024)// 緩沖大小  
                .build();   //構建完成
        //得到ImageLoader對像
        ImageLoader imageLoader = ImageLoader.getInstance();
        //進行初使化
        imageLoader.init(configuration);

Image-Universal-Loader