7
votes

Meilleure façon d'avoir un écran éclabousseur dans une application Android?

J'ai besoin d'un écran éclabousseur pour mon application. Essayé de créer une activité ayant l'image pour mon écran de splash; et essayé d'utiliser pour la boucle et la classe de minuterie pour introduire un délai. Mais ça ne marche pas de cette façon. Est-ce que je le fais mal; Si oui, quelle est la bonne façon?


3 commentaires

Qu'est-ce qui n'a pas fonctionné spécifiquement sur la minuterie? Cette approche devrait fonctionner. Pouvez-vous coller votre code?


Possible Duplicate Stackoverflow.com/Questtions/8642730/...


Vous devriez publier votre réponse si elle est différente de l'autre question ou supprimez le message s'il est identique.


6 Réponses :


1
votes

Vous pouvez simplement retarder? XXX


0 commentaires

3
votes

Essayez ceci xxx


0 commentaires

1
votes

Essayez ceci,

protected int _splashTime = 15000; 

private Handler handler;
private Runnable runnable; 

private Context context;


@Override
public void onCreate(Bundle savedInstance)
{
    super.onCreate(savedInstance);
    setContentView(R.layout.splash);   

    final SplashScreen sPlashScreen = this; 

    handler = new Handler();

     runnable = new Runnable() {
         @Override
            public void run() { 
             try {
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, _splashTime);
              }  
            finally {
                finish(); 
                //start a new activity

                //mtdCheckLicense();
                Intent main = new Intent();
                main.setClass(sPlashScreen, YourMainActivity.class);
                startActivity(main); 
                handler.removeCallbacks(runnable);

            }
        }
    }; 
    handler.postDelayed(runnable, 2000);
} 


0 commentaires

16
votes

Les solutions ci-dessus sont bonnes, mais si l'utilisateur appuie sur la touche arrière (et ferme votre application) avant la fin du délai de Splash. L'application ouvrira probablement toujours l'activité suivante, qui n'est pas vraiment conviviale.

C'est pourquoi je travaille avec un gestionnaire personnalisé et supprimez tous les messages en attente dans OnDestroy (). P>

public class SplashActivity extends Activity
{
    private final static int MSG_CONTINUE = 1234;
    private final static long DELAY = 2000;

    @Override
    protected void onCreate(Bundle args)
    {
        super.onCreate(args);
        setContentView(R.layout.activity_splash);

        mHandler.sendEmptyMessageDelayed(MSG_CONTINUE, DELAY);
    }

    @Override
    protected void onDestroy()
    {
        mHandler.removeMessages( MSG_CONTINUE );    
        super.onDestroy();
    }

    private void _continue()
    {
        startActivity(new Intent(this, SomeOtherActivity.class));
        finish();
    }

    private final Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch(msg.what){
                case MSG_CONTINUE:
                    _continue();
                    break;
            }
        }
    };
}


1 commentaires

Je pense que l'onpause est une meilleure option pour l'appel de Removemessages (). J'ai constaté que Ondestroy est appelé trop tard dans le cycle de vie et l'activité ultérieure apparaît après avoir cliqué sur le dos. +1 cependant :-)



0
votes

Le moyen le plus simple que je fais pour mon chaque projet ressemble à ceci:

public class SplashActivity extends Activity {
    protected boolean active = true;
    protected int splashTime = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(active && (waited < splashTime)) {
                        sleep(100);
                        if(active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // Start your Activity here
               }
           }
       };
       splashTread.start();    
   }
 //...


0 commentaires

0
votes

Avec les solutions décrites ici, vous perdez de temps, car ils mettent en pause l'initialisation de 2-3secondes avant de continuer.

ifree Ajout d'une mise en page d'écran code> sur mon Main_activity.xml code>. Je détecte le premier départ de l'application en étendant l'application. Si c'est le premier démarrage, j'affiche mon écran de splash pendant que l'interface utilisateur est la construction en arrière-plan ... (utilisez des threads d'arrière-plan si la barre de progression est à la traîne!) P>

//main_activity.xml
<?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" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- The main content view -->

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- The navigation drawer list -->

        <ListView
            android:id="@+id/slider_list"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/tvtv_background"
            android:choiceMode="singleChoice"
            android:divider="@drawable/nav_bar_divider"
            android:dividerHeight="1dp"
            android:listSelector="@android:color/transparent" />
    </android.support.v4.widget.DrawerLayout>

    <RelativeLayout
        android:id="@+id/splash_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@color/tvtv_white"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/splash_screen_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/splash_screen_text"
            style="@style/TVTextBlueContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_logo"
            android:layout_centerHorizontal="true"
            android:padding="10dp"
            android:text="Awesome splash shiat" />

        <ProgressBar
            android:id="@+id/splash_screen_loader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_text"
            android:layout_centerHorizontal="true"
            android:clickable="false"
            android:indeterminate="true" />
    </RelativeLayout>

</RelativeLayout>