使用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);
}
}
}
Universal-Image-Loader的一个BUG
发表于:2017-01-09
作者:网络转载
来源:
- 周排行
- 月排行
-   高质量的缺陷分析:让自己少写 bug
-   缺陷是什么?
-   从“扁鹊三兄弟”谈缺陷预防
-   如何编写更佳的bug report
-   软件缺陷的描述
-   怎么用Leangoo管理Bug
-   面对Bug的正确姿势
-   实例!软件缺陷数据度量和分析
-   缺陷是什么?
-   高质量的缺陷分析:让自己少写 bug
-   史上最臭名昭著五大软件Bug
-   多种缺陷管理软件简介
-   游戏测试中缺陷的分类
-   消灭Bug秘籍:如何处理大型软件中的错...