-8

How do I get this code to work? I want an image to be turned into a crycle and i found this code on this site but it doesnt say how to use it... anyone got any ideas?

Cropping circular area from bitmap in Android

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;
}
Community
  • 1
  • 1
James
  • 17
  • 6

2 Answers2

0

Lets say you have a drawable you want to use?

the image object - can be ImageView:

Bitmap bitImg = BitmapFactory.decodeResource(getResources(),
    R.drawable.YOUR_DRAWABLE_NAME);
image.setImageBitmap(getCroppedBitmap(bitImg));
Sean
  • 5,176
  • 2
  • 34
  • 50
0

You convert your drawable into a Bitmap, pass it in and set it to a ImageView (I assume this is what you want):

Bitmap myDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.myDrawable);

myImageView.setImageBitmap(getCroppedBitmap(myDrawable));
Ahmad
  • 69,608
  • 17
  • 111
  • 137