1

I want to crop a picture in current window,maybe circle or rectangle. Here are my code.I first get the whole picture in current window.Then to crop circle or rectangle.

private Bitmap getBitmap()
    {

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap mBackgroundBitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        mRect = ChooseView.cropRect; // the mRect is the crop area
        int width = mRect.width();
        int height = mRect.height();

        Bitmap whiteBgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(whiteBgBitmap);

        Rect dstRect = new Rect(0, 0, width, height);

        if(!TextUtils.isEmpty(CutParam.shape) && CutParam.shape.equals(CutParam.RECTANGLE))
        {

            canvas.drawColor(Color.WHITE);

            canvas.drawBitmap(mBackgroundBitmap, mRect, dstRect, null);
        }
        else
        {
            final Paint paint = new Paint();
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            canvas.drawCircle(mRect.left + mRect.width() / 2, mRect.top + mRect.height() / 2, CutParam.radius, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(mBackgroundBitmap, mRect, dstRect, null);

        }
        return whiteBgBitmap;
    }

The rectangle work will.But when I want to crop a circle,it will be a black image. I am fresh at Android.And I have read Cropping circular area from bitmap in Android .But I can't solve my question.Can you help me? PS:If crop a circle ,the mRect will be a square.

Community
  • 1
  • 1
Egos Zhang
  • 1,334
  • 10
  • 18

1 Answers1

0

Try this one from lvillani on github. It's simple, and gets you out from the muddy world of pictures in android.

fiipi
  • 663
  • 6
  • 20