How to make an ImageView with rounded corners?

前端 未结 30 2479
天涯浪人
天涯浪人 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条回答
  •  [愿得一人]
    2020-11-21 06:10

    If you are using Glide Library this would be helpful:

    Glide.with(getApplicationContext())
         .load(image_url)
         .asBitmap()
         .centerCrop()
         .into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
              RoundedBitmapDrawable circularBitmapDrawable =
                           RoundedBitmapDrawableFactory.create(getApplicationContext().getResources(), resource);
              circularBitmapDrawable.setCornerRadius(dpToPx(10));
              circularBitmapDrawable.setAntiAlias(true);
              imageView.setImageDrawable(circularBitmapDrawable);
            }
         });
    
    
    public int dpToPx(int dp) {
      DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
      return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }
    

提交回复
热议问题