3

I have a square bitmap being displayed underneath a semi-transparent circle. The user can touch and drag the bitmap to position it. I want to be able to crop what ever part of the bitmap is under the circle. How can I do this?

Vega
  • 27,856
  • 27
  • 95
  • 103
sara roberts
  • 63
  • 1
  • 1
  • 11
  • http://stackoverflow.com/questions/11932805/cropping-circular-area-from-bitmap-in-android – zgc7009 Nov 10 '14 at 18:55
  • Does this answer your question? [How to crop circular area from bitmap in Android](https://stackoverflow.com/questions/11932805/how-to-crop-circular-area-from-bitmap-in-android) – Vega Oct 09 '20 at 02:43

4 Answers4

13

have a look at RoundedBitmapDrawable in the support library

all you have to do is give it the bitmap and the corner radius

RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create(getResources(),bitmap);
img.setCornerRadius(radius);

imageView.setImageDrawable(img);
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • 3
    You can `img.setCircular(true)` instead of `img.setCornerRadius(radius)` if you want it to be perfect circular. – Roel Sep 16 '15 at 13:55
  • Best answer out there for pure code solution, no XML modification or custom shapes necessary – Themos Apr 20 '20 at 10:14
8

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 image
Bitmap imageBitmap=BitmapFactory.decodeResource(getResources(),  R.drawable.large_icon);
RoundedBitmapDrawable roundedBitmapDrawable=
  RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
roundedBitmapDrawable.setCornerRadius(50.0f);
roundedBitmapDrawable.setAntiAlias(true);
profilePic.setImageDrawable(roundedBitmapDrawable);
sree_sg
  • 719
  • 9
  • 11
  • Instead of using a constant corner radius you should use: `roundedBitmapDrawable.setCornerRadius(Math.min(roundedBitmapDrawable.getMinimumWidth(), roundedBitmapDrawable.getMinimumHeight())/2.0f);` – crubio Sep 02 '15 at 15:52
  • 1
    You can use setCircular(true) if you want it to be circular. – Roel Sep 16 '15 at 13:54
1

You can use the power of PorterDuff to get your bitmap in any shape or path...

Here is an example:

public static Bitmap getCircular(Bitmap bm, int cornerRadiusPx) {
    int w = bm.getWidth();
    int h = bm.getHeight();

    int radius = (w < h) ? w : h;
    w = radius;
    h = radius;

    Bitmap bmOut = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmOut);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(0xff424242);

    Rect rect = new Rect(0, 0, w, h);
    RectF rectF = new RectF(rect);

    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(rectF.left + (rectF.width()/2), rectF.top + (rectF.height()/2), radius / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bm, rect, rect, paint);

    return bmOut;
}
semsamot
  • 805
  • 9
  • 11
0

Here is a link to a sample project. It has a transparent square over an image. You can pinch zoom or drag the bottom image and can corp it.

https://github.com/tcking/ImageCroppingView.

The square is made by using canvas. you can change it to any shape as u desired by changing canvas. Hop it helps you.

Nitesh
  • 3,868
  • 1
  • 20
  • 26