我正在尝试使用Picasso将图片从网址加载到scanf
。
共享转换的图片位于ImageView
的{{1}}内,第二个位于LinearLayout中。
第一次发出请求时元素没有动画,因为毕加索需要将图像加载到视图中,但是在后续请求中它很好。
如何确保在启动转换之前已成功填充CardView
?
答案 0 :(得分:25)
解决方案是使用postponeEnterTransition()
和startPostponedEnterTransition()
调用来确保毕加索在活动开始之前已将图像加载到视图中。
This是一篇很棒的博客文章。
以下是您在RecyclerView
ImageView
答案 1 :(得分:5)
如果你想避免推迟输入转换所造成的影响,你可以强制Picasso使用它在缓存中已有的任何东西:
Target target = new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
this.imageView.setImageBitmap(bitmap);
}
@Override public void onBitmapFailed(Drawable errorDrawable) {}
@Override public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
Picasso.with(context)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(target);
通过传递Target
实例而不是ImageView
实例,您将强制Picasso提供它为该网址缓存的任何内容。如果您改为通过ImageView
,则不会立即加载图像。
请注意,您应该将目标对象保存在成员变量中,因为它被Picasso保存为弱引用,因此您不希望它被垃圾回收。
答案 2 :(得分:0)
你可以用通用图像加载器替换Picasso,它可以解决这个问题
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(drawable)
.showImageForEmptyUri(emptyDrawable)
.showImageOnFail(errorDrawable)
.resetViewBeforeLoading(false)
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.build();
imageLoader.displayImage(url, iv, options);