Android 正确的闪屏方式。
来源:互联网 发布:mysql数据库5.5.20安装 编辑:程序博客网 时间:2024/05/28 20:18
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!
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.
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 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:
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 正确的闪屏方式。
- android 引入v7包的正确方式
- Android cookies正确的更新方式
- Android启动页面的正确打开方式
- Android签名文件的正确打开方式
- Android studio导入library的正确方式
- 查看Android API文档的正确方式
- 查看Android API文档的正确方式
- Android横竖屏切换正确实现方式
- 正确的思维方式
- 正确的思维方式
- 闪屏页的正确方式
- Android paint的drawText() 的正确使用方式
- Android RecyclerView 真正的布局刷新的正确方式
- android发送消息正确方式
- Android PopupWindow响应返回键最正确的实现方式
- Android 多种方式正确的加载图像,有效避免oom
- Android 多种方式正确的加载图像,有效避免oom
- DELPHI常用的VCL类简介
- 利用caffe pre-trained model进行图像分类
- 通用选择框的功能拓展
- laravel遇到的问题
- 5. Longest Palindromic Substring
- Android 正确的闪屏方式。
- Epoll模型详解
- Epoll模型讲解
- laravel的吐槽
- pmpi简单实例
- laravel眼瞎么?
- caffe中如何可视化cnn各层的输出
- permutations
- subsets