2014-05-04 37 views
1

我想加載多個ImageView與畢加索,但它只加載一個ImageView。這是我的代碼:從畢加索的URL加載多個Imageview?

public void setClassicImg(JSONArray listImg, RelativeLayout rl) throws JSONException, IOException, MalformedURLException { 
    if (listImg.length() > 0) 
    { 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     for (int i = 0; i < listImg.length(); i++) 
     { 

      ImageView downloadedImg = new ImageView(this); 
          // downloadedImg.setBackgroundColor(Color.parseColor(listImg.getJSONObject(i).getString("color"))); 
      //  new DownloadImageTask(downloadedImg).execute(listImg.getJSONObject(i).getString("http")); 

      Picasso.with(this.getApplicationContext()).load(listImg.getJSONObject(i).getString("http")).into(downloadedImg); 

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listImg.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listImg.getJSONObject(i).getDouble("size_y"))); 
      params.leftMargin = (int) (metrics.widthPixels * listImg.getJSONObject(i).getDouble("position_x")); 
      params.topMargin = (int) (metrics.heightPixels * listImg.getJSONObject(i).getDouble("position_y")); 
      downloadedImg.setScaleType(ImageView.ScaleType.FIT_XY); 
      //    downloadedImg.getLayoutParams().width = BIND_ALLOW_OOM_MANAGEMENT; 


      /// downloadedImg.setBackgroundDrawable(animation); 
      //     downloadedImg.setImageDrawable(animation); 
      rl.addView(downloadedImg, params); 
     } 
    } 
} 

我解析一個json文件。包含Image的.png或.jpg的字符串位於字符串http中,因此我可以得到它,但是在設備上,它只顯示加載的第一個ImageView,好像它是一次循環一樣。

有人對我做錯了什麼想法嗎?

+0

我有同樣的問題,它的任何更新? –

回答

0

我得到了同樣的問題,畢加索只是加載第一個,在我的情況下我的解決方案是創建一個自定義類來處理它,所以我存儲在一個列表中的URL,當它結束加載第一個我開始加載下一個。並且不會很慢,因爲我加載了沒有目標的所有東西,所以它緩存了圖像。

@Override 
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
    //DO whatever you need with the bitmap 
    urls.remove(currentUrl); 
    if (urls.size() > 0) { 
     currentUrl = urls.get(0); 
     Picasso.with(context).load(urls.get(0)).into(this); 
    } 
} 

和我的方法新增網址

public void addUrl(String url) { 
    urls.add(url); 
    if (currentUrl.isEmpty()) { 
     //this would be the first Url, so load directly 
     currentUrl = url; 
     Picasso.with(context).load(url).into(this); 
    } 
    else { 
     //load to cache, than when loaded into the target it will get from memory 
     Picasso.with(context).load(url); 
    } 
}