Android加载圆形(圆角)图片的方式总结(RoundedBitmapDrawable、Glide)

一、问题引入

    Android开发中经常要使用到ImageView,而ImageView控件自带的宽度width、高度height等属性无法让ImageView呈现出圆形、圆角这样的形状,相信很多小伙伴都和我一样为这个问题苦恼,现在介绍几种方法,可以很方便地实现圆形图片。
Android加载圆形(圆角)图片的方式总结(RoundedBitmapDrawable、Glide)_第1张图片

二、方法总结

方法一:使用Glide(推荐使用)

  • 在build.gradle中引入Glide包:implementation 'com.github.bumptech.glide:glide:3.7.0'
  • 在Activity中引入Glide包等
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
  • 圆形图片
// context表示当前所在活动,例如MainActivity.this
// url表示图片资源网址或本地资源R.drawable.icon等
// apply应用圆形变换方法
// image表示布局中的ImageView控件
context = MainActivity.this
url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242332225H9-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1652004122&t=70e3b52d14b9bf1bb2d795b7e1d32bf5"
image = (ImageView) findViewById(R.id.image);
Glide.with(context)
	 .load(url)
     .apply(RequestOptions.bitmapTransform(new CircleCrop()))
     .into(image);
  • 圆角图片
// context表示当前所在活动,例如MainActivity.this
// url表示图片资源网址或本地资源R.drawable.icon等
// apply应用圆形变换方法,10表示圆角半径
// image表示布局中的ImageView控件
context = MainActivity.this
url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242332225H9-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1652004122&t=70e3b52d14b9bf1bb2d795b7e1d32bf5"
image = (ImageView) findViewById(R.id.image);
Glide.with(context)
	 .load(url)
     .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
     .into(image);

方法二:使用自定义的RoundImageView(实现圆形)

  • 自定义RoundImageView.java,代码如下所示:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundImageView extends androidx.appcompat.widget.AppCompatImageView{
    private Paint paint;
    public RoundImageView(Context context) {
        this(context, null);
    }
    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint = new Paint();
    }

    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null != drawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b = getCircleBitmap(bitmap, 14);
            final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
            final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
            paint.reset();
            canvas.drawBitmap(b, rectSrc, rectDest, paint);
        } else {
            super.onDraw(canvas);
        }
    }

    private Bitmap getCircleBitmap(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 Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        int x = bitmap.getWidth();
        canvas.drawCircle(x / 2, x / 2, x / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
  • 直接在布局文件中引入,示例如下:
<com.example.weibo.RoundImageView
	android:id="@+id/roundIcon"
	android:layout_width="40dp"
	android:layout_height="40dp"
	android:layout_margin="5dp"
/>

方法三:使用RoundedBitmapDrawable(老版本,不推荐使用)

  • RoundedBitmapDrawable位于android .support.v4.graphics.drawable,需在build.gradle中引入v4系列的包
  • 圆形图片
    setCircular(boolean circular) : 把图片的形状设为圆形
// 从Drawable资源中获取图片存入bitmap,设为圆形并存入ImageView控件
ImageView image = findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCircular(true);
image.setImageDrawable(roundedBitmapDrawable);
  • 圆角图片
    setCornerRadius(float cornerRadius) : 设置图片的圆角半径
ImageView image = findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(100);
image.setImageDrawable(roundedBitmapDrawable);

你可能感兴趣的:(Android开发,android,java)