2012-01-22 16 views
6

Vorrei che l'immagine splash iniziasse e restasse per 3 secondi, quindi scomparire e continuare o essere sostituita con il resto del layout nel file main.xml.Come mostrare una schermata iniziale per 3 secondi su Android?

Questo è il mio codice:

Main.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main); 

    ImageView splash = (ImageView) this.findViewById(R.id.splash); 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- margin=0px, padding=20px --> 
<!--textview padding=10dp, textSize=16sp--> 
<!--px=pixel, dp=density indepen sp=scale indepen fontsize preference --> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageView 
    android:id="@+id/splash" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/splash2"/> 

    <ImageView 
    android:id="@+id/myImageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/bg_main"/> 

    <ImageView 
    android:id="@+id/myImageView0" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/bar_top"/> 

<!-- 
    <TextView android:id="@+id/textItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:paddingTop="10dp" 
    android:paddingLeft="110dp" 
    android:background="#00000000" 
    android:textColor="#ffffffff" 
    android:textSize="22sp" 
    android:text="Find Car" 
    android:enabled="false" 
    > 
--> 

<TabHost android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="3dp"> 
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom = "@android:id/tabcontent" 
     /> 
    </RelativeLayout> 
    </TabHost> 
</RelativeLayout> 
+2

Si prega di indicare una risposta accettata. –

risposta

1

Quindi un buon modo per farlo sarebbe quello di chiamare AsyncTask e farlo aspettare 3 secondi poi su postProgress imposta la visualizzazione di immagini con id splash a visibilità sparita.

Così qui sono alcune risorse ...

http://developer.android.com/reference/android/os/AsyncTask.html

posso spiegare ulteriormente se necessario. anche tu potresti voler prendere in considerazione delle alternative. Stavo semplicemente offrendo una soluzione per la configurazione attuale.

ho deciso di includere un codice ....

private class SplashScreen extends AsyncTask<ImageView, Void, Void> { 
    ImageView imgView; 
    protected Void doInBackground(ImageView... view) { 
     imgView = view[0]; 
     wait(3000); // not sure if this works but u can fo a while loop etc if not 
    } 

    protected void onPostExecute(Long result) { 
     imgView.setVisibility(ImageView.GONE); 
    } 
} 

Poi, nel tuo onCreate() instantiate ed eseguire in questo modo ....

new SplashScreen().execute(splash); 
1

Fai un nuovo layout XML per il vostro schizzi, chiamato splash di seguito in setContentView(R.layout.splash);. Poi fai una nuova attività da giocare dopo lo splash, l'ho chiamato ACTIVITYTWO qui sotto, ma puoi cambiarlo. Modificare il numero in while (lTimer1 < 3000) per modificare la lunghezza dello splash, con 1000 uguale a 1 secondo.

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Window; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.splash); 

    Thread lTimer = new Thread() { 

     public void run() { 

      try { 
       int lTimer1 = 0; 
       while (lTimer1 < 3000) { 
        sleep(100); 
        lTimer1 = lTimer1 + 100; 
       } 
       startActivity(new Intent("com.example.ACTIVITYTWO")); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      finally { 
       finish(); 
      } 
     } 
    }; 
    lTimer.start(); 
} 

} 
+0

Ci sono modi molto migliori rispetto all'avvio di un timer non necessario. ad esempio i gestori post delayed. – Rotemmiz

+0

@Rotemmiz hai qualche codice, link, risposta? –

+0

dai un'occhiata alla risposta di Vikram Bodicherla – Rotemmiz

15

È possibile farlo

ImageView splash = (ImageView) this.findViewById(R.id.splash); 

splash.postDelayed(new Runnable(){ 
    splash.setVisibility(View.GONE); 
}, 3000); 

O forse aggiungere un'animazione invocando questo metodo (dalla documentazione Android) invece di impostare la visibilità per GONE direttamente

private void fadeSplashOut() { 
    // Set the content view to 0% opacity but visible, so that it is visible 
    // (but fully transparent) during the animation. 
    mContentView.setAlpha(0f); 
    mContentView.setVisibility(View.VISIBLE); 

    // Animate the content view to 100% opacity, and clear any animation 
    // listener set on the view. 
    mContentView.animate() 
     .alpha(1f) 
     .setDuration(mShortAnimationDuration) 
     .setListener(null); 

    // Animate the loading view to 0% opacity. After the animation ends, 
    // set its visibility to GONE as an optimization step (it won't 
    // participate in layout passes, etc.) 
    splash.animate() 
     .alpha(0f) 
     .setDuration(mShortAnimationDuration) 
     .setListener(new AnimatorListenerAdapter() { 
      @Override 
      public void onAnimationEnd(Animator animation) { 
       splash.setVisibility(View.GONE); 
      } 
     }); 
} 
+0

Non hai bisogno del gestore. Basta usare 'splash.postDelayed (...)' – kabuko

+0

@kabuko Hai ragione, modificato la mia risposta. Grazie :) –

+0

Super, hai salvato il mio tempo. Grazie :) – Naruto

14
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    //Sets the layout of welcome_screen.xml 
    setContentView(R.layout.welcome_screen); 
    Thread timer= new Thread() 
    { 
     public void run() 
     { 
      try 
      { 
       //Display for 3 seconds 
       sleep(3000); 
      } 
      catch (InterruptedException e) 
      { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 
      finally 
      { 
       //Goes to Activity StartingPoint.java(STARTINGPOINT) 
       Intent openstartingpoint=new Intent("x.y.z.START"); 
       startActivity(openstartingpoint); 
      } 
     } 
    }; 
    timer.start(); 
} 


//Destroy Welcome_screen.java after it goes to next activity 
@Override 
protected void onPause() 
    { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    finish(); 

    } 
+1

grazie Tejaswini. la tua risposta è semplice e utile – mSafdel

+0

perché hai bisogno di 'finish' sul metodo' onPause'? –

0

prova questo

public class Welcome extends Activity 
{ 
/** Called when the activity is first created. */ 
    Handler mHandler,actHandler;   

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.welcome); 

     new Thread(){ 
      public void run(){ 
      try{     
       Thread.sleep(3000);     
       }        
      catch(Exception ex){ 

       Log.e("Welcome Exception :",ex.toString()); 
       } 
        try{ 
        Message msg=mHandler.obtainMessage(); 
        mHandler.sendMessage(msg);  
        } 
        catch(NullPointerException ex){ 
        Log.e("Handler Exception :",ex.toString());               
        }      
        } 

     }.start(); 
      mHandler=new Handler(){ 
      public void handleMessage(Message msg){ 
      super.handleMessage(msg);     


      Intent i=new Intent(Welcome.this,M_chat.class); 
      startActivity(i); 
      finish(); 
      } 
      }; 
      } 
    } 
3

C'è una soluzione in più per questo, è possibile creare classi diverse per SplashScreen e rendere SplashScreen come attività Launcher ma non MainActivity. Like This:

 <activity 
       android:name=".SplashScreen" 
       android:label="@string/title_activity_splash_screen" 
       android:screenOrientation="portrait" 
       android:theme="@style/AppTheme.NoActionBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 

     <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     </activity> 

E thn in SplashSacreen.java, si scrive il codice come questo:

 public class SplashScreen extends AppCompatActivity { 


private static int SPLASH_TIME_OUT = 3000; 

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

    new Handler().postDelayed(new Runnable() { 


     @Override 
     public void run() { 
      // This method will be executed once the timer is over 
      // Start your app main activity 
      Intent i = new Intent(SplashScreen.this, MainActivity.class); 
      startActivity(i); 

      // close this activity 
      finish(); 
     } 
    }, SPLASH_TIME_OUT); 
    } 
} 

Thn dopo in SplashScreen.file xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/holo_red_dark" > 

<ImageView 
    android:id="@+id/imgLogo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/comp_logo" /> 

e controllarlo

+0

Ciò potrebbe causare perdite di memoria mentre si fa riferimento al contesto di attività all'interno del gestore –

1

Usa Handler per contenere l'interfaccia utente per un certo tempo:

public class SplashActivity extends Activity { 

    /*Duration of wait*/ 
    private final int SPLASH_DISPLAY_LENGTH = 2000; 

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

     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       /* Create an Intent that will start the MainActivity. */ 
       Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); 
       startActivity(mainIntent); 
       finish(); 
      } 
     }, SPLASH_DISPLAY_LENGTH); 
    } 
} 
Problemi correlati