2012-01-22 53 views
6

我希望啓動圖像開始並保持3秒鐘,然後消失並繼續,或者用main.xml中的其餘佈局替換。如何在Android上顯示啓動屏幕3秒?

這是我的代碼:

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

請註明的答案接受。 –

回答

1

所以一個好的辦法做到這一點會打電話的AsyncTask,並讓它等待3然後在postProgress上設置imageview id濺到可見性消失。

因此,這裏有一些資源...

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

,如果需要,我可以進一步解釋。你也可能想考慮替代方案。我只是爲您當前的設置提供解決方案。

我決定把一些代碼....

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

然後在你的onCreate()實例化和執行,像這樣....

new SplashScreen().execute(splash); 
1

創建一個新的XML佈局你的色斑,在setContentView(R.layout.splash);以下稱爲飛濺。然後在飛濺之後製作一個新的活動,我將它稱爲ACTIVITYTWO,但您可以改變它。更改while (lTimer1 < 3000)中的數字以更改飛濺的長度,1000等於1秒。

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

有比啓動不必要的計時器好得多的方法。例如發佈延遲處理程序。 – Rotemmiz

+0

@Rotemmiz你有一些代碼,鏈接,答案? –

+0

看看Vikram Bodicherla的回答 – Rotemmiz

15

你可以做到這一點

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

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

或者,也許通過調用這個方法(從Android文檔)添加動畫,而不是設置知名度直接GONE

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

你不需要處理程序。只需使用'splash.postDelayed(...)' – kabuko

+0

@kabuko你是對的,編輯我的答案。謝謝:) –

+0

超級,你節省了我的時間。謝謝:) – 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

謝謝Tejaswini。你的答案簡單而有用 – mSafdel

+0

爲什麼你需要'onPause'方法的'finish'? –

0

試試這個

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

還有一個解決方案,您可以爲SplashScreen創建不同的類,並將SplashScreen創建爲啓動器活動,但不是MainActivity。 這樣的:

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

而且在SplashSacreen.java THN,你寫這樣的代碼:

 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在閃屏後。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" /> 

然後檢查

+0

這可能會導致內存泄漏,因爲您正在引用Activity上下文內部處理程序 –

1

使用處理器持有一段時間UI:

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); 
    } 
} 
相關問題