Anadroid 之使用ImageLoader 默认配置和手动配置参数

来源:互联网 发布:凸优化 中文版 pdf 编辑:程序博客网 时间:2024/05/16 13:01

1。容易忘记的东西,那就是添加网络权限(打头描述)

     <uses-permission android:name="android.permission.INTERNET" />      <!-- Include next permission if you want to allow UIL to cache images on SD card -->      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

2。在自己的MyApplication中配置即可

public class MyApplication  extends Application{    @Override    public void onCreate() {        // TODO Auto-generated method stub        super.onCreate();         //创建默认的ImageLoader配置参数  //        ImageLoaderConfiguration configuration = ImageLoaderConfiguration  //                .createDefault(this);          //Initialize ImageLoader with configuration.//        ImageLoader.getInstance().init(configuration);         //手动配置        initImageLoader(getApplicationContext());    }    public void initImageLoader(Context context) {        DisplayImageOptions options = new DisplayImageOptions.Builder()                .showImageOnLoading(R.drawable.ic_launcher) // 设置图片在下载期间显示的图片                .showImageForEmptyUri(R.drawable.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片                .showImageOnFail(R.drawable.ic_launcher) // //设置图片加载/解码过程中错误时候显示的图片                .resetViewBeforeLoading(true) // 设置图片在下载前是否重置,复位                .delayBeforeLoading(200) // 设置图片下载前的延迟                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中                .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中                // .preProcessor(...) //设置图片加入缓存前,对bitmap进行设置                // .postProcessor(...) //设置显示前的图片,显示后这个图片一直保留在缓存中                // .extraForDownloader(...)                .considerExifParams(false) // 设置额外的内容给ImageDownloader                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // 设置图片以如何的编码方式显示                .bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型                // .decodingOptions(...) //设置图片的解码配置                .displayer(new RoundedBitmapDisplayer(3)) // default                // .handler(new Handler()) // default                .build();        // This configuration tuning is custom. You can tune every option, you        // may tune some of them,        // or you can create default configuration by        // ImageLoaderConfiguration.createDefault(this);        // method.        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "zoomImage/imgcache");        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(                context).threadPoolSize(3)                .threadPriority(Thread.NORM_PRIORITY - 2)                .denyCacheImageMultipleSizesInMemory()                .diskCacheFileNameGenerator(new Md5FileNameGenerator())                .tasksProcessingOrder(QueueProcessingType.LIFO)                .writeDebugLogs()                // Remove for release app                .defaultDisplayImageOptions(options)                .diskCache(new UnlimitedDiscCache(cacheDir)).build();        // Initialize ImageLoader with configuration.        ImageLoader.getInstance().init(config);    }}

2。使用,在baseActivity初始化即可

protected ImageLoader imageLoader = ImageLoader.getInstance();

注:在使用ImageLoader的时候,尽量不要使用默认配置,手动配置,可以按照自己的习惯就缓存的图片保存在自己命名的文件夹下面,这样,可以随意改动。

0 0
原创粉丝点击