App启动时白屏&使用BitmapDrawable解决启动页背景图片拉伸

一,App启动时,会白屏或者黑屏,采用如下方法解决:

1.启动页的Activity设置style

        <activity
            android:name=".activity.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Start">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

2. Activity样式 Theme.Start 代码如下:

 <style name="Theme.Start" parent="Theme.AppCompat.Light.NoActionBar">
        <!--设置背景颜色-->
        <item name="android:background">@drawable/splash</item>
        <!-- 隐藏标题栏 -->
        <item name="windowNoTitle">true</item>
        <!-- 隐藏状态栏 -->
        <item name="android:windowFullscreen">true</item>
    </style>

问题:如果没有启动页Activity,给Application设置background是不是也可以去掉白屏呢?

二,BitmapDrawable解决启动页背景图片拉伸

上面的background属性为一张启动页图片,发现在pad上显示启动页的图片会拉伸变形。ImageView可以通过ScaleType属性设置图片src的缩放模式,可惜activity的background没有类似设置缩放模式属性。

但是我们可以从图片下手(一开始使用.9图片,但是小米pad(android4.4.4)上splash页面就缩小了,不知道为什么,可能哪里操作有误或者不兼容。)使用BitmapDrawable,对原来的图片进行包装,来解决图片拉伸问题。

BitmapDrawable 知识参考: https://blog.csdn.net/u014005316/article/details/53509179

代码如下 splash_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:gravity="clip_horizontal|clip_vertical"
        android:dither="true"
        android:src="@drawable/splash"
        android:tileMode="disabled"/>
    <!--属性解释-->
    <!--android:src 图片资源id-->
    <!--android:antialias是否开启抗锯齿,应该开启-->
    <!--android:dither是否开启抖动效果,应该开启-->
    <!--android:filter是否开启过滤效果,应该开启-->
    <!--android:gravity对图片进行定位-->
    <!--android:mipMap纹理映射,一般为false-->
    <!--android:tileMode平铺模式四个值:disabled(默认值即关闭平铺模式)、clamp、repeat、mirror-->

将Theme.Start的background属性值改为splash_bg。

注意:背景图片splash_bg设置给background属性后,需要再给windowBackground设置为图片的底色。因为背景图片splash_bg是多大显示多大,不会拉伸,周围未填充的区域会是有白色或黑色。

修改后的Theme.Start,代码如下:

   <style name="Theme.Start" parent="Theme.AppCompat.Light.NoActionBar">
       <!-- 设置整个window窗体的背景颜色,为图片的底色,因为图片居中,图片未覆盖的区域会是白色-->
        <item name="android:windowBackground">@color/splash_blue</item>
        <!--设置背景颜色-->
        <item name="android:background">@drawable/splash_bg</item>
        <!-- 隐藏标题栏 -->
        <item name="windowNoTitle">true</item>
        <!-- 隐藏状态栏 -->
        <item name="android:windowFullscreen">true</item>
    </style>

缺点:在pad上,原来是启动时背景图片拉伸变形了,感觉不是很好。现在使用BitmapDrawable后,图片会保持自身大小不变。不会变形。但是同一张背景图,在手机上看挺好的;在大屏幕pad上,显得很小,感觉也不是很好。

<item name="android:background">@drawable/splash</item> splash是原图片,拉伸变形。

<item name="android:background">@drawable/splash_bg</item> splash_bg是BitmapDrawable,保持自身大小不变。

问:有没有办法像ios那样,手机和pad使用不同的启动图呢?

关于适配:dimen是根据设备的像素密度改变控件实际px值,这里通过background属性设置的背景图片,大小相当于是match_parent,没办法指定大小,怎么办? :(

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值