UIL开源框架的使用

来源:互联网 发布:怎么做个快3软件 编辑:程序博客网 时间:2024/05/16 09:54

安卓中用于图片加载的开源框架。

基本配置

在项目的Application中:

public class MyApplication extends Application {@TargetApi(Build.VERSION_CODES.GINGERBREAD)@SuppressWarnings("unused")@Overridepublic void onCreate() {if (Config.DEVELOPER_MODE&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());}super.onCreate();initImageLoader(getApplicationContext());}public static void initImageLoader(Context context) {// 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.ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove for release app.build();// Initialize ImageLoader with configuration.ImageLoader.getInstance().init(config);}
其中onCreate()方法中的Config如下:

public static class Config {public static final boolean DEVELOPER_MODE = false;}

使用:

使用是很简单的:首先创建一个options:
// 使用DisplayImageOptions.Builder()创建DisplayImageOptionsDisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub)// 设置图片下载期间显示的图片.showImageForEmptyUri(R.drawable.ic_empty)// 设置图片Uri为空或是错误的时候显示的图片.showImageOnFail(R.drawable.ic_error)// 设置图片加载或解码过程中发生错误显示的图片.cacheInMemory(true)// 设置下载的图片是否缓存在内存中.cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中.displayer(new RoundedBitmapDisplayer(20))// 设置成圆角图片
<span style="white-space:pre">.bitmapConfig(Bitmap.Config.RGB_565)<span style="white-space:pre"></span> </span>       <span style="white-space:pre">//设置图片的解码类型</span>.build();// 创建配置过得DisplayImageOption对象
其次用:
ImageLoader imageLoader = ImageLoader.getInstance();/*** 显示图片。可根据需要调用displayImage的重载方法。一般是不需要第四个参数监听器的。* 参数1:图片url* 参数2:显示图片的控件* 参数3:显示图片的设置* 参数4:监听器*/imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener);
这样便可加载图片。而且对于listview等,会自动的进行滑动不加载,停止加载。

其他使用:

圆角图片:

RoundedBitmapDisplayer.roundCorners(bitmap, iv, 60)
它返回的是一个bitmap,所以还需要用iv.setImageBitmap进行设置。

淡入效果:

FadeInBitmapDisplayer.animate(iv, 10000);

清除缓存:

imageLoader.clearMemoryCache();<span style="white-space:pre"></span>// 清除内存缓存imageLoader.clearDiscCache();<span style="white-space:pre"></span>// 清除SD卡中的缓存




0 0