Crop Image as circle in Android

前端 未结 6 903
不思量自难忘°
不思量自难忘° 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 02:55

    Try with below code :

    public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
      // TODO Auto-generated method stub
      int targetWidth = 50;
      int targetHeight = 50;
      Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                                targetHeight,Bitmap.Config.ARGB_8888);
    
                    Canvas canvas = new Canvas(targetBitmap);
      Path path = new Path();
      path.addCircle(((float) targetWidth - 1) / 2,
      ((float) targetHeight - 1) / 2,
      (Math.min(((float) targetWidth), 
                    ((float) targetHeight)) / 2),
              Path.Direction.CCW);
    
                    canvas.clipPath(path);
      Bitmap sourceBitmap = scaleBitmapImage;
      canvas.drawBitmap(sourceBitmap, 
                                    new Rect(0, 0, sourceBitmap.getWidth(),
        sourceBitmap.getHeight()), 
                                    new Rect(0, 0, targetWidth,
        targetHeight), null);
      return targetBitmap;
     }
    

提交回复
热议问题