2016-05-16 2 views
1

Ich habe diese Funktion, die Bild von URL laden. Die einzige Sache ist, dass es keinen Fortschritt beim Laden des Bildes zeigt, nur der Bildschirm wird dunkler und nach einiger Zeit wird nur das Bild angezeigt. Dies lässt den Benutzer denken, dass es nicht funktioniert. Wie kann ich den Fortschrittsdialog hinzufügen, während das Bild geladen wird oder wie das Bild geladen wird?ANDROID Fortschrittsdialog beim Laden von Bild im Dialog/Popup

public void showImage() { 
    Dialog builder = new Dialog(this); 
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    builder.getWindow().setBackgroundDrawable(
      new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 
     @Override 
     public void onDismiss(DialogInterface dialogInterface) { 
      //nothing; 
     } 
    }); 

    ImageView imageView = new ImageView(this); 
    Picasso.with(this).load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png").into(imageView); 
    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT)); 
    builder.show(); 
} 
+0

Mögliche Duplikat [Animation Laden Bild in picasso] (http: //stackoverflow.com/questions/24826459/animated-loading-image-in-picasso) –

+0

die '.into()' Methode hat die sek ond Argument als Callback, um zu überprüfen, ob das Bild geladen ist. Hast du es versucht ? – vtproduction

Antwort

2
public void showImage() { 
    Dialog builder = new Dialog(this); 
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    builder.getWindow().setBackgroundDrawable(
      new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 
     @Override 
     public void onDismiss(DialogInterface dialogInterface) { 
      //nothing; 
     } 
    }); 

    final ProgressDialog progressDialog = new ProgressDialog(this); 
    ImageView imageView = new ImageView(this); 
    Picasso.with(this) 
      .load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png") 
      .into(imageView, new com.squareup.picasso.Callback() { 
       @Override 
       public void onSuccess() { 

        // The image has loaded you can make the progress bar invisible 
        if (progressDialog.isShowing()) 
         progressDialog.dismiss(); 
       } 

       @Override 
       public void onError() { 
        // Show some error message 
        if (progressDialog.isShowing()) 
         progressDialog.dismiss(); 
       } 
      }); 


    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT)); 
    builder.show(); 

    progressDialog.setMessage("please wait"); 
    progressDialog.setCancelable(false); 
    progressDialog.show(); 
} 
+0

Ich muss den 'progressDialog' als' final' deklarieren, aber dann kann 'setVisibility' nicht aufgelöst werden. –

+0

Nun heißt es, Methode überschreibt die Methode nicht von ihrer Oberklasse. –

+0

wo kommt dieser Fehler? Ich meine, auf welcher bestimmten Linie? –

4

versuchen Sie Platzhalter diese Ordner-Layout hinzufügen

<RelativeLayout 
android:id="@+id/hoteldetails_myContainer" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 


<ProgressBar 
    android:id="@+id/hoteldetails_progressBar" 
    style="?android:attr/progressBarStyleSmall" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:visibility="gone" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_marginTop="20dp" 
    android:layout_marginBottom="20dp" /> 

<ImageView 
    android:id="@+id/hoteldetails_imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scaleType="fitXY"/> 
</RelativeLayout> 

und dies Ladecode

Picasso.with(this) 
     .load(hoteImageStr) 
     .error(R.drawable.loading) 
     .into(img, new com.squareup.picasso.Callback() { 
      @Override 
      public void onSuccess() { 
       if (progress != null) { 
        progress .setVisibility(View.GONE); 
       } 
      } 

      @Override 
      public void onError() { 

      } 
     }); 
Verwandte Themen