How to crop circular area from bitmap in Android How to crop circular area from bitmap in Android android android

How to crop circular area from bitmap in Android


After long brainstorming I have found the solution

public Bitmap getCroppedBitmap(Bitmap bitmap) {    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());    paint.setAntiAlias(true);    canvas.drawARGB(0, 0, 0, 0);    paint.setColor(color);    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,            bitmap.getWidth() / 2, paint);    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    canvas.drawBitmap(bitmap, rect, rect, paint);    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);    //return _bmp;    return output;}


to generate Circle from rectangles

public static Bitmap getCircularBitmap(Bitmap bitmap) {    Bitmap output;    if (bitmap.getWidth() > bitmap.getHeight()) {        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);    } else {        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());    float r = 0;    if (bitmap.getWidth() > bitmap.getHeight()) {        r = bitmap.getHeight() / 2;    } else {        r = bitmap.getWidth() / 2;    }    paint.setAntiAlias(true);    canvas.drawARGB(0, 0, 0, 0);    paint.setColor(color);    canvas.drawCircle(r, r, r, paint);    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    canvas.drawBitmap(bitmap, rect, rect, paint);    return output;}


You Can make your imageview circular using RoundedBitmapDrawable

here is the code for achieving roundedImageview:

ImageView profilePic=(ImageView)findViewById(R.id.user_image);//get bitmap of the imageBitmap imageBitmap=BitmapFactory.decodeResource(getResources(),  R.drawable.large_icon);RoundedBitmapDrawable roundedBitmapDrawable=RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);//setting radiusroundedBitmapDrawable.setCornerRadius(50.0f);roundedBitmapDrawable.setAntiAlias(true);profilePic.setImageDrawable(roundedBitmapDrawable);