ImageLoader加载画圆与缓存

  • 继承Application
public class MyLoader extends Application{

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        File cacheDir = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            cacheDir = new File(Environment.getExternalStorageDirectory(),
                    "imageloader/Cache");
        }

        @SuppressWarnings("deprecation")
        ImageLoaderConfiguration fig = new ImageLoaderConfiguration.Builder(
                this)
                .memoryCacheExtraOptions(480, 800)
                // maxwidth, max height,即保存的每个缓存文件的最大长宽
                .threadPoolSize(3)
                // 线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
                // You can pass your own memory cache
                // implementation/你可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)
                .discCacheSize(50 * 1024 * 1024)
                // .discCacheFileNameGenerator(new
                // Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCacheFileCount(100)
                // 缓存的文件数量
                .discCache(new LimitedAgeDiskCache(cacheDir, 70))
                // 自定义缓存路径
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .imageDownloader(
                        new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
                // connectTimeout (5s),readTimeout(30s)超时时间
                .writeDebugLogs() // Remove for releaseapp
                .build();// 开始构建
        ImageLoader.getInstance().init(fig);


    }
}
  • 工具类
public class ImageUtil {

    public static DisplayImageOptions getOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher) // 加载图片时的图片
                .showImageForEmptyUri(R.drawable.ic_launcher) // 没有图片资源时的默认图片
                .showImageOnFail(R.drawable.ic_launcher) // 加载失败时的图片
                .cacheInMemory(true) // 启用内存缓存
                .cacheOnDisk(true) // 启用外存缓存
                .considerExifParams(true) // 启用EXIF和JPEG图像格式
                .displayer(new Displayer(0)) // 设置圆形
                // .displayer(new RoundedBitmapDisplayer(20)) 设置显示风格这里是圆角矩形
                .build();

        return options;

    }

    // 设置圆形
    static class Displayer extends RoundedBitmapDisplayer {

        public Displayer(int cornerRadiusPixels) {
            super(cornerRadiusPixels);
        }

        // 显示位图
        @Override
        public void display(Bitmap bitmap, ImageAware imageAware,
                LoadedFrom loadedFrom) {
            imageAware.setImageDrawable(new CircleDrawable(bitmap, margin));
        }

        class CircleDrawable extends Drawable {
            private final int margin;
            private final RectF mRect = new RectF();
            private final BitmapShader bitmapShader;
            private final Paint paint;
            private RectF mBitmapRect;

            public CircleDrawable(Bitmap bitmap, int margin) {
                this.margin = 0;
                // 创建着色器
                bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                        Shader.TileMode.CLAMP);
                mBitmapRect = new RectF(margin, margin, bitmap.getWidth()
                        - margin, bitmap.getHeight() - margin);
                // 设置画笔
                paint = new Paint();
                paint.setAntiAlias(true);
                paint.setShader(bitmapShader);
            }

            // 画圆,覆盖原来的位图
            @Override
            protected void onBoundsChange(Rect bounds) {
                super.onBoundsChange(bounds);
                mRect.set(margin, margin, bounds.width() - margin,
                        bounds.height() - margin);

                // 调整位图,设置该矩阵,转换映射源矩形和目的矩形
                Matrix shaderMatrix = new Matrix();
                shaderMatrix.setRectToRect(mBitmapRect, mRect,
                        Matrix.ScaleToFit.FILL);
                // 设置着色器矩阵
                bitmapShader.setLocalMatrix(shaderMatrix);
            }

            // 画出其边界(通过设置的setBounds)
            @Override
            public void draw(Canvas canvas) {
                canvas.drawRoundRect(mRect, mRect.width() / 2,
                        mRect.height() / 2, paint);
            }

            /**
             * 返回此绘制对象的不透明度/透明度 ,返回的值是抽象的格式常数的PixelFormat之一: 未知,半透明,透明或不透明
             */
            @Override
            public int getOpacity() {
                // 半透明
                return PixelFormat.TRANSLUCENT;
            }

            // 设置透明度
            @Override
            public void setAlpha(int alpha) {
                paint.setAlpha(alpha);
            }

            // 彩色滤光片(通过设置setColorFilter)
            @Override
            public void setColorFilter(ColorFilter cf) {
                paint.setColorFilter(cf);
            }
        }
    }
}
  • 设置图片与清除
//加载图片
ImageLoader.getInstance().displayImage("URL", holder.iv, ImageUtil.getOptions());
// 清除本地缓存
ImageLoader.getInstance().clearDiskCache();  
// 清除内存缓存 
ImageLoader.getInstance().clearMemoryCache(); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值