设置DisplayImageOptions属性的时候用这个BitmapDisplayer即可实现CenterCrop效果。
public class CenterCropRoundedBitmapDisplayer implements BitmapDisplayer {
protected final int cornerRadius;
protected final int margin;
public CenterCropRoundedBitmapDisplayer(int cornerRadiusPixels) {
this(cornerRadiusPixels, 0);
}
public CenterCropRoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) {
this.cornerRadius = cornerRadiusPixels;
this.margin = marginPixels;
}
public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
if(!(imageAware instanceof ImageViewAware)) {
throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
} else {
imageAware.setImageDrawable(new CenterCropRoundedBitmapDisplayer.RoundedDrawable(bitmap, this.cornerRadius, this.margin,imageAware.getWidth(),imageAware.getHeight()));
}
}
public static class RoundedDrawable extends Drawable {
protected final float cornerRadius;
protected final int margin;
protected final RectF mRect = new RectF();
protected final RectF mBitmapRect;
protected final BitmapShader bitmapShader;
protected final Paint paint;
float scale;
float dx = 0, dy = 0;
public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin,int vwidth,int vheight) {
this.cornerRadius = (float)cornerRadius;
this.margin = margin;
this.bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
this.mBitmapRect = new RectF((float)margin, (float)margin, (float)(bitmap.getWidth() - margin), (float)(bitmap.getHeight() - margin));
this.paint = new Paint();
this.paint.setAntiAlias(true);
this.paint.setShader(this.bitmapShader);
this.paint.setFilterBitmap(true);
this.paint.setDither(true);
/*设置centercrop属性*/
if (bitmap.getWidth() * vheight > vwidth * bitmap.getHeight()) {
scale = (float) vheight / (float) bitmap.getHeight();
dx = (vwidth - bitmap.getWidth() * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) bitmap.getWidth();
dy = (vheight - bitmap.getHeight() * scale) * 0.5f;
}
}
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
this.mRect.set((float)this.margin, (float)this.margin, (float)(bounds.width() - this.margin), (float)(bounds.height() - this.margin));
Matrix shaderMatrix = new Matrix();
shaderMatrix.setRectToRect(this.mBitmapRect, this.mRect, Matrix.ScaleToFit.FILL);
shaderMatrix.setScale(scale, scale);
shaderMatrix.postTranslate(Math.round(dx), Math.round(dy));
this.bitmapShader.setLocalMatrix(shaderMatrix);
}
public void draw(Canvas canvas) {
canvas.drawRoundRect(this.mRect, this.cornerRadius, this.cornerRadius, this.paint);
}
public int getOpacity() {
return -3;
}
public void setAlpha(int alpha) {
this.paint.setAlpha(alpha);
}
public void setColorFilter(ColorFilter cf) {
this.paint.setColorFilter(cf);
}
}
}