How to make an ImageView with rounded corners?

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

    you can use only ImageView in your layout and using glide, you can apply round corners using this method.

    first in your gradle write,

    compile 'com.github.bumptech.glide:glide:3.7.0'
    

    for image with rounded corners,

    public void loadImageWithCorners(String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .centerCrop()
                .placeholder(R.color.gray)
                .error(R.color.gray)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new BitmapImageViewTarget(view) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(32.0f); // radius for corners
                        view.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }
    

    call method :

    loadImageWithCorners("your url","your imageview");
    

提交回复
热议问题