2014-03-19 110 views
16

在顯示圖像之前,我可以使用Picasso下載圖像嗎? 我想先緩存圖像。使用Android 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建議!

+34

爲什麼不使用'fetch()'來代替? – dnkoutso

+0

@dnkoutso fetch方法沒有返回任何內容,那麼我將如何獲取位圖? – AndroidDev

+13

我沒有看到你在get()調用中返回的位圖。我強烈建議你在這裏使用'fetch()',讓畢加索爲你處理線程和請求合併。如果一個提取正在進行,並且有一個'into()',它將合併這兩個並傳遞它們。 – dnkoutso

回答

31

由於dnkoutso說,你只需要使用:

取()

這是建立在對畢加索!請upvote dnkoutso的評論。


PS我假設你問的是「我如何製作畢加索PRE-LOAD圖像?」

注意,畢加索絕對和完全完全處理圖像的所有高速緩存 - 與您沒有進一步的努力 - 這是畢加索的目的,爲什麼你使用它 - 這是畢加索的存在理由。所以你不必擔心這一點。

再次,如果你說預加載或讓我們說「熱身」,然後做什麼dnkoutso說。 (順便說一下,所有這一切簡單歸結爲:「在圖像的長ListView中看起來有多美好」,我們發現畢加索是如此的流暢,你甚至不需要擔心,一般約在大多數應用程序熱身階段事實上,即使像這樣的關鍵概念....

http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

注 - 這個著名的文章http://www.programmingmobile.com/2012/03/buttery-smooth-list-scrolling-in.html已經從網上消失了..

...畢加索真的很容易使用,你真的很少需要仔細打理「老式」的ListVi如何處理。總之,你很少需要擔心「熱身」,我們發現。希望它有幫助。)

7

您可以在顯示之前獲取圖像。加載後,您可以加載到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() { 

    } 
});