Android App 启动图片的坑

Android App启动时默认是空白界面,等负责启动的Activity准备好后才显示对应的layout.

对此有个众所周知的简单办法解决,即在负责启动的Activity的theme自定义一下,里边加上一张启动图片:

在AndroidManifest.xml中

<activity
    android:label="@string/app_name"
    android:theme="@style/Theme.AppStartLoad"
    android:name=".StartupActivity" >
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

在styles.xml中

name与上述代码的theme一致

<style name="Theme.AppStartLoad" parent="Theme.AppCompat.DayNight.NoActionBar">
		<item name="android:windowBackground">@drawable/loading</item>
		<item name="android:windowNoTitle">true</item>
		<item name="android:statusBarColor">@color/transparent</item>
		<item name="android:navigationBarColor">@color/transparent</item>
	</style>

然后准备一张图片,文件名为 loading.png即可(与上述代码的windowBackground一致)

所说的坑:

这个loading.png的长宽是有要求的,如果太小,启动界面只显示此图片那么大的区域(这个我是今天刚发现,原来是随便做了张图,w*h = 1080*1920约72KB,觉得比较大想改小点节省点size,结果发现了这个问题)

然后,我想到一些大屏手机乃至平板,图片太小了岂不是遮挡了,所以我就长宽各翻1倍,结果大跌眼镜:手机启动画面显示后,启动失败了!(设备是小米青春10)

Logcat提示:

java.lang.RuntimeException: Canvas: trying to draw too large(122412096bytes) bitmap.

好家伙,一张2160*3840的图片约213KB,系统很实诚地按真彩绘制,内存122M!

所以,这个loading.png的长宽,既不能太大,也不能太小。但安卓设备长宽那可太丰富多彩了,你设的大,就有一部分设备启动不了,你设的小,就有一部分把主界面遮挡到边边角角。

解决办法:下文的第2种方法,可以使用很小的图片,图片周围用一个纯色shape填色。

对于本app而言,其中bitmap的gravity设置为center即可,不用设置top之类的

解决 Android APP 启动页白屏问题及如何实现全屏显示_android 启动白屏_思涛的博客的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/piggy514/article/details/132270106
今日推荐