Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Getting roundedCorner for an Image in Android

    • 0
    • 3
    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 515
    Comment on it

    Sometimes we need to display images with rounded corners in our several Applications, and this can be done very easily by writing some lines of code.

    To get roundedCorner for an Image use the following code:

    /**
     * This method is used to make rounded corners for image bitmap
     * 
     * @param bitmap is the image bitmap for which you want to get rounded corners
     * @return rounded corner bitmap
     */
    public static Bitmap getRoundedCornerImage(Bitmap bitmap) 
    {
        try
        {
            Bitmap output = null;
            if(bitmap != null)
            {
                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 = 8;
    
                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;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            System.gc();
        }
        return null;
    }

    Hope this will help you.Good Luck :)

 2 Comment(s)

  • Hi Arpit, you can use the below line to decode the file:

    BitmapFactory.Options o2 = new BitmapFactory.Options();

    BitmapFactory.decodeStream(new FileInputStream(f), null, o2)

    I had used like this and it was working fine for me.

  • It is not helping me I'm using it as shown below :-

    Bitmap bitmap;
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions);
    profilePicImageView.setImageBitmap(getRoundedCornerImage(bitmap));
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: