3

I am making an application in which I want to round my ImageView by its corners. But it's not getting rounded from its corner, I have used the following code:

<?xml version="1.0" encoding="utf-8"?>

 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="8dp"
    android:shape="rectangle" >

<solid android:color="#6BC1F8" />

<corners
    android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="5dp" />

</shape>

for rounding the corners of image..,but I am getting like this image : enter image description here

and I want to do like below:

enter image description here

I am not sure where I am doing wrong, please guide me :)

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
User11
  • 453
  • 5
  • 21

4 Answers4

2

Use Shape in android to make the rounder corners

create the xml file named it as roundcorner.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#33DDFF" />
        <corners android:radius="4dp" />
    </shape>

In your ImageButton add this attribute android:background="@drawable/roundcorner"

and use android:src to set the image for the imagebutton

OR

Use image view set android:src = "your image" , android:background= "your shape "and android:clickable="true"

Lal
  • 14,726
  • 4
  • 45
  • 70
1

There is a pretty nice way to create rounded ImageView programmatically.You can give it a try,

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;
    }
}

Also you can refer the sample app created by Romain Guy, a Google engineer

https://docs.google.com/a/impressol.com/file/d/0B3dxhm5xm1sia2NfM3VKTXNjUnc/edit?pli=1

references,

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

How to make an ImageView with rounded corners?

Community
  • 1
  • 1
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
1

Try this

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

    <gradient
        android:angle="270"
        android:endColor="#cccccc"
        android:startColor="#cccccc" />

    <corners android:radius="9dp" />

    <stroke
        android:width="1px"
        android:color="#000000" />


</shape> 

Using this as android:background="@drawable/custom for your imageview, you will get the following output.

Screenshot of output

EDIT : Increase the value of android:radius if you want more rounded corner. And if you don't want black border then remove the <stroke ..... />

Hope this is helpful.

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
-1

You can't just add property as I know. You can do that only for shape. But you can do something like that: Add two Imageviews. First image with transparent field inside with rounded corners. And second your image put under first image.

thealeksandr
  • 1,686
  • 12
  • 17