How to make an ImageView with rounded corners?

前端 未结 30 2599
天涯浪人
天涯浪人 2020-11-21 05:39

In Android, an ImageView is a rectangle by default. How can I make it a rounded rectangle (clip off all 4 corners of my Bitmap to be rounded rectangles) in the ImageView?

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 06:17

    if your image is on internet the best way is using glide and RoundedBitmapDrawableFactory (from API 21 - but available in support library) like so:

     Glide.with(ctx).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
        @Override
        protected void setResource(Bitmap res) {
            RoundedBitmapDrawable bitmapDrawable =
                 RoundedBitmapDrawableFactory.create(ctx.getResources(), res);
            bitmapDrawable.setCircular(true);//comment this line and uncomment the next line if you dont want it fully cricular
            //circularBitmapDrawable.setCornerRadius(cornerRadius);
            imageView.setImageDrawable(bitmapDrawable);
        }
    });
    

提交回复
热议问题