Crop Image as circle in Android

前端 未结 6 887
不思量自难忘°
不思量自难忘° 2020-12-16 02:30

Does anyone know how to crop an image\\bitmap to a circle? I can not find any solution, sorry ..

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 03:14

    For having rounded corners for ImageView, convert your image into bitmap and then try following code :

    private Bitmap getRoundedCroppedBitmap(Bitmap bitmap) {
        int widthLight = bitmap.getWidth();
        int heightLight = bitmap.getHeight();
        
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Config.ARGB_8888);
            
        Canvas canvas = new Canvas(output);
        Paint paintColor = new Paint();
        paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);
            
        RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
            
        canvas.drawRoundRect(rectF, widthLight / 2, heightLight / 2, paintColor);
            
        Paint paintImage = new Paint();
        paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
        canvas.drawBitmap(bitmap, 0, 0, paintImage);
            
        return output;
    }
    

提交回复
热议问题