2012-01-22 34 views
6

Chciałbym rozpocząć obraz powitalny i pozostać przez 3 sekundy, a następnie zniknąć i kontynuować lub zostać zastąpiony resztą układu w pliku main.xml.Jak wyświetlać ekran powitalny przez 3 sekundy na Androidzie?

To jest mój kod:

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

proszę zaznaczyć odpowiedź jako zaakceptowany. –

Odpowiedz

1

Dlatego dobrym sposobem na to byłoby nazwać asynctask i mieć go czekać 3 sekund następnie na postProgress ustawić widok obrazu z id splash do widoczności minęło.

Tak oto niektóre środki ...

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

mogę wyjaśnić dodatkowo w razie potrzeby. również możesz chcieć rozważyć alternatywy. Po prostu oferowałem rozwiązanie dla twojej obecnej konfiguracji.

postanowiłem zawierać jakiś kod ....

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

Następnie w onCreate() instancji i wykonać jak tak ....

new SplashScreen().execute(splash); 
1

Dodać nowy układ XML dla powitalny, o nazwie splash poniżej w setContentView(R.layout.splash);. Następnie wykonaj nową czynność do odtworzenia po rozbiciu, nazwałem ją ACTIVITYTWO poniżej, ale możesz to zmienić. Zmień liczbę w while (lTimer1 < 3000), aby zmienić długość splash, z 1000 równą 1 sekundę.

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

Istnieje wiele lepszych sposobów niż uruchomienie niepotrzebnego timera. post opóźnione procedury obsługi, na przykład. – Rotemmiz

+0

@Rotemmiz czy masz jakiś kod, link, odpowiedź? –

+0

spójrz na odpowiedź Vikram Bodicherla – Rotemmiz

15

Można to zrobić

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

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

Albo dodać animację poprzez wywołanie tej metody (od Androida docs) zamiast ustawiania widoczności Gone bezpośrednio

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

Nie potrzebujesz pomocy. Po prostu użyj 'splash.postDelayed (...)' – kabuko

+0

@kabuko Masz rację, zredagowałem moją odpowiedź. Dzięki :) –

+0

Super, zaoszczędziłeś mój czas. Dzięki :) – 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

dzięki Tejaswini. twoja odpowiedź jest prosta i przydatna – mSafdel

+0

dlaczego potrzebujesz 'finish' w metodzie' onPause'? –

0

spróbuj tego

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

Istnieje jeszcze jedno rozwiązanie, możesz utworzyć inną klasę dla SplashScreen i utworzyć SplashScreen jako aktywność w programie Launcher, ale nie w MainActivity. tak:

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

I thn w SplashSacreen.java, można napisać kod jak poniżej:

 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 po w ekran powitalny.plik 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" /> 

a następnie sprawdzić go

+0

Może to prowadzić do przecieków pamięci, gdy odwołujesz się do kontekstu działania wewnątrz obsługi –

1

Używaj Handler trzymać UI na jakiś czas:

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); 
    } 
} 
Powiązane problemy