2014-07-22 139 views
16

我想onStart()方法使用畢加索加載從服務器映像,我想顯示一個進度條,直到照片被完全下載 這裏是我的代碼:如何在picasso中加載圖像時使用進度條?

@Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 

     Picasso.with(context).load(imageLoad) 
       .placeholder(R.id.progressBarDetails) 
       .error(R.drawable.friend_request).noFade().resize(200, 200) 
       .into(avatarImage, new Callback() { 
        @Override 
        public void onError() { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onSuccess() { 
         // TODO Auto-generated method stub 
         progressbar.setVisibility(View.GONE); 
        } 

       }); 

     Picasso.with(this).load(imageLoad).into(target); 

    } 

    OnFinished a = new OnFinished() { 

     @Override 
     public void onSendFinished(IntentSender IntentSender, Intent intent, 
       int resultCode, String resultData, Bundle resultExtras) { 
      // TODO Auto-generated method stub 
      intent = new Intent(getApplicationContext(), Map.class); 
     } 
    }; 

    private Target target = new Target() { 
     @Override 
     public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        File file = new File(Environment 
          .getExternalStorageDirectory().getPath() 
          + "/actress_wallpaper.jpg"); 
        try { 
         file.createNewFile(); 
         FileOutputStream ostream = new FileOutputStream(file); 
         bitmap.compress(CompressFormat.JPEG, 75, ostream); 
         ostream.close(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

       } 
      }).start(); 
     } 
+0

爲什麼你必須在'onStart'上兩次調用'Picasso'?你的帶有回調的'畢加索'應該可以工作。 – chip

+0

你的意思是Picasso.with(this).load(imageLoad).into(target); – androidAhmed

回答

16

我沒有測試你的代碼,但即使該工作,該文件actress_wallpaper.jpg未加載ImageView中。在文檔,它說

對象實現這個類必須有Object.equals(Object)Object.hashCode()妥善存放工作實施內部。

試試這個:

File file = new File(pathToFile); 

Picasso.with(context) 
     .load(file) 
     .into(imageView, new Callback() { 
      @Override 
      public void onSuccess() { 
       progressbar.setVisibility(View.GONE); 
      } 
     }); 

警告我沒有測試過我的代碼。

更新:

我試圖2.3.2版本2.3.3和,好像是有一個問題https://github.com/square/picasso/issues/539

+0

該代碼工作在加載圖像,但在顯示進度條,直到加載的圖像沒有工作 – androidAhmed

+0

回調是位圖加載到imageview後觸發 – chip

+0

progressbar工作正常,但當我觸摸屏幕,然後顯示圖像 –

1

這是一個老問題,但可能是這個答案可以幫助別人作爲從服務器加載圖像時,我在顯示進度條時也遇到了問題。

我正在使用畢加索2.4.0。而我正在使用Picasso Target界面在imageview中加載圖像。下面是測試和工作代碼:

首先加入下面幾行:

ImageView ivPhoto = (ImageView) findViewById(R.id.iv_photo); 
ProgressBar pbLoadingBar = (ProgressBar) findViewById(R.id.pb_loading_bar); 

//get image url 
String imageUrl = getImageUrl(); 

//ImageViewTarget is the implementation of Target interface. 
//code for this ImageViewTarget is in the end 
Target target = new ImageViewTarget(ivPhoto, pbLoadingBar); 
Picasso.with(mContext) 
      .load(imageUrl) 
      .placeholder(R.drawable.place_holder) 
      .error(R.drawable.error_drawable) 
      .into(target); 

下面是上述

private static class ImageViewTarget implements Target { 

    private WeakReference<ImageView> mImageViewReference; 
    private WeakReference<ProgressBar> mProgressBarReference; 

    public ImageViewTarget(ImageView imageView, ProgressBar progressBar) { 
     this.mImageViewReference = new WeakReference<>(imageView); 
     this.mProgressBarReference = new WeakReference<>(progressBar); 
    } 

    @Override 
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 

     //you can use this bitmap to load image in image view or save it in image file like the one in the above question. 
     ImageView imageView = mImageViewReference.get(); 
     if (imageView != null) { 
      imageView.setImageBitmap(bitmap); 
     } 

     ProgressBar progressBar = mProgressBarReference.get(); 
     if (progressBar != null) { 
      progressBar.setVisibility(View.GONE); 
     } 
    } 

    @Override 
    public void onBitmapFailed(Drawable errorDrawable) { 
     ImageView imageView = mImageViewReference.get(); 
     if (imageView != null) { 
      imageView.setImageDrawable(errorDrawable); 
     } 

     ProgressBar progressBar = mProgressBarReference.get(); 
     if (progressBar != null) { 
      progressBar.setVisibility(View.GONE); 
     } 
    } 

    @Override 
    public void onPrepareLoad(Drawable placeHolderDrawable) { 
     ImageView imageView = mImageViewReference.get(); 
     if (imageView != null) { 
      imageView.setImageDrawable(placeHolderDrawable); 
     } 

     ProgressBar progressBar = mProgressBarReference.get(); 
     if (progressBar != null) { 
      progressBar.setVisibility(View.VISIBLE); 
     } 
    } 
} 

上面的代碼工作正常使用的目標接口的實現,如果用於裝載活動中的圖像。但是,如果您想在gridview/recyclerview中加載圖像或查看使用相同視圖持有者的尋呼機等,您可能會得到an issue其中onBitmapLoaded()未被調用(因爲視圖已被回收,而Picasso只保留對目標的弱引用目的)。這裏是a link來解決這個問題。

相關問題