如何使用 Picasso 保存图像,这样就没有加载时间?
How to save images with Picasso so there is no loading time?
我有一个显示报价的应用程序,背景中有一张图片。当我点击 "next" 按钮时,将显示下一个报价并加载新图像。加载大约需要 0.5 秒,并将之前的图像作为占位符。但是当我切换回去时,没有加载时间。
这意味着,图像保存在某个地方,因此不需要再次加载。不幸的是,这只是暂时的。当我看到接下来的 5 张图片并返回到第一张时,需要再次加载第一张图片。所以我尝试在开始时加载所有图片(只有 25 张图片),如下所示:
Picasso.get().load(backgrounds.get(1));
Picasso.get().load(backgrounds.get(2));
Picasso.get().load(backgrounds.get(3));
Picasso.get().load(backgrounds.get(4));
Picasso.get().load(backgrounds.get(5));
Picasso.get().load(backgrounds.get(6));
Picasso.get().load(backgrounds.get(7));
当我点击我的 "next" 按钮时,我使用这个:
Picasso.get().load(backgrounds.get(counterBackground)).fit().noPlaceholder().into(background);
但是这两件事都没有达到预期的效果。图像每次需要大约 0.5 的加载时间,并且 "noPlaceholder" 没有像预期的那样工作,占位符仍然存在。
那么,有人知道如何缩短加载时间吗?比如,如何在开始加载所有图像?
感谢您的每一个回答!
您可以在使用 Picasso 时创建缓存,这将导致更快的加载时间(只要图像被缓存
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
Picasso.setSingletonInstance(picasso);
source
您可以使用指示器查看从何处加载图像
setIndicatorsEnabled(true)
您还可以使用回调在图片加载完成时获得指示
可以看到更多详情here
编辑
你可以使用
Picasso.get().load(R.drawable.pic).into(imageview)
我认为它也适用于 mipmap,通常 mipmap 用于您的资源的错误/占位符(如果您不使用可绘制对象)
2.
您可以针对如何 cache/load 您的图片
创建自己的政策
Link
我有一个显示报价的应用程序,背景中有一张图片。当我点击 "next" 按钮时,将显示下一个报价并加载新图像。加载大约需要 0.5 秒,并将之前的图像作为占位符。但是当我切换回去时,没有加载时间。
这意味着,图像保存在某个地方,因此不需要再次加载。不幸的是,这只是暂时的。当我看到接下来的 5 张图片并返回到第一张时,需要再次加载第一张图片。所以我尝试在开始时加载所有图片(只有 25 张图片),如下所示:
Picasso.get().load(backgrounds.get(1));
Picasso.get().load(backgrounds.get(2));
Picasso.get().load(backgrounds.get(3));
Picasso.get().load(backgrounds.get(4));
Picasso.get().load(backgrounds.get(5));
Picasso.get().load(backgrounds.get(6));
Picasso.get().load(backgrounds.get(7));
当我点击我的 "next" 按钮时,我使用这个:
Picasso.get().load(backgrounds.get(counterBackground)).fit().noPlaceholder().into(background);
但是这两件事都没有达到预期的效果。图像每次需要大约 0.5 的加载时间,并且 "noPlaceholder" 没有像预期的那样工作,占位符仍然存在。
那么,有人知道如何缩短加载时间吗?比如,如何在开始加载所有图像?
感谢您的每一个回答!
您可以在使用 Picasso 时创建缓存,这将导致更快的加载时间(只要图像被缓存
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
Picasso.setSingletonInstance(picasso);
source
您可以使用指示器查看从何处加载图像
setIndicatorsEnabled(true)
您还可以使用回调在图片加载完成时获得指示
可以看到更多详情here
编辑
你可以使用
Picasso.get().load(R.drawable.pic).into(imageview)
我认为它也适用于 mipmap,通常 mipmap 用于您的资源的错误/占位符(如果您不使用可绘制对象)
2.
您可以针对如何 cache/load 您的图片
创建自己的政策Link