Skip to content Skip to sidebar Skip to footer

Add Border To Getroundedcornerbitmap Android

In getRoundedCornerBitmap(Context context, Bitmap input, int pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR ) method below i attem

Solution 1:

Try below code :-

intw= bitmap.getWidth();                                          
inth= bitmap.getHeight();                                         

intradius= Math.min(h / 2, w / 2);                                
Bitmapoutput= Bitmap.createBitmap(w + 8, h + 8, Config.ARGB_8888);

Paintp=newPaint();                                              
p.setAntiAlias(true);                                               

Canvasc=newCanvas(output);                                      
c.drawARGB(0, 0, 0, 0);                                             
p.setStyle(Style.FILL);                                             

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

p.setXfermode(newPorterDuffXfermode(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;

publicclassGraphicsUtil
{

    /*
     * Draw image in circular shape Note: change the pixel size if you want
     * image small or large
     */public Bitmap getCircleBitmap(Bitmap bitmap)
    {
        Bitmap output;
        Canvascanvas=null;
        finalintcolor=0xffff0000;
        finalPaintpaint=newPaint();
        Rectrect=null;
        if (bitmap.getHeight() > 501)
        {
            output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
            canvas = newCanvas(output);
            rect = newRect(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 = newCanvas(output);
            rect = newRect(0, 0, 500, 500);
        }
        finalRectFrectF=newRectF(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(newPorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    publicstaticintcalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
    {
        // Raw height and width of imagefinalintheight= options.outHeight;
        finalintwidth= options.outWidth;
        intinSampleSize=1;

        if (height > reqHeight || width > reqWidth)
        {
            // Calculate ratios of height and width to requested height and// widthfinalintheightRatio= Math.round((float) height / (float) reqHeight);
            finalintwidthRatio= 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;
    }

    publicstatic Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight)
    {
        // First decode with inJustDecodeBounds=true to check dimensionsfinal BitmapFactory.Optionsoptions=newBitmapFactory.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);
    }

}

Post a Comment for "Add Border To Getroundedcornerbitmap Android"