Rounded corners for an Imageview in android

北城余情 提交于 2019-12-03 16:12:19

you can make the image's left bottom and right bottom corner rounded,like this:

code:

public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final RectF rectF = new RectF(0, 0, w, h);

    canvas.drawRoundRect(rectF, radius, radius, paint);

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

    /**
     * here to define your corners, this is for left bottom and right bottom corners
     */
    final Rect clipRect = new Rect(0, 0, w, h - radius);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    canvas.drawRect(clipRect, paint);

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

    bitmap.recycle();

    return output;
}

this method can give you a image with left bottom and right bottom corner rounded.

Madhuri

For having rounded corners for imageview, convert your image into bitmap and then try following code :

public static Bitmap roundCorner(Bitmap src, float round) 
{
    // image size
    int width = src.getWidth();
    int height = src.getHeight();

    // create bitmap output
    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    // set canvas for painting
    Canvas canvas = new Canvas(result);
    canvas.drawARGB(0, 0, 0, 0);

    // config paint
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);

    // config rectangle for embedding
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    // draw rect to canvas
    canvas.drawRoundRect(rectF, round, round, paint);

    // create Xfer mode
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    // draw source image to canvas
    canvas.drawBitmap(src, rect, rect, paint);

    // return final image
     return result;
}

your linear layout is rounded corner and there is no doubt about that but your image is not. Here in the screen-shot the image is overlapping the linearlayout in the bottom. Try adding some padding to the linear layout(android:padding="20dp") . This should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!