-1

see the image corners.Please help

enter image description here

i want to round bottom left and top left corners of my image rounded while setting it to image view

My code:

private static Bitmap createRoundedRectBitmap(@NonNull Bitmap bitmap, float topLeftCorner, float topRightCorner, float bottomRightCorner, float bottomLeftCorner) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output);
    final int color = Color.WHITE;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    Path path = new Path();
    float[] radii = new float[]{
            topLeftCorner, bottomLeftCorner,
            topRightCorner, topRightCorner,
            bottomRightCorner, bottomRightCorner,
            bottomLeftCorner, bottomLeftCorner
    };

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    path.addRoundRect(rectF, radii, Path.Direction.CW);
    canvas.drawPath(path, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
Community
  • 1
  • 1
Abdul Basit
  • 29
  • 1
  • 4
  • no matter how such round corners you give to image view, if the image is not rounded it will not show that way, either make rounded images , or crop the corners of the image. – Mohit Feb 15 '18 at 08:03
  • 2
    **`any efforts from your side`** – AskNilesh Feb 15 '18 at 08:04
  • is there any other way like using bit map or like this kind of stuff ? – Abdul Basit Feb 15 '18 at 08:05
  • Be more clear while asking question, you have attached iOS screenshot but asking question in Android, should be clear what you want to do exactly! Also do googling and so some basic R&D before throwing a question – Paresh Mayani Feb 15 '18 at 08:05
  • i m new in android actuall i have a mockup in ios. that's why – Abdul Basit Feb 15 '18 at 08:07

3 Answers3

1

This xml will give you left top and bottom corner rounder.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:right="-20dp">
    <shape android:shape="rectangle" >
        <solid android:color="@android:color/holo_green_dark" />
        <corners android:radius="20dp" />
    </shape>

</item>

<item  android:right="-20dp">
    <shape>
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="3dp"
            android:color="@android:color/holo_red_dark" />
        <corners android:radius="20dp"/>
    </shape>
</item>

</layer-list>

Top and bottom left corner rounded

Modify and create your drawable as your requirement

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
0

i have a used these two functions together :

public Bitmap getRoundedSquareBitmap(Bitmap bitmap) {
        Bitmap output;

        output = Bitmap.createBitmap(bitmap.getHeight(), 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());


        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.WHITE);

        canvas.drawPath(RoundedRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), 5, 5, true), paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        if (output.getWidth() > AppSharedPref.instance.getScreenWidth() * 2)
            output = Bitmap.createScaledBitmap(output, output.getWidth() / 3, output.getWidth() / 3, false);
        else
            output = Bitmap.createScaledBitmap(output, output.getWidth() / 2, output.getWidth() / 2, false);
        return output;
    }

and then used this from another post : https://stackoverflow.com/a/28655800/4336375

public Path RoundedRect(float left, float top, float right, float bottom, float rx, float ry, boolean conformToOriginalPost) {
        Path path = new Path();
        if (rx < 0) rx = 0;
        if (ry < 0) ry = 0;
        float width = right - left;
        float height = bottom - top;
        if (rx > width / 2) rx = width / 2;
        if (ry > height / 2) ry = height / 2;
        float widthMinusCorners = (width - (2 * rx));
        float heightMinusCorners = (height - (2 * ry));

        path.moveTo(right, top + ry);
        path.rQuadTo(0, -ry, -rx, -ry);//top-right corner
        path.rLineTo(-widthMinusCorners, 0);
        path.rQuadTo(-rx, 0, -rx, ry); //top-left corner
        path.rLineTo(0, heightMinusCorners);

        if (conformToOriginalPost) {
            path.rLineTo(0, ry);
            path.rLineTo(width, 0);
            path.rLineTo(0, -ry);
        } else {
            path.rQuadTo(0, ry, rx, ry);//bottom-left corner
            path.rLineTo(widthMinusCorners, 0);
            path.rQuadTo(rx, 0, rx, -ry); //bottom-right corner
        }

        path.rLineTo(0, -heightMinusCorners);

        path.close();//Given close, last lineto can be removed.

        return path;
    }

modify the code to crop your image into round rectangle as per requirement.

Mohit
  • 134
  • 7
0

Can make use of drawable for setting background for image.

drawable for top-left and bottom-left rounded corners.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/darker_gray" />
<corners
    android:bottomLeftRadius="6dp"
    android:bottomRightRadius="0dp"
    android:topLeftRadius="6dp"
    android:topRightRadius="0dp" />

drawable for top-right and bottom-right rounded corners.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/darker_gray" />
<corners
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="6dp"
    android:topLeftRadius="0dp"
    android:topRightRadius="6dp" />

Can change the background for image programatically within recyclerView viewHolder.

Archana
  • 597
  • 4
  • 18