应用启动时显示标志几秒钟

13

在应用程序开始和菜单显示之前,我想显示标志几秒钟。 我还想在它消失时使用一些动画效果。 我应该创建一个新的 Activity 吗? 还是可以在布局中设置?

我想在应用程序启动和菜单可见之前显示标识几秒钟,并在其消失时使用一些动画效果。 我应该创建一个新的 Activity 还是可以在布局中设置?请保留 HTML 标记。

这可能会对你有所帮助:http://www.barebonescoder.com/2010/04/a-simple-android-splash-screen/ - user2443607
喜欢启动画面吗?这里有一个例子。 - Stefan Bossbaly
7个回答

19

定义一个包含您的标识的启动屏幕布局,然后将此代码添加到您的活动中:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    //display the logo during 5 seconds,
    new CountDownTimer(5000,1000){
        @Override
        public void onTick(long millisUntilFinished){} 

        @Override
        public void onFinish(){
               //set the new Content of your activity
               YourActivity.this.setContentView(R.layout.main);
        }
   }.start();
}

这不太好,因为应用程序可以在5秒内运行得更快。 - Konstantin Konopko
@KonstantinKonopko 这取决于您在应用程序启动时加载的内容。您可以使用带有标志的 SplashScreen,并在后台运行 AsyncTask 以便于应用程序的启动或加载,因此如果应用程序运行快速,则标志将显示直到 onPostExecute 被调用。 - Houcine

3

您可以使用仅样式的方法在启动时使用闪屏标志。不幸的是,它只能在API 23及以上版本中使用。但是您无需管理闪屏Activity。

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:windowBackground">@drawable/logo_startup</item>
        <item name="android:windowNoTitle">true</item>
    </style>

res/drawable-v23/logo_startup.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/PageBackground"/>

    <item android:width="@dimen/logo_startup" android:height="@dimen/logo_startup" android:gravity="center">
        <bitmap android:src="@drawable/logo"/> //use PNG file here, not vector
    </item>

</layer-list>

res/drawable/logo_startup.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/PageBackground"/>

</layer-list>

2

您可以使用设置setVisibility(Visibility.GONE)的图像视图,或者编写一个在时间结束后弹出并退出的活动。这取决于您个人的喜好...


0

为什么?用户不喜欢等待。但是,如果你需要等待因为你正在加载一些数据,你可以:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    /* Do some work in a new thread, calling setContentView at the end with your view */
}

0

活动文件:

package com.karan.android.video;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
 
public class splash extends Activity 
{
   
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.splash);
      Thread splashThread = new Thread() 
      {
         @Override
         public void run() 
         {
            try {
               int waited = 0;
               while (waited < 3000)
               {
                  sleep(100);
                  waited += 100;
               }
            } catch (InterruptedException e)
            {
               // do nothing
            } finally 
            {
               finish();
               Intent i = new Intent(splash.this,video.class);
               startActivity(i);
            }
         }
      };
      splashThread.start();
   }
}

Xml文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   
     <ImageView           
         android:src="@drawable/buff"       
         android:id="@+id/ImageView01"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
      </ImageView>
   
    <TextView 
    android:textSize="40dp"
    android:textColor="#CCFF00"    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"  
    android:text="Buffering..."
    /> 

    
</FrameLayout>

0

延迟执行可以用更简单的方式实现:

new Handler().postDelayed(new Runnable() {
  // ... Hide splash image and show the real UI
}, 3000)

同时,Android标准的android.widget.ViewSwitcher类非常适用于这种情况。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接