Mid-Autumn festival to work overtime yesterday, there is a demand used in an ImageView. ScaleType. CENTER_CROP effect, see below:

SetScaleType does not work because the bitmap draws rounded corners. It is the first time for me to encounter this situation. I know the problem, and of course it is a Google call, but the Internet is basically Glide base and CENTER_CROP solutions, which are not applicable to me. So finally can only according to their own ideas to do, in this record, if you have a better method, welcome to correct:

  1. Crop the original bitmap directly to CENTER_CROP

  2. Rounded the clipped bitmap

  3. The ImageView uses the final bitmap directly

code

I paste the clipping method below:

public static Bitmap cropBitmap(Bitmap src, int tagW, int tagH) {
    final int srcW = src.getWidth();
    final int srcH = src.getHeight();
    final float xScale = (float) tagW / srcW;
    final float yScale = (float) tagH / srcH;
    final float scale = Math.max(xScale, yScale);

    final float scaleW = scale * srcW;
    final float scaleH = scale * srcH;
    final float left = (tagW - scaleW) / 2;
    final float top = (tagH - scaleH) / 2;

    final RectF tagRect = new RectF(left, top, left + scaleW, top + scaleH); // New bitmap clipping area
    final Bitmap dst = Bitmap.createBitmap(tagW, tagH, src.getConfig());
    final Canvas canvas = new Canvas(dst);
    canvas.drawBitmap(src, null, tagRect, null);
    return dst;
}
Copy the code

Fillet processing methods are also provided as follows:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float[] rids) {
    try {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth()
                , bitmap.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0.0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0.0, bitmap.getWidth(), bitmap.getHeight()));
        paint.setAntiAlias(true);
        canvas.drawARGB(0.0.0.0);
        paint.setColor(Color.BLACK);
        Path path = new Path();
        path.addRoundRect(rectF, rids, Path.Direction.CW); // rids[] the radians of the four angles
        canvas.drawPath(path, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        final Rect src = new Rect(0.0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (OutOfMemoryError outOfMemoryError){
        outOfMemoryError.printStackTrace();
        return bitmap;
    } catch (Exception e) {
        TLog.e(TAG, TLog.USR, e);
        returnbitmap; }}Copy the code

Here’s the code to use:

private void setRoundBitmap(Bitmap bitmap) {
    Bitmap scaleBitmap = null;
    try {
        // First crop to the target size
        scaleBitmap = NaviUtil.crop(bitmap, tagImgW, tagImgH);
        float radius = NaviUtil.dp2px(getContext(), 8);
        // float[] sets 2 to control each arc Angle
        tagBitmap = NaviUtil.getRoundedCornerBitmap(scaleBitmap,
                new float[] {0.0 f.0.0 f.0.0 f.0.0 f
                        , radius, radius, radius, radius});
    } catch (OutOfMemoryError outOfMemoryError) {
        return;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        recycleBitmap(bitmap);
        recycleBitmap(scaleBitmap);
    }
    if (null != mImgView) {
        mImgView.setImageBitmap(tagBitmap);
    }
}
Copy the code

So that’s it. I hope it’s helpful