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

后端 未结 7 2068
暖寄归人
暖寄归人 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:50

    We can manage the height and width of an image from xml code and draw circle/oval from java code like

        
    

    for oval view

    ImageView img1 = (ImageView) findViewById(R.id.imageView1);
    Bitmap bm = BitmapFactory.decodeResource(getResources(),
            R.drawable.user_image);
    Bitmap conv_bm = getRoundedBitmap(bm);
    img1.setImageBitmap(conv_bm);
    
    
    public static Bitmap getRoundedBitmap(Bitmap bitmap)
    {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(output);
    
        final int color = Color.RED;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        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(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    
        bitmap.recycle();
    
        return output;
      }
    
     }
    

提交回复
热议问题