Crop square image to circle - Programmatically

前端 未结 7 1429
余生分开走
余生分开走 2020-12-01 01:14

i was searching for past one day and i was not successful .

i get the image from API , and i download it to a bitmap file using the following code .

         


        
7条回答
  •  死守一世寂寞
    2020-12-01 01:51

    use the function below to draw a circle on bitmap and then set the circled bitmap to imageView

     public static Bitmap getClip(Bitmap bitmap) {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            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);
            canvas.drawCircle(bitmap.getWidth() / 2f, bitmap.getHeight() / 2f,
                bitmap.getWidth() / 2f, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }
    

    Note: Dividing numbers must be float

提交回复
热议问题