views:

1483

answers:

4

In android, ImageView is a rectangle by default. Is it possible to make it a rounded rectangle (clipped off all 4 corners of my Bitmap to be rounded rectangles) in the ImageView? If yes, can you please tell me now can I do that?

A: 

Just set imageView.layer.cornerRadius = X;.

Steven Canfield
imageView.layer does not exist as far as I can see in Android.
Janusz
A: 

AFAIK, for Android there isn't any way you can give the rounded rect to image view.

Btw you can provide a image background with the corners non-transparent and rest transparent.

Karan
But what about Steven Canfield said in above answer?
michael
I think that is for iPhone not for Android.
Karan
+4  A: 

You should extend ImageView and draw your own rounded rectangle.

If you want a frame around the image you could also superimpose the rounded frame on top of the image view in the layout.

[edit]Superimpose the frame on to op the original image, by using a FrameLayout for example. The first element of the FrameLayout will be the image you want to diplay rounded. Then add another ImageView with the frame. The second ImageView will be displayed on top of the original ImageView and thus Android will draw it's contents above the orignal ImageView.

MrSnowflake
Thank you. But there is only setDrawable method for ImageView, how can I setDrawable of the ImageView to the content of my image and then superimpose a rounded frame on top of the ImageView?
michael
I'm sorry, I was being unclear. I meant superimposing in the layout: thus (ie) use a `FrameLayout` put an `ImageView` in it and add another `ImageView` with the rounded frame. That way the first `ImageView` will display your selected picture and the second `ImageFrame` will display the rounded frame.
MrSnowflake
+6  A: 

This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images.

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

This isn't my code, but I've used it and it's works wonderfully. I used it as a helper within an ImageHelper class and extended it just a bit to pass in the amount of feathering I need for a given image.

Final code looks like this:

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

Hope this helps someone!

George Walters II
This should be picked as the answer. Thank you George!
DroidIn.net