解决安卓启动页图片拉伸问题
2020-11-12 本文已影响0人
蓝不蓝编程
背景
不少App启动时,会出现长时间白屏,为了避免白屏的不悦体验,一方面肯定是提升启动速度,另一方面则是提供一个启动页面,让画面感填充无聊的等待.
解决方案
- 新建SplashActivity
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(Intent(this,MainActivity::class.java))
}
}
- 增加启动图bg_splash.xml
其中splash
为真正的图片
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#FFFFFF" />
</shape>
</item>
<item android:bottom="10dp">
<bitmap
android:gravity="bottom"
android:src="@drawable/splash" />
</item>
</layer-list>
- styles.xml文件中增加theme定义
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/bg_splash</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
- 在Manifest文件中,对activity指定theme
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
PS:对于启动图,除了上面在bg_splash.xml通过layer叠加避免图片拉伸,还可以采用点九图.疗效是一致的.
完整源代码
https://gitee.com/cxyzy1/splash-demo
附录
https://blog.csdn.net/u010218170/article/details/92437667
https://blog.csdn.net/u011418943/article/details/88537446
https://yanzhenjie.blog.csdn.net/article/details/52201896