How to make an ImageView with rounded corners?

前端 未结 30 3122
天涯浪人
天涯浪人 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:14

    With the help of glide library and RoundedBitmapDrawableFactory class it's easy to achieve. You may need to create circular placeholder image.

        Glide.with(context)
            .load(imgUrl)
            .asBitmap()
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.placeholder)
            .into(new BitmapImageViewTarget(imgProfilePicture) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
                            Bitmap.createScaledBitmap(resource, 50, 50, false));
                    drawable.setCornerRadius(10); //drawable.setCircular(true);
                    imgProfilePicture.setImageDrawable(drawable);
                }
            });
    

提交回复
热议问题