Is there a way to round corners in the image view?

Asked 1 years ago, Updated 1 years ago, 73 views

On Android, image views are usually squares. I want to make a square with rounded corners in the image view, is it possible?

android layout imageview rounded-corners

2022-09-22 22:30

1 Answers

If you apply the code below, you can make the corners of the image round as you want.

http://www.ruibm.com/?p=184

It's not my code, but I referred to it here. You can make the corners as round as you want by adjusting the roundPx value.

package com.company.app.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class ImageHelper {
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), 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(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
}

I hope it helps.


2022-09-22 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.