5

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....

Mayur R. Amipara
  • 1,223
  • 1
  • 11
  • 32

3 Answers3

1

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;
}
Sheychan
  • 2,415
  • 14
  • 32
1

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

Jimmy Ma
  • 21
  • 5
0

This seems to be an open issue in Universal Image Loader. The work around that i can suggest for this is, load the image bitmap and then centercrop and corner round the bitmap as needed. Here is the code sample.

 BaseActivity.imageLoader.loadImage(mUrl, mOptions, new ImageLoadingListener() 
 {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason)
            {
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                if (loadedImage != null)
                {
                    Bitmap croppedBitmap = ThumbnailUtils.extractThumbnail(loadedImage, HIQUtil.dpToPixel(getActivity(), 295), HIQUtil.dpToPixel(getActivity(), 211));
                    Bitmap roundedCropped = getRoundedCornerBitmap(croppedBitmap, 5);
                    imageView.setImageBitmap(roundedCropped);
                }
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });

To get rounded corner bitmap, you can us this method:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
            .getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

Make sure to set

adjustViewBounds ="true"

in your imageview

Sudhasri
  • 1,264
  • 16
  • 19