How to make an image fit into a circular frame in android

后端 未结 7 2067
暖寄归人
暖寄归人 2020-12-01 00:27

I have a ListView in which there is an ImageView, the image in the ImageView gets loaded dynamically after its fetched from the server

7条回答
  •  盖世英雄少女心
    2020-12-01 00:52

     public static Bitmap getCircleBitmap(Bitmap bitmap) {
            final Bitmap circuleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getWidth(), Bitmap.Config.ARGB_8888);
            final Canvas canvas = new Canvas(circuleBitmap);
    
            final int color = Color.RED;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
            final RectF rectF = new RectF(rect);
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawOval(rectF, paint);
    
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            bitmap.recycle();
    
            return circuleBitmap;
        }
    

提交回复
热议问题