本文主要是介绍Android 启动页-解决图片被拉伸和压缩问题,适配虚拟导航栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android 启动页设置非常简单
//styles.xml 设置主题<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"><item name="android:windowBackground">@drawable/bg_splash</item><item name="android:windowFullscreen">true</item></style>//activity使用主题,这时点击app图标,就会显示@drawable/bg_splash图片<activity android:name=".MainActivity"android:theme="@style/SplashTheme"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity>
不过这么设置会存在启动页图片被拉伸和压缩的问题,比如
解决图片变形的问题,可以使用bg_splash.xml文件创建drawable代替图片
//使用layer-list添加多个图层,返回LayerDrawable
//注意:bitmap使用的图片大小要小于屏幕大小
<?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><itemandroid:bottom="10dp"><bitmapandroid:gravity="bottom"android:src="@drawable/ic_bg_splash"/></item>
</layer-list>
运行效果图:


图片被虚拟导航栏遮住了, 适配:
//android:windowDrawsSystemBarBackgrounds属性是API 21以上的,需要放在values-v21文件夹里 <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>
运行效果图:
Demo下载
这篇关于Android 启动页-解决图片被拉伸和压缩问题,适配虚拟导航栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!