[英]Preload images into memory/disk with Android Picasso
Can I download images with Picasso before displaying them? 我可以在展示之前用Picasso下载图像吗? I want to cache images first.
我想先缓存图像。
Sample scenario: User clicks on the button, sees the progressbar, and when the images have finished loading user see the images on the screen. 示例场景:用户单击按钮,查看进度条,图像加载完成后,用户可以在屏幕上看到图像。
I tried to load images with the "get" method but that did not cache the images. 我尝试使用“get”方法加载图像,但是没有缓存图像。
Thread thread = new Thread()
{
@Override
public void run() {
try {
Picasso picasso = PicassoOwnCache.with(getApplicationContext());
RequestCreator picassoRequest;
for (String imgUrl : imagesUrls) {
picassoRequest = picasso.load(imgUrl);
picassoRequest.get();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
This is my Picasso singleton class 这是我的毕加索单身课程
public class PicassoOwnCache {
static Picasso singleton = null;
static Cache cache = null;
public static Picasso with(int cacheSize, Context context) {
if (singleton == null) {
int maxSize = calculateMemoryCacheSize(context);
cache = new LruCache(cacheSize <= maxSize ? cacheSize : maxSize);
singleton = new Picasso.Builder(context)
.memoryCache(cache)
.build();
}
return singleton;
}
public static Picasso with(Context context) {
if (singleton == null) {
cache = new LruCache(calculateMemoryCacheSize(context));
singleton = new Picasso.Builder(context)
.memoryCache(cache)
.build();
}
return singleton;
}
static int calculateMemoryCacheSize(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
int memoryClass = am.getMemoryClass();
if (largeHeap && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
}
return 1024 * 1024 * memoryClass / 10;//7;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static class ActivityManagerHoneycomb {
static int getLargeMemoryClass(ActivityManager activityManager) {
return activityManager.getLargeMemoryClass();
}
}
}
Next show (cached) image to the user. 接下来向用户显示(缓存)图像。
Picasso picasso = PicassoOwnCache.with(getApplicationContext());
picasso.setDebugging(true) ;
RequestCreator picassoRequest;
picassoRequest = picasso.load(imgUrl);
picassoRequest
.placeholder(R.drawable.loading_logo)
.error(R.drawable.no_internet)
.fit() // I tries also without fit()
.into(holder.getImageView());
Unfortunately, this does not work. 不幸的是,这不起作用。 Thanks for yopur suggestions!
感谢yopur的建议!
As dnkoutso says you just use: 正如dnkoutso所说,你只需使用:
Which is built in to Picasso! 这是毕加索的内置! Please upvote dnkoutso's comment.
请upvote dnkoutso的评论。
PS I'm assuming what you're asking is "how do I make Picasso PRE-LOAD images?" PS我假设你所问的是“我如何制作毕加索预载图像?”
Note that Picasso absolutely and totally completely handles all caching of images -- with no further effort from you -- that is the purpose of Picasso and why you use it -- that is the raison d'être of Picasso . 请注意,毕加索绝对完全处理所有图像的缓存 - 没有你的进一步努力 - 这是毕加索的目的以及你使用它的原因 - 这是毕加索的存在理由 。 So you don't need to worry about that.
所以你不必担心。
Again if you're talking pre-loading or let's say "warm-up" , then do exactly what dnkoutso said. 再次,如果你正在谈论预装或让我们说“热身” ,那么就完全按照dnkoutso说的那样做。
(By the way, all of this simply comes down to: "how good things look in long ListViews of images". We've actually found that Picasso is just so smooth, you really don't even need to worry, generally, about a warm-up phase in most apps. indeed, even critical concepts like this.... (顺便说一句,所有这一切都归结为:“在图片的长ListView中看起来有多好”。我们实际上发现Picasso非常流畅,一般来说,你甚至都不需要担心大多数应用程序的热身阶段。事实上,甚至像这样的关键概念......
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
Note - this famous article http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html has disappeared from the web .. 注意 - 这篇着名的文章http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html已经从网上消失了。
... really Picasso is just so easy to use, you, really, rarely need to bother with "old-fashioned" carefully ListView handling. ...真的Picasso非常容易使用,你真的很少需要打扰“老式”仔细的ListView处理。 In short you very rarely need to worry about "warm-up", we've found.
总之,我们发现,你很少需要担心“热身”。 Hope it helps.)
希望能帮助到你。)
You can fetch images before displaying. 您可以在显示之前获取图像。 After it is loaded, you can load into imageView.
加载后,您可以加载到imageView。 When you call Picasso#load() second time, image won't be downloaded again.
当你第二次调用Picasso#load()时,不会再次下载图像。 It will come from cache.
它将来自缓存。
startProgress(); // handle showing progress view somehow
Picasso.with(getActivity()).load(imageUrl).fetch(new Callback() {
@Override
public void onSuccess() {
Picasso.with(getActivity()).load(imageUrl).into(imgViewMedia);
stopProgress(); // handle stopping progress view somehow
}
@Override
public void onError() {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.