preLOAD图像到内存/硬盘采用Android毕加索 [英] Preload images into memory/disk with Android Picasso

查看:251
本文介绍了preLOAD图像到内存/硬盘采用Android毕加索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以显示它们之前下载毕加索的图像? 我想先缓存图像。

Can I download images with Picasso before displaying them? 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.

我试图用get方法载入图片,但没有缓存的图像。

I tried to load images with the "get" method but that did not cache the images.

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();
        }
    }
}

下届展会(缓存)的图像给用户。

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());

不幸的是,这是行不通的。 感谢yopur建议!

Unfortunately, this does not work. Thanks for yopur suggestions!

推荐答案

由于dnkoutso说,你只需要使用:

As dnkoutso says you just use:

这是建立在毕加索!请upvote dnkoutso的评论。

Which is built in to Picasso! Please upvote dnkoutso's comment.

PS我假设你问什么是如何使毕加索pre-LOAD图像吗?

PS I'm assuming what you're asking is "how do I make Picasso PRE-LOAD images?"

需要注意的是毕加索绝对的,完全的完全处理图像的所有高速缓存 - 与您没有进一步的努力 - 这是毕加索的目的,为什么你使用它 - 这是毕加索的存在理由。所以你并不需要担心。

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.

此外,如果你谈论的 pre加载或让我们说的热身,然后做的正是dnkoutso说。

Again if you're talking pre-loading or let's say "warm-up", then do exactly what dnkoutso said.

(顺便说一句,这一切仅仅归结为:多么好的事情看起来在图像的长列表视图我们实际上已经发现,毕加索就是这么流畅,你真的甚至不需要担心,通常,关于大多数应用程序的热身阶段的确,即使是这样的关键概念......

(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....

<一个href="http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/">http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

请注意 - 这个著名的文章<一个href="http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html">http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html已经从网络上消失了。

Note - this famous article http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html has disappeared from the web ..

...真的毕加索就是这么容易使用,你,真的,很少需要费心老式的认真的ListView处理。总之,你很少需要担心热身,我们发现。希望它能帮助。)

... really Picasso is just so easy to use, you, really, rarely need to bother with "old-fashioned" carefully ListView handling. In short you very rarely need to worry about "warm-up", we've found. Hope it helps.)

这篇关于preLOAD图像到内存/硬盘采用Android毕加索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆