Glide.with(getContext()).load(videoBean.getCoverUrl()) .transform(new CenterCrop(), new RoundedCornersTransformation(10,0,RoundedCornersTransformation.CornerType.TOP)) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { // 图片加载完成后,判断ImageView是否在屏幕中可见 if (target.isVisible()) { // ImageView在屏幕中可见,直接显示图片 target.onResourceReady(resource, null); } return true; } }) .into(coverImageView);没有target.isVisible()这个方法
时间: 2024-02-10 17:35:26 浏览: 28
你说的对,确实没有`target.isVisible()`这个方法。可能是因为新版本的Glide已经不再支持这个方法了。如果你想判断ImageView是否在屏幕中可见,可以使用`ViewCompat.isAttachedToWindow(view) && view.getGlobalVisibleRect(rect)`来判断。其中,`ViewCompat.isAttachedToWindow(view)`用来判断View是否被附加到了Window上,`view.getGlobalVisibleRect(rect)`用来获取View在屏幕中的可见区域。
相关问题
Glide.with load asset 下图片
使用 Glide Assets 加载本地资源文件的步骤如下:
1. 引入 Glide Assets 库
在项目的 build.gradle 文件中添加以下依赖:
```
dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'
implementation 'com.github.bumptech.glide:glide-compiler:4.12.0'
implementation 'com.github.bumptech.glide:assets:0.2.4'
}
```
2. 加载本地资源文件
通过 Glide.with() 方法获取 Glide 实例,并调用 load() 方法加载资源文件,最后通过 into() 方法将资源文件加载到 ImageView 中。
```
Glide.with(context)
.load("file:///android_asset/image.jpg")
.into(imageView);
```
其中,file:///android_asset/image.jpg 表示资源文件的路径,可以根据实际情况进行修改。另外,需要注意的是,Glide Assets 只能加载 assets 目录下的资源文件。
Glide.centerCrop()第一次显示无效
可能是因为图片还没有加载完成,导致第一次显示无效。你可以尝试使用`.listener()`方法来监听图片加载完成的事件,然后再进行centerCrop处理。示例代码如下:
```
Glide.with(context)
.load(imageUrl)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (resource != null) {
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
return false;
}
})
.into(imageView);
```
在`onResourceReady`回调中,我们检查资源是否为空,如果不为空,则设置`imageView`的`ScaleType`为`CENTER_CROP`。这样,在资源加载完成后,我们才会对图片进行centerCrop处理。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)