0

In getRoundedCornerBitmap(Context context, Bitmap input, int pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR ) method below i attempted to display rounded image but when i try to add a border around the circle nothing is displayed. How can i add border to this rounded method.

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR  ) {

        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final float densityMultiplier = context.getResources().getDisplayMetrics().density;

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, w, h);
        final RectF rectF = new RectF(rect);

        //make sure that our rounded corner is scaled appropriately
        final float roundPx = pixels*densityMultiplier;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);


        //draw rectangles over the corners we want to be square
        if (squareTL ){
            canvas.drawRect(0, 0, w/2, h/2, paint);
        }
        if (squareTR ){
            canvas.drawRect(w/2, 0, w, h/2, paint);
        }
        if (squareBL ){
            canvas.drawRect(0, h/2, w/2, h, paint);
        }
        if (squareBR ){
            canvas.drawRect(w/2, h/2, w, h, paint);
        }

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(input, 0,0, paint);

        return output;
    }

Update now the circle is cut from left and bottom. How can i get a full rounded image enter image description here

Dimitri
  • 1,924
  • 9
  • 43
  • 65

1 Answers1

3

Try below code :-

int w = bitmap.getWidth();                                          
int h = bitmap.getHeight();                                         

int radius = Math.min(h / 2, w / 2);                                
Bitmap output = Bitmap.createBitmap(w + 8, h + 8, Config.ARGB_8888);

Paint p = new Paint();                                              
p.setAntiAlias(true);                                               

Canvas c = new Canvas(output);                                      
c.drawARGB(0, 0, 0, 0);                                             
p.setStyle(Style.FILL);                                             

c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);                  

p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));                 

c.drawBitmap(bitmap, 4, 4, p);                                      
p.setXfermode(null);                                                
p.setStyle(Style.STROKE);                                           
p.setColor(Color.WHITE);                                            
p.setStrokeWidth(3);                                                
c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);                  

return output;  

see below link for more info:-

How to add a shadow and a border on circular imageView android?

Adding a round frame circle on rounded bitmap

or use below code :-

imageViewUser.setImageBitmap(new GraphicsUtil().getCircleBitmap(GraphicsUtil.decodeSampledBitmapFromResource(filePath,120, 120)));  // filepath is your image path

GraphicsUtil.java

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

public class GraphicsUtil
{

    /*
     * Draw image in circular shape Note: change the pixel size if you want
     * image small or large
     */
    public Bitmap getCircleBitmap(Bitmap bitmap)
    {
        Bitmap output;
        Canvas canvas = null;
        final int color = 0xffff0000;
        final Paint paint = new Paint();
        Rect rect = null;
        if (bitmap.getHeight() > 501)
        {
            output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(output);
            rect = new Rect(0, 0, 500, 500);
        }
        else
        {
            //System.out.println("output            else =======");
            bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
            output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(output);
            rect = new Rect(0, 0, 500, 500);
        }
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setFilterBitmap(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) 1);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
    {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth)
        {
            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight)
    {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

}
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113