Android Universal Image Loader是当前使用比较多的图片加载工具类,可以非常好的“一站式”解决图片下载、压缩、存储、缓存、显示等问题。并且提供 自定义显示图片的接口类BitmapDisplayer,可以自定义图片展示样式。
不过,如果你要展示圆角图片,会发现图片的scaleType失效了,图片拉伸失真。
原来,圆角图片RoundedBitmapDisplayer只能支持centerInside的scaleType,并且该工具类的作者不打算继续维护了。
我自定义了一个BitmapDisplayer来模拟centerCrop的展示效果。
思路很简单,在原始图片的基础上,截取图片中间的部分,创建为新的bitmap,而后在新的bitmap上画圆角,然后传递给需要展示的imageview。
主要代码如下
public class RoundedCenterBitmapDisplayer implements BitmapDisplayer {
protected final int cornerRadius; protected final int margin; protected final float targetWidthHeightRatio; public RoundedCenterBitmapDisplayer(int cornerRadiusPixels, float targetWidthHeightRatio) { this(cornerRadiusPixels, 0, targetWidthHeightRatio); } public RoundedCenterBitmapDisplayer(int cornerRadiusPixels, int marginPixels, float targetWidthHeightRatio) { this.cornerRadius = cornerRadiusPixels; this.margin = marginPixels; this.targetWidthHeightRatio = targetWidthHeightRatio; } @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 RoundedCenterDrawable(bitmap, cornerRadius, margin, targetWidthHeightRatio)); } public static class RoundedCenterDrawable extends Drawable { protected final float cornerRadius; protected final int margin; protected final float targetWidthHeightRatio; protected final RectF mRect = new RectF(), mBitmapRect; protected final BitmapShader bitmapShader; protected final Paint paint; public RoundedCenterDrawable(Bitmap bitmap, int cornerRadius, int margin, float targetWidthHeightRatio) { this.cornerRadius = cornerRadius; this.margin = margin; this.targetWidthHeightRatio = targetWidthHeightRatio; float bWidth = (float)bitmap.getWidth(); float bHeight =(float) bitmap.getHeight(); float ratio = bWidth / bHeight; Bitmap targetBitmap = null; if (ratio > targetWidthHeightRatio) { int height = bitmap.getHeight(); int width = (int) (height * targetWidthHeightRatio); int x = (bitmap.getWidth() - width) / 2; int y = 0; targetBitmap = bitmap.createBitmap(bitmap, x, y, width, height); } else { int width = bitmap.getWidth(); int height = (int) (width / targetWidthHeightRatio); int x = 0; int y = (bitmap.getHeight() - height) / 2; targetBitmap = bitmap.createBitmap(bitmap, x, y, width, height); } bitmapShader = new BitmapShader(targetBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mBitmapRect = new RectF(margin, margin, targetBitmap.getWidth() - margin, targetBitmap.getHeight() - margin); paint = new Paint(); paint.setAntiAlias(true); paint.setShader(bitmapShader); paint.setFilterBitmap(true); paint.setDither(true); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin); // Resize the original bitmap to fit the new bound Matrix shaderMatrix = new Matrix(); shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL); bitmapShader.setLocalMatrix(shaderMatrix); } @Override public void draw(Canvas canvas) { canvas.drawRoundRect(mRect, 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); } } }
其中targetWidthHeightRatio是展示图片的长宽比,通过这个长宽比,来控制图片的展示区域。以上。
源码地址: 点击打开链接