Android-Universal-Image-Loader
Android-Universal-Image-Loader copied to clipboard
CENTER_CROP doesn't work when I use a Displayer
both RoundedBitmapDisplayer and CircleBitmapDisplayer
maybe centercrop works , but the Bitmap was warped
You can create a custom class implements BitmapDisplayer.And add
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
// Resize the original bitmap to fit the new bound
Matrix shaderMatrix = new Matrix();
if (mScaleType == ImageView.ScaleType.CENTER_CROP) {
mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);
ScaleState scaleState = getScaleMultiple(mBitmapRect, mRect);
float scale = Math.max(scaleState.widthSaleMultiple, scaleState.heightSaleMultiple);
shaderMatrix.setScale(scale, scale);
if (scaleState.widthSaleMultiple < scaleState.heightSaleMultiple) {
shaderMatrix.postTranslate(0, (mRect.height() - mBitmapRect.height() * scale) / 2);
} else {
shaderMatrix.postTranslate((mRect.width() - mBitmapRect.width() * scale) / 2, 0);
}
bitmapShader.setLocalMatrix(shaderMatrix);
}
static private ScaleState getScaleMultiple(RectF bitmapBounds, RectF viewBounds) {
ScaleState scaleState = new ScaleState();
scaleState.widthSaleMultiple = viewBounds.width() / bitmapBounds.width();
scaleState.heightSaleMultiple = viewBounds.height() / bitmapBounds.height();
return scaleState;
}
static class ScaleState {
float widthSaleMultiple = 1.0f;
float heightSaleMultiple = 1.0f;
}
Others just copy RoundedBitmapDisplayer.
@Fishbonelsy thks
how to get "mScaleType "-->if (mScaleType == ImageView.ScaleType.CENTER_CROP)?
@miafu
public RoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels, ImageView.ScaleType scaleType) {
this(cornerRadiusPixels, 0);
mScaleType = scaleType;
}
Add additional parameters^_^
@Fishbonelsy thanks for your code, and I make a little change to fix translate direction issue
if (scaleState.widthSaleMultiple < scaleState.heightSaleMultiple) {
shaderMatrix.postTranslate((mRect.width() - mBitmapRect.width() * scale) / 2, 0);
} else {
shaderMatrix.postTranslate(0, (mRect.height() - mBitmapRect.height() * scale) / 2);
}