Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CenterCrop is not working in universal image loader : Android

i am currently using Universal Image Loader 1.9.3 and initialize it as,

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(100)).cacheOnDisc().build();
    ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(Register.this).defaultDisplayImageOptions(defaultOptions).memoryCache(new WeakMemoryCache());

    ImageLoaderConfiguration config = builder.build();
    imageLoader2 = ImageLoader.getInstance();
    imageLoader2.init(config);

Here i have used RoundedBitmapDisplayer because i want image as round shape and i have set the property of image view in xml file as android:scaleType="centerCrop", so it must have result as center crop image but it didn't give center crop image.. images are stretched even gave center crop....

like image 448
Mayur R. Amipara Avatar asked Oct 31 '22 08:10

Mayur R. Amipara


2 Answers

Yeah, it is mentioned that it always keep the aspect ratio, where changing scaletype property on xml wont work... use a coded crop instead

public static Bitmap toCropcenterfitoriginal(Bitmap srcBmp) {
    Bitmap dstBmp = ThumbnailUtils.extractThumbnail(srcBmp,
            srcBmp.getWidth() / 2, srcBmp.getWidth() / 3);
    ;

    return dstBmp;
}
like image 157
Sheychan Avatar answered Nov 12 '22 10:11

Sheychan


you can change the RoundedDrawable in the RoundedBitmapDisplayer with:

public static class RoundedDrawable extends Drawable {

        protected final float cornerRadius;
        protected final int margin;

        protected  RectF mRect = new RectF(),
                mBitmapRect;
        protected final BitmapShader bitmapShader;
        protected final Paint paint;
        protected Bitmap mBitmap;

        public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
            this.cornerRadius = cornerRadius;
            this.margin = margin;
            mBitmap = bitmap;

            bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mBitmapRect = new RectF(margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);

            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(bitmapShader);
        }

        @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);
            int width = bounds.right - bounds.left;
            int height = bounds.bottom - bounds.top;

            float scale = width * 1.0f / mBitmap.getWidth();
            // 如果根据宽度缩放后,高度小于targetHeight
            if (scale * mBitmap.getHeight() < height) {
                scale = height * 1.0f / mBitmap.getHeight();
            }
            int outWidth = Math.round(scale * mBitmap.getWidth());
            int outHeight = Math.round(scale * mBitmap.getHeight());

            shaderMatrix.postScale(scale, scale);

            int left = 0;
            int top = 0;
            if (outWidth == width) {
                top = (outHeight - height) * -1 / 2;
            }
            else {
                left = (outWidth - width) * -1 / 2;
            }

            shaderMatrix.postTranslate(left, top);
            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);
        }
    }

then you can find your pic show in CenterCrop and rounded corner.

you can also check my github for detail: https://github.com/417704684/RoundCornerDrawable

like image 23
Jimmy Ma Avatar answered Nov 12 '22 11:11

Jimmy Ma