2014-07-22 23 views
16

Chcę onStart() sposób, aby załadować obraz z serwerem za pomocą Picasso i chcę pokazać pasek postępu aż zdjęcia są całkowicie pobrane Oto mój kod:jak używać progressbar podczas ładowania obrazu w 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

dlaczego musisz dwukrotnie wywoływać 'Picasso' w' onStart'? twoje 'Picasso' z funkcją oddzwaniania powinno działać. – chip

+0

masz na myśli Picasso.with (this) .load (imageLoad) .into (cel); – androidAhmed

Odpowiedz

16

nie testowałem swój kod, ale nawet jeśli to działa, plik actress_wallpaper.jpg nie jest załadowany w ImageView. W Dokumentach, mówi

Przedmioty wykonawcze do tej klasy muszą mieć implementację roboczą Object.equals(Object) i Object.hashCode() dla prawidłowego przechowywania wewnętrznie.

Spróbuj tego:

File file = new File(pathToFile); 

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

ostrzegamy nie testowałem mój kod.

Aktualizacja:

Próbowałem wersja 2.3.2 i 2.3.3, wydaje się, że istnieje problem https://github.com/square/picasso/issues/539

+0

kod działa w obrazie obciążenia, ale na pasku postępu wyświetlania do momentu załadowania obrazu nie zadziałał – androidAhmed

+0

wywołanie zwrotne jest wywoływane po załadowaniu bitmapy do widoku obrazu – chip

+0

pasek postępu działa dobrze, ale gdy dotknę ekranu, pokaże się obrazek –

1

To jest stare pytanie, ale może być to odpowiedź może pomóc innym, jak Miałem także problemy z wyświetlaniem paska postępu podczas ładowania obrazu z serwera.

Używam Picasso 2.4.0. i używam interfejsu Picasso Target do załadowania obrazu w widoku obrazu. Oto sprawdzony i działa kod:

Najpierw należy dodać następujące wiersze:

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

Oto implementacja interfejsu docelowego stosowany powyżej

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

Powyższy kod działa poprawnie, jeżeli są stosowane do załadunku obraz w aktywności. Ale jeśli chcesz załadować obraz w widoku gridview/recyclerview lub wyświetlić pager itp., Gdy używany jest ten sam uchwyt widoku, możesz uzyskać an issue, gdzie onBitmapLoaded() nie jest wywoływany (ponieważ widok jest przetwarzany, a Picasso tylko słabo odwołuje się do Target obiekt). Oto a link, aby rozwiązać ten problem.