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

一、问题引入

    Android开发中经常要使用到ImageView,而ImageView控件自带的宽度width、高度height等属性无法让ImageView呈现出圆形、圆角这样的形状,相信很多小伙伴都和我一样为这个问题苦恼,现在介绍几种方法,可以很方便地实现圆形图片。
在这里插入图片描述

二、方法总结

方法一:使用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);
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值