Android Studio 怎么把图片设置成圆角
时间: 2023-12-19 20:31:18 浏览: 51
以下是两种在Android Studio中将图片设置成圆角的方法:
1. 使用Java代码动态设置圆角
```java
// 加载图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// 将图片转换成圆角
Bitmap roundBitmap = getRoundBitmap(bitmap, 50);
// 将圆角图片显示在ImageView上
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(roundBitmap);
// 获取圆角图片的方法
public static Bitmap getRoundBitmap(Bitmap bitmap, int radius) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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 = radius;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
```
2. 使用Glide加载图片并设置圆角
```java
// 加载图片并设置圆角
Glide.with(this)
.load(R.drawable.image)
.apply(RequestOptions.bitmapTransform(new RoundedCorners(50)))
.into(imageView);
```