2015-04-03 76 views
5

我想建立一個應用程序,通過使用畢加索圖書館在gridview中顯示圖像。圖像由遠程服務器檢索。我應該製作一個AsyncTask類還是該類由畢加索圖書館本身處理?到目前爲止,我所見過的所有畢加索教程都顯得有些模糊。畢加索圖書館和GridView圖像

謝謝,

泰奧。

+2

畢加索是一個功能強大的圖像下載和緩存庫爲Android。您不需要編寫下載圖像的異步任務,而是通過此庫處理。讓我知道如果你需要更多的信息如何寫它我可以在網格視圖中顯示你的工具。 – 2015-04-03 05:55:35

+0

感謝您的回覆。我會用gridview試試它,如果我有任何問題,我會問你。 – Theo 2015-04-03 06:08:14

回答

7

它的使用方法很簡單畢加索lib中加載圖像在GridView中,as demonstrated here

class SampleGridViewAdapter extends BaseAdapter { 
    private final Context context; 
    private final List<String> urls = new ArrayList<String>(); 

    public SampleGridViewAdapter(Context context) { 
    this.context = context; 

    // Ensure we get a different ordering of images on each run. 
    Collections.addAll(urls, Data.URLS); 
    Collections.shuffle(urls); 

    // Triple up the list. 
    ArrayList<String> copy = new ArrayList<String>(urls); 
    urls.addAll(copy); 
    urls.addAll(copy); 
    } 

    @Override public View getView(int position, View convertView, ViewGroup parent) { 
    SquaredImageView view = (SquaredImageView) convertView; 
    if (view == null) { 
     view = new SquaredImageView(context); 
     view.setScaleType(CENTER_CROP); 
    } 

    // Get the image URL for the current position. 
    String url = getItem(position); 

    // Trigger the download of the URL asynchronously into the image view. 
    Picasso.with(context) // 
     .load(url) // 
     .placeholder(R.drawable.placeholder) // 
     .error(R.drawable.error) // 
     .fit() // 
     .tag(context) // 
     .into(view); 

    return view; 
    } 

    @Override public int getCount() { 
    return urls.size(); 
    } 

    @Override public String getItem(int position) { 
    return urls.get(position); 
    } 

    @Override public long getItemId(int position) { 
    return position; 
    } 
} 
+0

謝謝我的朋友。保重。 – Theo 2015-04-03 06:07:15