如何在Android上显示启动画面3秒?

时间:2012-01-22 02:23:06

标签: android

我希望启动并停留3秒钟,然后消失并继续或替换为main.xml中的其余布局。

这是我的代码:

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>

10 个答案:

答案 0 :(得分: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);
            }
        });
}

答案 1 :(得分:15)

@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();

    }

答案 2 :(得分:4)

还有一个解决方案,您可以为SplashScreen创建不同的类,并将SplashScreen作为Launcher活动而不是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中,您可以编写如下代码:

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

在SplashScreen.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" />

然后检查

答案 3 :(得分:4)

使用Handler来保存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);
    }
}

答案 4 :(得分:1)

所以这样做的一个好方法是调用asynctask并让它等待3秒然后在postProgress上设置带有id splash的imageview到可见性消失。

所以这里有一些资源......

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

答案 5 :(得分:1)

为你的启动创建一个新的XML布局,在setContentView(R.layout.splash);下面称为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();
}

}

答案 6 :(得分: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();
            }
            };  
          }   
   }

答案 7 :(得分:0)

private void moveToHome(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashScreen.this, HomeScreen.class);
            startActivity(i);
            finish();
        }
    }, 4000);
}

答案 8 :(得分:0)

对于Kotlin:

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        Handler().postDelayed(Runnable {
            val i = Intent(this@SplashActivity, HomeScreen::class.java)
            startActivity(i)
            finish()
        }, 4000)


    }
}

答案 9 :(得分:0)

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*Duration of wait*/
        int SPLASH_DISPLAY_LENGTH = 10000;
        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);
    }
}