继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

图片处理框架limageloader的基本配置

当年话下
关注TA
已关注
手记 124
粉丝 14
获赞 50

需要下载universal-image-loader-1.9.2-with-sources.jar
/**
 * @author ben 2014-7-4
 */
public class ImageUtil {

    private static boolean isInit = false;
    static String filename = Environment.getExternalStorageDirectory() + "/foshanimage/";
    // 常规设置
    private static DisplayImageOptions normalOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher) // 设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.drawable.ic_launcher) // 设置图片加载/解码过程中错误时候显示的图片
            // .showImageOnLoading(R.drawable.ic_launcher) // 设置图片在下载期间显示的图片
            // .showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
            // .showImageOnFail(R.drawable.ic_launcher) // 设置图片加载/解码过程中错误时候显示的图片
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            // .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//

            // .delayBeforeLoading(int delayInMillis)//int
            // delayInMillis为你设置的下载前的延迟时间
            // 设置图片加入缓存前,对bitmap进行设置
            // .preProcessor(BitmapProcessor preProcessor)
            // .resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位
            // .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少
            // .displayer(new FadeInBitmapDisplayer(100))// 是否图片加载好后渐入的动画时间
            .build();// 构建完成

    public static Bitmap loadImage(Context context, String uri) {
        initImageLoader(context);

        return ImageLoader.getInstance().loadImageSync(uri, normalOptions);
    }

    public static void displayImage(Context context, String uri, ImageView imageView) {
        initImageLoader(context);
        ImageLoader.getInstance().displayImage(uri, imageView, normalOptions);
    }

    public static void displayImage(Context context, String uri, ImageView imageView, DisplayImageOptions options) {
        initImageLoader(context);
        ImageLoader.getInstance().displayImage(uri, imageView, options);
    }

    public static void displayImage(Context context, String uri, ImageView imageView, ImageLoadingListener listener) {
        initImageLoader(context);
        ImageLoader.getInstance().displayImage(uri, imageView, normalOptions, listener);
    }

    private static synchronized void initImageLoader(Context context) {
        if (isInit) {
            // Logger.i("已经....初始化imgloader");
            return;
        }
        Context appContext = context.getApplicationContext();
        // File cacheDir = StorageUtils.getOwnCacheDirectory(appContext,
        // "imageloader/Cache");
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(appContext)
        // .memoryCacheExtraOptions(1280, 720)
                .memoryCacheExtraOptions(224, 245).denyCacheImageMultipleSizesInMemory()

                // max width, max height,即保存的每个缓存文件的最大长宽
                // .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75,
                // null) // Can slow ImageLoader, use it carefully (Better don't
                // use it)/设置缓存的详细信息,最好不要设置这个
                .threadPoolSize(2)
                // 线程池内加载的数量

                .threadPriority(Thread.NORM_PRIORITY - 3).denyCacheImageMultipleSizesInMemory()
                // .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 *
                // 1024)) // You can pass your own memory cache
                // implementation/你可以通过自己的内存缓存实现
                .memoryCacheSize(200 * 1024 * 1024) // 内存缓存大小
                .diskCacheSize(200 * 1024 * 1024) // 文件缓存大小
                // .discCacheFileNameGenerator(new
                // Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
                .tasksProcessingOrder(QueueProcessingType.FIFO).diskCacheFileCount(100) // 缓存的文件数量

                .diskCache(new UnlimitedDiscCache(new File(filename)))// 自定义缓存路径
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple()).imageDownloader(new BaseImageDownloader(appContext, 10 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout
                                                                                                                                                            // (30 s)超时时间
                .writeDebugLogs() // Remove for release app
                .build();// 开始构建
        ImageLoader.getInstance().init(config);
        isInit = true;
    }

}

原文链接:http://www.apkbus.com/blog-508687-62960.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP