Android-Universal-Image-Loader icon indicating copy to clipboard operation
Android-Universal-Image-Loader copied to clipboard

CENTER_CROP doesn't work when I use a Displayer

Open liyiheng opened this issue 9 years ago • 5 comments

both RoundedBitmapDisplayer and CircleBitmapDisplayer

maybe centercrop works , but the Bitmap was warped

liyiheng avatar May 07 '16 02:05 liyiheng

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 avatar Jul 12 '16 03:07 Fishbonelsy

@Fishbonelsy thks

liyiheng avatar Jul 12 '16 04:07 liyiheng

how to get "mScaleType "-->if (mScaleType == ImageView.ScaleType.CENTER_CROP)?

miafu avatar Apr 18 '17 03:04 miafu

@miafu

    public RoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels, ImageView.ScaleType scaleType) {
        this(cornerRadiusPixels, 0);
        mScaleType = scaleType;
    }

Add additional parameters^_^

Fishbonelsy avatar Jul 13 '17 02:07 Fishbonelsy

@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);
}

ltpanda avatar Dec 21 '17 04:12 ltpanda