0

I am setting rounded image in imageView. Now, I want to set border for rounded image view.

If it is simple rectangle image then we can do it by setting background color for that image view and setting padding. So it will look like it has a border.

But How I can set border for rounded image view ?

My code to create rounded image for square

    public static Bitmap getRoundededImage(Bitmap bitmap, int diameter) {

    Bitmap output = null;

    try {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        int rectX = (bitmap.getWidth() - diameter) / 2;
        int rectY = (bitmap.getHeight() - diameter) / 2;

        final Rect rect = new Rect(rectX, rectY, rectX + diameter, rectY
                + diameter);

        final RectF rectF = new RectF(rect);
        final float roundPx = diameter;

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

        // draw border
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) 20);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }

    return output;
}

I tried to search and found this Question. I tried this solution but it didn't work. When I applied this solution nothing changed in image.

Please help me to resolve this or guide me in a right path.

Community
  • 1
  • 1
keen
  • 3,001
  • 4
  • 34
  • 59

1 Answers1

0

From your code I am observing that, you are drawing some bitmap over view, then simply you calculate your boundary co-ordinate , and draw path along the boundary, by the using canvas.drawPath(), its might me help you.

Please go through this then you can get some ideas.

Draw in Canvas by finger, Android http://www.java2s.com/Code/Android/2D-Graphics/Drawpath.htm

This is one more example over rounded corner images http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

This is example of drawing Rounded corner over some ractangle

canvas.drawRoundRect(new RectF(0, 100, 100, 200), 6, 6, paint); 

There are lots of api for getting boundary corner , jsut you need only some calculation according to your images,

Hope this information will help you.

Community
  • 1
  • 1
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85