在安卓应用中显示闪屏[英] Showing Splash screen in android apps

本文是小编为大家收集整理的关于在安卓应用中显示闪屏的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

您是否曾经在启动第一个活动之前看到许多应用程序中的页面,并且以某种方式就像一个等待页面?我是Android的初学者,当我单击应用程序时,首先出现一个空白的白页,然后在3秒后主要活动出现.但是在许多应用程序中,在第一个活动之前有一种定制的页面,就像进度条或STH一样.在启动我的应用程序之前,我该如何自定义不那么漂亮的空白页?提前.

推荐答案

您正在谈论的技术从技术上讲称为 splash屏幕.

Splash屏幕用于显示冷启动,我们可以在其中准备好运行应用程序的东西. Google提倡使用它.

几乎没有显示飞溅屏幕的方法.

  1. 您可以使用CountDownTimer这样.在SplashActivity.java

    private int TIME_OUT = 3000;

    CountDownTimer countDownTimer = new CountDownTimer(TIME_OUT, TIME_OUT) {
    
    @Override
    public void onTick(long millisUntilFinished)
    {
        // Leave this
    }
    
    @Override
    public void onFinish()
    {
        // Start your app main activity
        Intent i = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(i);
    
        // close this activity
        finish();
    }
    

    }.start();

  2. 您可以使用Handler这样.在SplashActivity.java

    private static int TIME_OUT = 3000;
    
    new Handler().postDelayed(new Runnable() 
    {
        @Override
        public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
    
                finish();
            }
    

    },time_out);

  3. 使用主题 - 正确的方法

添加background_splash.xml drawable文件夹中的文件

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/gray"/>
    <item>
       <bitmap
          android:gravity="center"
          android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

在styles.xml

中添加这些行
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

在AndroidManifest.xml

中添加这些行
<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

最终添加一个称为SplashActivity.java的活动并添加这些代码

public class SplashActivity extends AppCompatActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

其他推荐答案

您可以使用飞溅屏幕 工作代码:

public class Mainsplash extends Activity {


     /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 3000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splashscreen);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);




        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Mainsplash.this,MainActivity.class);
                Mainsplash.this.startActivity(mainIntent);
                Mainsplash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/load_src"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashimg"
    android:gravity="center"
    android:orientation="vertical" >

  <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
      android:layout_alignParentBottom="true"
         >


        <ProgressBar
            android:id="@+id/prg"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>
    </RelativeLayout>




</LinearLayout>

以及清单文件

<activity
            android:name=".Mainsplash"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

使用Intent Filter进行该飞溅活动,使其首先启动

希望这会有所帮助

本文地址:https://www.itbaoku.cn/post/1938015.html

问题描述

Have you ever seen the page in many apps that before launching the first activity appears and somehow it is like a waiting page ?! I am beginner to android and when I click on my apps , first a blank white page appears then after 3 seconds main activity comes out . but in many apps there is a kind of customised page before the first activity that is like a progress bar or sth . how can I customise that not so beautiful blank page before starting my app?! Thanx in advance .

推荐答案

Technically thing you are talking about is called splash screen.

Splash screen is used to show cold start where we can make our things ready to run an application. Google advocated to use it.

There are few approaches to show splash screen.

  1. You can use CountDownTimer like this. In SplashActivity.java

    private int TIME_OUT = 3000;

    CountDownTimer countDownTimer = new CountDownTimer(TIME_OUT, TIME_OUT) {
    
    @Override
    public void onTick(long millisUntilFinished)
    {
        // Leave this
    }
    
    @Override
    public void onFinish()
    {
        // Start your app main activity
        Intent i = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(i);
    
        // close this activity
        finish();
    }
    

    }.start();

  2. You can use Handler like this. In SplashActivity.java

    private static int TIME_OUT = 3000;
    
    new Handler().postDelayed(new Runnable() 
    {
        @Override
        public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
    
                finish();
            }
    

    }, TIME_OUT);

  3. Using theme - correct way of doing

Add background_splash.xml file in drawable folder

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/gray"/>
    <item>
       <bitmap
          android:gravity="center"
          android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

Add these lines in styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

Add these lines in AndroidManifest.xml

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Finally add an activity called SplashActivity.java and add these code

public class SplashActivity extends AppCompatActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

其他推荐答案

You can use the Splash Screen for this Working code:

public class Mainsplash extends Activity {


     /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 3000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splashscreen);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);




        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Mainsplash.this,MainActivity.class);
                Mainsplash.this.startActivity(mainIntent);
                Mainsplash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

}

Xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/load_src"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashimg"
    android:gravity="center"
    android:orientation="vertical" >

  <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
      android:layout_alignParentBottom="true"
         >


        <ProgressBar
            android:id="@+id/prg"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>
    </RelativeLayout>




</LinearLayout>

And also in Manifest file

<activity
            android:name=".Mainsplash"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

use the intent filter for that splash activity to make it launch first

hope this helps