android mageview更换资源
时间: 2023-08-21 07:01:54 浏览: 35
要在 Android 中使用 Glide 加载图片并动态更换资源,可以按照以下步骤操作:
1. 在 app 的 build.gradle 文件中添加 Glide 依赖:
```
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
```
2. 在布局文件中添加一个 ImageView 控件:
```
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
3. 在 Activity 或 Fragment 中获取 ImageView 控件的引用,并使用 Glide 加载图片:
```
ImageView imageView = findViewById(R.id.imageView);
Glide.with(this)
.load(R.drawable.old_image)
.into(imageView);
```
4. 在需要更换图片的时候,调用 Glide 的 load 方法加载新的图片资源即可:
```
Glide.with(this)
.load(R.drawable.new_image)
.into(imageView);
```
通过以上步骤,就可以使用 Glide 在 Android 中动态更换 ImageView 的图片资源了。