glide scaletype 无效_android 自定义圆角ImageView,后设置scaleType="centerCrop"无效?

设置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);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值