博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Universal-Image-Loader的一个BUG
阅读量:4313 次
发布时间:2019-06-06

本文共 3540 字,大约阅读时间需要 11 分钟。

使用UIL的内置圆角图片的功能时,发现一个BUG,就是它会拉伸图片,造成图片失真。费了一下午的功夫,重写了RoundedBitmapDisplayer,总算解决这个问题。

代码如下:

public class RoundedBitmapDisplayer implements BitmapDisplayer {    protected final int cornerRadius;    protected final int margin;    public RoundedBitmapDisplayer(int cornerRadiusPixels) {        this(cornerRadiusPixels, 0);    }    public RoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) {        this.cornerRadius = cornerRadiusPixels;        this.margin = marginPixels;    }    @Override    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {        if (!(imageAware instanceof ImageViewAware)) {            throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");        }        imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, margin));    }    public static class RoundedDrawable extends Drawable {        protected final float cornerRadius;        protected final int margin;        protected final RectF mBorderRect = new RectF(), mBitmapRect;        protected final BitmapShader mBitmapShader;        protected final Paint paint;        private final Matrix mShaderMatrix = new Matrix();        private float scale = 1.0f, dx, dy;         private int mBitmapWidth, mBitmapHeight;        private int mBorderWidth = 0;        public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {            this.cornerRadius = cornerRadius;            this.margin = margin;            mBitmapWidth = bitmap.getWidth();            mBitmapHeight = bitmap.getHeight();            mBitmapShader = 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(mBitmapShader);            paint.setFilterBitmap(true);            paint.setDither(true);        }        @Override        protected void onBoundsChange(Rect bounds) {            super.onBoundsChange(bounds);            mBorderRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);                            mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);            mShaderMatrix.reset();            dx = 0;            dy = 0;            if (mBitmapWidth * mBorderRect.height() > mBorderRect.width() * mBitmapHeight) {                scale = mBorderRect.height() / (float) mBitmapHeight;                dx = (mBorderRect.width() - mBitmapWidth * scale) * 0.5f;            } else {                scale = mBorderRect.width() / (float) mBitmapWidth;                dy = (mBorderRect.height() - mBitmapHeight * scale) * 0.5f;            }            mShaderMatrix.setScale(scale, scale);            mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth / 2, (int) (dy + 0.5f) + mBorderWidth / 2);            // 设置shader            mBitmapShader.setLocalMatrix(mShaderMatrix);        }        @Override        public void draw(Canvas canvas) {            canvas.drawRoundRect(mBorderRect, cornerRadius, cornerRadius, paint);        }        @Override        public int getOpacity() {            return PixelFormat.TRANSLUCENT;        }        @Override        public void setAlpha(int alpha) {            paint.setAlpha(alpha);        }        @Override        public void setColorFilter(ColorFilter cf) {            paint.setColorFilter(cf);        }    }}

 

转载于:https://www.cnblogs.com/wan1976/p/5008751.html

你可能感兴趣的文章
mysql -- 查询并插入
查看>>
windows下Yarn安装与使用
查看>>
排序算法——快速排序
查看>>
禁用表单元素 && 禁止选中
查看>>
YII2 电话号码的验证规则
查看>>
SPFA模板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板板...
查看>>
java注解
查看>>
[翻译] Fast Image Cache
查看>>
iOS文件处理类
查看>>
Hadoop集群datanode磁盘不均衡的解决方案
查看>>
Vue 开源项目库汇总(转)
查看>>
403 ,502 到正确的nginx 配置
查看>>
[Nescafé41]异化多肽(多项式求逆元)
查看>>
网络中常见的ping命令协议
查看>>
不用position,让div垂直居中
查看>>
sql 语句 查找
查看>>
谓词和运算符
查看>>
WIN10打开资源管理器显示该文件没有与之关联的程序来执行该操作.请安装应用,请在“默认应用设置”..关联 —— 解决方案...
查看>>
HttpWebRequest中的SendChunked
查看>>
linux文件存储方式
查看>>