应用冷启动画面显示方式(转)

原标题:Splash Screens the Right Way(Author:Chris Stewart

即启动时先显示应用图标,然后进行内容显示,先上效果:

YouTube splash screen

The very idea of the splash screen makes me a little angry. Just saying the phrase makes me cringe.

Splash screens just waste your time, right? As an Android developer, when I see a splash screen, I know that some poor dev had to add a three-second delay to the code.

Then, I have to stare at some picture for three seconds until I can use the app. And I have to do this every time it’s launched. I know which app I opened. I know what it does. Just let me use it!

What Google Recommends

You may be surprised to hear that Google advocates that you use a splash screen. It’s right here in the material design spec.

This wasn’t always the case; Google used to advocate against splash screens, and even called it an anti-pattern.

Adia no splash screen

What gives?

Splash Screens the Right Way

I believe that Google isn’t contradicting itself; the old advice and the new stand together. (That said, it’s still not a good idea to use a splash screen that wastes a user’s time. Please don’t do that.)

However, Android apps do take some amount of time to start up, especially on a cold start. There is a delay there that you may not be able to avoid. Instead of leaving a blank screen during this time, why not show the user something nice? This is the approach Google is advocating. Don’t waste the user’s time, but don’t show them a blank, unconfigured section of the app the first time they launch it, either.

If you look at recent updates to Google apps, you’ll see appropriate uses of the splash screen. Take a look at the YouTube app, for example.

YouTube splash screen

The amount of time you spend looking at this splash screen is exactly the amount of time it takes the app to configure itself. This is on a cold launch, too, which means this is the slowest launch possible. If the app is cached, the splash screen will go away almost immediately.

Implementing a Splash Screen

Implementing a splash screen the right way is a little different than you might imagine. The splash view that you see has to be ready immediately, even before you can inflate a layout file in your splash activity.

So you will not use a layout file. Instead, specify your splash screen’s background as the activity’s theme background. To do this, first create an XML drawable in res/drawable.

Note: all code below is available on GitHub.

<?xml version="1.0" encoding="utf-8"?>
<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>

Here, I’ve set up a background color and an image.

Next, you will set this as your splash activity’s background in the theme. Navigate to your styles.xml file and add a new theme for your splash activity:

<resources>

    <!-- Base application theme. -->
    <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>

</resources>

In your new SplashTheme, set the window background attribute to your XML drawable. Configure this as your splash activity’s theme in your 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, your SplashActivity class should just forward you along to your main activity:

public class SplashActivity extends AppCompatActivity {

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

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

Notice that you don’t even set up a view for this SplashActivity. The view comes from the theme. When you set up the UI for your splash activity in the theme, it is available immediately.

If you did have a layout file for your splash activity, that layout file would be visible to the user only after your app has been fully initialized, which is too late. You want the splash to be displayed only in that small amount of time before the app is initialized.

Doing it Right

With these steps completed, you will have a splash screen implemented the right way:

Sample splash screen

Armed with this knowledge, make your splash screen work the right way. Don’t waste the user’s time, but give them something nice to look at while they wait.

### 回答1: 你可以在 AndroidManifest.xml 文件中为启动 Activity 添加以下属性:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"。 这将使应用程序在启动时以全屏模式启动,并隐藏标题栏。如果你想要在运行时切换全屏模式,你可以在 Activity 中使用以下代码实现: ```java // 进入全屏模式 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN); // 退出全屏模式 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); ``` 请注意,在全屏模式下,用户无法通过系统UI导航栏访问 Android 操作系统的其他部分。因此,你需要确保你的应用程序提供了足够的导航和控件,以便用户能够轻松地从应用程序中导航。 ### 回答2: 冷启动全屏是指在Android应用程序启动时,将整个屏幕显示给用户观看。相比于热启动冷启动需要更多的时间和资源来加载应用程序的各种组件和数据。 当用户点击应用程序的图标或者通过其他方式打开应用程序时,Android系统会触发应用程序的冷启动过程。在冷启动过程中,系统首先会分配一定的内存资源给应用程序,并加载应用程序的主线程。接下来,系统会启动应用程序的主活动(Main Activity),并加载该活动所需的布局文件和资源文件。同时,系统还会加载应用程序的其他组件和数据,如服务、广播接收器、数据库等。 在冷启动全屏过程中,应用程序的主活动会占据整个屏幕,并显示一些加载中的动画或者展示应用程序的Logo,以提升用户体验。此时,用户通常会感觉到应用程序的启动时间较长,因为系统需要加载较多的资源和数据。 为了加快冷启动全屏的速度,开发者可以采取一些优化措施。例如,减少应用程序的启动时间,可以通过延迟加载某些资源或者使用异步加载方式来实现。另外,可以在冷启动过程中提供一些欢迎页面或者引导用户的内容,以增加用户的耐心和兴趣。 总之,冷启动全屏是Android应用程序启动一个过程,它需要加载大量的资源和数据,可能会导致一定的延迟。开发者可以通过一些优化方式来提升冷启动的速度和用户体验。 ### 回答3: 在Android中,冷启动全屏是指在应用程序的启动过程中,首先显示一个全屏的启动画面,然后再加载应用的主界面。 冷启动全屏一般用于提高应用启动体验,使用户在应用加载的过程中不会感到无聊或者等待时间过长,同时也能够展示应用的品牌形象或者其他相关信息。 实现冷启动全屏的主要步骤包括以下几个方面: 1. 设计启动画面:根据应用的主题或品牌形象,设计一个具有吸引力的启动画面画面应当简洁明了,同时也要考虑到在不同尺寸和分辨率的设备上显示的适配问题。 2. 设置启动画面:在Android的资源文件中,添加一个启动画面对应的布局文件,并将其设置为应用启动页。可以使用ImageView或者其他相应的控件来显示图片或者动画。 3. 添加延时加载:为了确保启动画面能够显示足够长的时间,需要在启动页的布局文件中添加一定的延时加载。可以通过Handler或者Timer等方式实现,在一定的时间后再跳应用的主界面。 4. 加载主界面:在延时加载结束后,通过Intent跳应用的主界面。同时,可以在此过程中添加过渡动画或者其他视效,以增加启动体验的流畅性和美观性。 总之,冷启动全屏在一定程度上提升了用户体验,使应用启动过程不再枯燥乏味,同时也能够突出应用的品牌形象。然而,需要注意的是,在实现过程中还需要考虑到设备适配、加载时间和用户等待等方面的问题,以保证最佳的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值