Android 正确的闪屏方式。
2016-04-01 04:12
806 查看
From
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
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!
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.
What gives?
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.
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 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.
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:
In your new
Finally, your
Notice that you don’t even set up a view for this
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.
With these steps completed, you will have a splash screen implemented the right way:
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.
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
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 inthe material design spec.
This wasn’t always the case; Google used to advocate against splash screens, and even called it an anti-pattern.
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.
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
SplashActivityclass 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: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.
相关文章推荐
- Android WIFI模块分析
- Android之Wifi学习(2)------连接Wifi
- Android之Wifi学习(1)
- OnItemClickListener 没响应的根本原因-源码解析
- Android学习之高仿知乎首页
- Android消息机制Handler解析(源码+Demo)
- android 实现刮刮乐刮奖效果
- Android Studio Git 使用总结
- Android 6.0.x 系统安装xposed框架
- 设计模式-迭代器模式Interator--摘自武奇老师博客160305
- 图片3D翻转效果 --摘自李硕老师博客160305
- Android 常见的坑
- Android-全屏动画
- 读取Android sd卡的文件
- android获取外置SD卡的路径
- Android开发学习笔记《一》
- Android四大组件
- Android竖屏模式实现横屏效果
- 关于Android布局优化(二)
- Android 中在一个程序中启动另一程序