1. 程式人生 > >ImageLoader 快取圖片 並配置全域性

ImageLoader 快取圖片 並配置全域性

//介面卡中的程式碼

 //配置色彩模式為RGB_4444
        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.ARGB_4444)
                .build();
        ImageLoader.getInstance().displayImage(list.get(i).getImageUrl(),hodler.imageView,displayImageOptions);

//MyApp中的程式碼 ( 注意繼承的是Application )

public class MyApp extends Application{
    //ImageLoader來載入圖片,並全域性配置,配置本地快取路徑在SDCard下imagefile目錄
    @Override
    public void onCreate() {
        super.onCreate();
        File file = new File(Environment.getExternalStorageDirectory(),"imagefile");
        ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
                .diskCache(new UnlimitedDiskCache(file))
                .build();
      ImageLoader.getInstance().init(imageLoaderConfiguration);
    }
}

最後不要忘記在清單檔案中新增name 還有網路請求許可權哦!