How to make an ImageView with rounded corners?

前端 未结 30 2474
天涯浪人
天涯浪人 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);
                }
            });
    
    0 讨论(0)
  • 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);
        }
    });
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-11-21 06:18

    This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images.

    http://www.ruibm.com/?p=184

    This isn't my code, but I've used it and it's works wonderfully. I used it as a helper within an ImageHelper class and extended it just a bit to pass in the amount of feathering I need for a given image.

    Final code looks like this:

    package com.company.app.utils;
    
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.Bitmap.Config;
    import android.graphics.PorterDuff.Mode;
    
    public class ImageHelper {
        public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                    .getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);
            final float roundPx = pixels;
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            return output;
        }
    }
    

    Hope this helps someone!

    0 讨论(0)
  • 2020-11-21 06:18

    Rounded image Using ImageLoader here

    Create DisplayImageOptions:

    DisplayImageOptions options = new DisplayImageOptions.Builder()
        // this will make circle, pass the width of image 
        .displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_menu))) 
        .cacheOnDisc(true)
        .build();
    
    imageLoader.displayImage(url_for_image,ImageView,options);
    

    Or you can user Picasso Library from Square.

    Picasso.with(mContext)
        .load(com.app.utility.Constants.BASE_URL+b.image)
        .placeholder(R.drawable.profile)
        .error(R.drawable.profile)
        .transform(new RoundedTransformation(50, 4))
        .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
        .centerCrop()
        .into(v.im_user);
    

    you can download RoundedTransformation file here here

    0 讨论(0)
  • 2020-11-21 06:18

    As all the answers seemed too complicated for me just for round corners I thought and came to another solution which I think is worth to share, just with XML in case you have some space around the image:

    Create a bordered shape with transparent content like this:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners 
            android:radius="30dp" />
        <stroke 
            android:color="#ffffffff"
            android:width="10dp" />
    </shape> 
    

    Then in a RelativeLayout you can first place your image and then in the same location above the shape with another ImageView. The cover-shape should be larger in size by the amount of the border width. Be careful to take a larger corner radius as the outer radius is defined but the inner radius is what covers your image.

    Hope it helps somebody, too.

    Edit as per CQM request the relative layout example:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageToShow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imgCorners"
            android:layout_alignLeft="@+id/imgCorners"
            android:layout_alignRight="@+id/imgCorners"
            android:layout_alignTop="@+id/imgCorners"
            android:background="#ffffff"
            android:contentDescription="@string/desc"
            android:padding="5dp"
            android:scaleType="centerCrop" />
    
        <ImageView
            android:id="@+id/imgCorners"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/desc"
            android:src="@drawable/corners_white" />
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题