Android开发中经常要使用到ImageView,而ImageView控件自带的宽度width、高度height等属性无法让ImageView呈现出圆形、圆角这样的形状,相信很多小伙伴都和我一样为这个问题苦恼,现在介绍几种方法,可以很方便地实现圆形图片。
implementation 'com.github.bumptech.glide:glide:3.7.0'
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);
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"
/>
// 从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);
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);