【问题标题】:Make background image in splash screen center crop在启动画面中心裁剪背景图像
【发布时间】:2019-04-24 04:05:01
【问题描述】:

我使用图像作为斜线屏幕的图片。

<style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

但是,在某些不同的设备中,它已被拉伸和扭曲。有没有办法像在 ImageView 中一样使照片居中裁剪?

【问题讨论】:

标签: android android-xml android-theme


【解决方案1】:

与其直接设置图像,不如尝试从 xml 可绘制对象中设置它,这样可以更好地控制图像。

您可以按照此处显示的步骤进行操作:-

Splash the right way

【讨论】:

    【解决方案2】:

    如果您的drawable 是位图图像,那么您必须将drawable 资源的bitmap 元素上的android:gravity 属性更改为"center"

    默认值是"fill" “如果需要,增加对象的水平和垂直大小,使其完全填满容器。”More xml bitmap info.

    例如drawable/background_splash.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <color android:color="@color/colorAccent"></color>
        </item>
        <item>
            <bitmap
                android:src="@drawable/logo"
                android:gravity="center"
                android:antialias="true"
            />
        </item>
    </layer-list>
    

    【讨论】:

    • 将重力设置为中心只将drawable放在中间,它不会像ImageView一样进行中心裁剪。它应该同时放大和裁剪drawable。
    • 没错,我错过了“裁剪”部分。居中时,我的解决方案不能保证图像的两个尺寸都等于或大于启动屏幕的相应尺寸。要使用我的解决方案实现此(缩放),您必须为每个屏幕密度桶(drawable-hdpi、drawable-xhdpi、...)仔细选择启动图像的大小。
    【解决方案3】:

    试试这个解决方案

    首先将初始屏幕图像调整为不同大小,并将它们放在drawable文件夹中,例如drawable-mpdi,drawable-hpdi,drawable-xhpdi,drawable-xxhpdi,drawable-xxxpdi

    360x640 - mdpi 
    540x960 - hpdi
    720x1280 - xhpdi
    1080x1920 - xxhpdi
    1440x2560 - xxxhpdi
    

    然后在xml的splash活动中

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_screen">
    
    
    </RelativeLayout>
    

    或者简单地为特定活动定义 drawable 样式

    <style name="AppTheme.Launcher">
            <item name="android:windowBackground">@drawable/splash_screen</item>
    </style>
    

    【讨论】:

    • 看来你没有得到我的问题
    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多