如何在Android上显示启动屏幕3秒钟? [英] How to show a splash screen for 3 seconds on Android?

查看:90
本文介绍了如何在Android上显示启动屏幕3秒钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like the splash image to begin and stay for 3 seconds, and then disappear and continue or be replaced with the rest of the layout in the main.xml.

这是我的代码:

Main.java

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>

推荐答案

您可以这样做

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

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

或者通过调用此方法(从Android文档中)来添加动画,而不是直接将可见性设置为GONE

Or maybe add an animation by invoking this method (from the Android docs) instead of setting visibility to GONE directly

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

这篇关于如何在Android上显示启动屏幕3秒钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆