Affichage d'une superposition de chargement sur L'écran Android

je cherche à afficher une superposition sur l'écran qui affiche un petit ticker de chargement ou peut-être même du texte pendant que mon application tente de se connecter au serveur. Mon écran de connexion est tout à l'intérieur d'une disposition linéaire verticale.

l'effet que j'essaie d'obtenir est quelque chose comme ceci: http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message

30
demandé sur Jonik 2013-08-02 19:48:21

5 réponses

peut-être trop tard, mais je pense que quelqu'un pourrait le trouver utile.

activité:

public class MainActivity extends Activity implements View.OnClickListener {

    String myLog = "myLog";

    AlphaAnimation inAnimation;
    AlphaAnimation outAnimation;

    FrameLayout progressBarHolder;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);

        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                new MyTask().execute();
                break;
        }

    }

    private class MyTask extends AsyncTask <Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            button.setEnabled(false);
            inAnimation = new AlphaAnimation(0f, 1f);
            inAnimation.setDuration(200);
            progressBarHolder.setAnimation(inAnimation);
            progressBarHolder.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            outAnimation = new AlphaAnimation(1f, 0f);
            outAnimation.setDuration(200);
            progressBarHolder.setAnimation(outAnimation);
            progressBarHolder.setVisibility(View.GONE);
            button.setEnabled(true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                for (int i = 0; i < 5; i++) {
                    Log.d(myLog, "Emulating some task.. Step " + i);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

mise en page xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start doing stuff"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Do Some Stuff"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <FrameLayout
        android:id="@+id/progressBarHolder"
        android:animateLayoutChanges="true"
        android:visibility="gone"
        android:alpha="0.4"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:layout_gravity="center" />
    </FrameLayout>
</RelativeLayout>
62
répondu Kostya Buts 2014-06-28 23:22:48

j'aime l'approche en Kostya, Mais la réponse de .

en S'appuyant sur cela, voici quelques idées pour faire la même superposition facilement réutilisable à travers votre application:

envisager de placer le FrameLayout de superposition dans un fichier de mise en page séparé, par exemple res/layout/include_progress_overlay :

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.4"
    android:animateLayoutChanges="true"
    android:background="@android:color/black"
    android:clickable="true"
    android:visibility="gone">

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminate="true"/>

</FrameLayout>

(une chose que j'ai ajouté dans le FrameLayout de superposition est android:clickable="true" . Ainsi, pendant que la superposition est montrée, il empêche les clics passant par les éléments UI sous elle. Au moins dans Mes cas d'utilisation typiques, c'est ce que je veux.)

puis l'inclure au besoin:

<!-- Progress bar overlay; shown while login is in progress -->
<include layout="@layout/include_progress_overlay"/>

et en code:

View progressOverlay;
[...]

progressOverlay = findViewById(R.id.progress_overlay);
[...]

// Show progress overlay (with animation): 
AndroidUtils.animateView(progressOverlay, View.VISIBLE, 0.4f, 200);
[...]

// Hide it (with animation): 
AndroidUtils.animateView(progressOverlay, View.GONE, 0, 200); 

avec code d'animation extrait dans une méthode util:

/**
 * @param view         View to animate
 * @param toVisibility Visibility at the end of animation
 * @param toAlpha      Alpha at the end of animation
 * @param duration     Animation duration in ms
 */
public static void animateView(final View view, final int toVisibility, float toAlpha, int duration) {
    boolean show = toVisibility == View.VISIBLE;
    if (show) {
        view.setAlpha(0);
    }
    view.setVisibility(View.VISIBLE);
    view.animate()
            .setDuration(duration)
            .alpha(show ? toAlpha : 0)
            .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            view.setVisibility(toVisibility);
        }
    });
}

(ici en utilisant view.animate() , ajouté dans API 12, au lieu de AlphaAnimation .)

46
répondu Jonik 2017-05-23 11:54:40

j'ai ProgressBar dans la disposition Relative et je le cache ou le montre respectivement. Et oui l'activité peut être transparente.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/hsvBackgroundContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
    </LinearLayout>


    <ProgressBar
        android:id="@+id/pbProgess"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
3
répondu Malachiasz 2013-08-02 15:55:02

un spinner avec un message au-dessus de l'application peut être créé en utilisant un ProgressDialog. Alors qu'il ne permet pas d'obtenir l'effet exact que dans l'image, c'est une bonne façon de montrer que l'application fonctionne.

3
répondu James Monger 2013-08-07 08:17:26

vous devez faire une opération de fond en utilisant le concept de thread comme AsyncTask. En utilisant ceci vous pouvez cacher le travail réel de la partie UI. Et AsyncTask ne sera pas affecté une fois vos opérations terminées.

  • créer une sous-classe D'AsyncTask
  • utiliser AsyncTask pour faire un travail de fond
  • Call on PreExecute () to initialize task
  • utilisez une barre de progression avec setIndeterminate (vrai) pour permettre à l' mode indéterminé
  • appelez onProgressUpdate () pour animer votre progressbar pour laisser l'utilisateur savoir que du travail est fait
  • utiliser incrementProgressBy () pour incrementprogressbar contenu par un valeur spécifique
  • appeler doInBackground () et faire le travail de fond ici
  • attraper un objet InterruptedException pour trouver la fin de l'arrière-plan opération
  • appeler onPostExecute () pour dénoter le fin de l'opération et montrer le résultat

tutoriel Android ProgressDialog

Splash screen au moment du chargement des ressources dans l'application android

-2
répondu Yash Krishnan 2017-05-23 12:18:06