본문 바로가기
Android/Code Piece

비트맵 이미지를 원형으로 크롭하기

by featherwing 2020. 4. 11.
반응형

StackOverFlow에서 발견한  좋은 코드를 일부 수정하였습니다.

Width, Heigt 를 넣으면 사이즈 조절을 하도록 추가하였습니다.

원본 사이즈 그대로 원모양으로 크롭된 이미지를 출력하기 위해서는

 

Width와 Height에 0, 0을 넣어주면 됩니다.  

 

 

 public static Bitmap getBitmapCircleCrop(Bitmap bitmap, int Width, int Height) {
     
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.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(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        
        Bitmap CroppedBitmap = output;
        //width, Height에 0,0을 넣으면 원본 사이즈 그대로 출력
        if(Width!=0 && Height!=0)  CroppedBitmap = Bitmap.createScaledBitmap(output, Width, Height, false);
        return CroppedBitmap;
       
    }

 

Reference : How to crop circular area from bitmap in Android

반응형

댓글