[英]Preload images into memory/disk with Android Picasso
我可以在展示之前用Picasso下载图像吗? 我想先缓存图像。
示例场景:用户单击按钮,查看进度条,图像加载完成后,用户可以在屏幕上看到图像。
我尝试使用“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();
这是我的毕加索单身课程
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();
}
}
}
接下来向用户显示(缓存)图像。
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());
不幸的是,这不起作用。 感谢yopur的建议!
正如dnkoutso所说,你只需使用:
这是毕加索的内置! 请upvote dnkoutso的评论。
PS我假设你所问的是“我如何制作毕加索预载图像?”
请注意,毕加索绝对完全处理所有图像的缓存 - 没有你的进一步努力 - 这是毕加索的目的以及你使用它的原因 - 这是毕加索的存在理由 。 所以你不必担心。
再次,如果你正在谈论预装或让我们说“热身” ,那么就完全按照dnkoutso说的那样做。
(顺便说一句,所有这一切都归结为:“在图片的长ListView中看起来有多好”。我们实际上发现Picasso非常流畅,一般来说,你甚至都不需要担心大多数应用程序的热身阶段。事实上,甚至像这样的关键概念......
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
注意 - 这篇着名的文章http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html已经从网上消失了。
...真的Picasso非常容易使用,你真的很少需要打扰“老式”仔细的ListView处理。 总之,我们发现,你很少需要担心“热身”。 希望能帮助到你。)
您可以在显示之前获取图像。 加载后,您可以加载到imageView。 当你第二次调用Picasso#load()时,不会再次下载图像。 它将来自缓存。
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.