版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010356768/article/details/89633832
实现
创建欢迎页SplashActivity
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
SplashActivity.this.fileList();
}
},3000);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
return super.onKeyDown(keyCode, event);
}
}
欢迎页文件activity_splash
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/splash"
android:scaleType="centerCrop"/>
</LinearLayout>
主界面MainActivity和布局文件activity_main这里不写了
AndroidManifest设置SplashActivity为启动的activity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xx.myapplication">
......
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity android:name=".SplashActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
</application>
</manifest>
主题AppTheme去掉标题栏
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
......
<item name="windowNoTitle">true</item>
</style>
效果图
白屏问题处理
当我们点开app会先进入短暂的空白页面再进入欢迎页面
原因
- 在Activity启动onCreate()方法,执行setContentView()时出现白屏
- 页面的窗体绘制先于资源加载,这个时候就会出现短暂的白屏,也就是说还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景
<style name="ThemeSplash" parent="Theme.AppCompat.Light">
这种亮色系造成了白色闪屏
<style name="ThemeSplash" parent="ThemeOverlay.AppCompat.Dark">
这种亮色系造成了黑色闪屏
<item name="android:windowBackground">@color/white</item>
归根结底也就是这个属性决定了白屏或黑屏的颜色
解决办法
我们要想办法让这个黑色的背景变成用户喜欢看到的画面或者让它透明化。所以,我们有下面两种方案:
- 自定义SplashActivity的Theme
就是把出现的黑屏通过android:windowBackground属性设置为我们想要的背景
<style name="SplashTheme" parent="android:Theme">
<item name="android:windowBackground">@mipmap/splash</item>
<item name="android:windowNoTitle">true</item>
</style>
<activity android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
使用这种方法的话,SplashActivity中的setContentView()可以去掉了
setContentView(R.layout.activity_splash);
- 使用透明主题android:Theme.Translucent.NoTitleBar.Fullscreen
使用透明主题,点图标后在桌面上要等上那么一小会儿,然后是整个界面才会一下子显示出来,让人误以为慢的是桌面launcher而不是应用
<style name="SplashTheme"
parent="android:Theme.Translucent.NoTitleBar.Fullscreen">
</style>
使用同上