android studio吧imageView图片设置为圆形
时间: 2024-10-07 07:08:31 浏览: 177
在Android Studio中,如果你想要让ImageView显示的图片变为圆形,可以使用`CircleImageView`库或者自定义一个适配器将图片转换为圆形。以下是两种常见的做法:
1. 使用`CircleImageView`库:
- 首先,在你的build.gradle文件(Module: app)中添加依赖:
```gradle
implementation 'de.hdodenhof:circleimageview:3.0.0'
```
- 然后在XML布局中使用`CircleImageView`替换标准的`ImageView`:
```xml
<com.github.bumptech.glide.load.resource.drawable.GlideDrawable> <!-- 如果你用的是Glide -->
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circle_image"
android:src="@drawable/your_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
2. 自定义适配器:
- 创建一个`RoundedBitmapDrawable`类来处理图片转换:
```java
private static class RoundedBitmapDrawable extends BitmapDrawable {
public RoundedBitmapDrawable(Resources res, Bitmap bitmap) {
super(res, bitmap);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
setCornerRadius(getIntrinsicWidth() / 2f); // 或者根据实际尺寸调整圆角半径
}
}
```
- 在ImageView的设置上,动态创建这个适配器:
```java
ImageView imageView = findViewById(R.id.image_view);
Bitmap yourImage = ...; // 图片加载或获取
imageView.setImageDrawable(new RoundedBitmapDrawable(getResources(), yourImage));
```
阅读全文
相关推荐


















