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

lil*_*lzz 8 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);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Vik*_*rla 15

你可以这样做

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

splash.postDelayed(new Runnable(){
  splash.setVisibility(View.GONE);
}, 3000);
Run Code Online (Sandbox Code Playgroud)

或者可以通过调用此方法(来自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);
            }
        });
}
Run Code Online (Sandbox Code Playgroud)


Tej*_*ini 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();

    }
Run Code Online (Sandbox Code Playgroud)


Ram*_*h R 6

使用处理程序将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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Mau*_*ycy 1

因此,一个好的方法是调用 asynctask 并让它等待 3 秒,然后在 postProgress 将带有 id 飞溅的 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的onCreate()实例化和执行中像这样......

new SplashScreen().execute(splash);
Run Code Online (Sandbox Code Playgroud)