SplashScreen与矢量拉伸全屏

时间:2016-08-26 18:45:09

标签: android android-layout splash-screen android-vectordrawable

我使用本教程完成了启动画面,效果很好。 https://www.bignerdranch.com/blog/splash-screens-the-right-way/ 基本上我通过主题设置了一个splascreen:

   <style name="ThemeSplash" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/drawable_splashcreen</item>
    </style>

我想把矢量图像放在这里:

(drawable_splashcreen)

<item android:drawable="@color/color_background_splash_screen" />

<item
    android:drawable="@drawable/vector_najdiflet_logo"

    />

图像将在整个屏幕上拉伸。在API 23上,它的工作方式应该如此。但在较旧的设备上它只是拉伸。我尝试了宽度,高度,甚至搞砸了视口但没有成功。对此有何解决方法?

8 个答案:

答案 0 :(得分:8)

对于全屏启动,请尝试使用:

android:gravity="fill_horizontal|fill_vertical"
  

如果不解决,可能解决方案是创建分离的图像   对于每种分辨率大小。

最常见的解决方案:

  • 小= 240 x 320px(ldpi)
  • 中= 320 x 480px(mdpi)
  • 大= 480 x 800px(hdpi)
  • xLarge = 640 x 960px(xhdpi)

纵向格式:

  • ldpi = 240 x 360px(0.75 x mdpi)
  • mdpi = 320 x 480px(基密度)
  • hdpi = 480 x 720px(1.5 x mdpi)
  • xhdpi = 640 x 960px(2 x mdpi)
  • xxhdpi = 960 x 1440px(3 x mdpi)
  • xxxhdpi = 1080 x 1920px(4 x mdpi)

横向格式(反转纵向格式):

  • ldpi = 360 x 240px(0.75 x mdpi)
  • mdpi = 480 x 320px(基密度)
  • hdpi = 720 x 480px(1.5 x mdpi)
  • xhdpi = 960 x 640px(2 x mdpi)
  • xxhdpi = 1440 x 960px(3 x mdpi)
  • xxxhdpi = 1920 x 1080px(4 x mdpi)

您可以在此处找到更多相关信息:

  1. https://design.google.com/devices/

  2. Android splash screen image sizes to fit all devices

  3. http://vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer/

答案 1 :(得分:8)

我偶然发现了同样的问题。 不幸的是,似乎没有可能仅使用可用于pre API 23的矢量绘制启动画面。

问题是您无法在进程外部加载VectorDrawableCompat,例如在本例中的主题android:windowBackground中。因此,这里可能发生的事情是,在API 21上,将Vector转换为兼容的PNG。因此,在<layered-list>中,将转换后的PNG插入到<item>元素中,这导致位图延伸到所有边缘,因为它缺少了<bitmap>元素。

所以我的解决方法是:

在文件夹drawable_splashscreen.xml中创建一个drawables-v23,它类似于矢量可绘制对象的以下内容

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:opacity="opaque">

    <item android:drawable="?attr/colorPrimary"/>

    <item android:drawable="@drawable/ic_splashscreen" android:gravity="center"/>

</layer-list>

然后在常规drawable_splashscreen.xml文件夹中创建另一个drawables

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:opacity="opaque">

    <item android:drawable="?attr/colorPrimary"/>

    <item>
        <bitmap android:src="@drawable/ic_splashscreen" android:gravity="center"/>
    </item>

</layer-list>

注意<bitmap>元素。因此,现在,当在API 23之前的设备上使用PNG时,它将正确显示,并且不会延伸到整个背景。不幸的是,您还必须提供启动屏幕作为PNG,才能在旧的API中工作。但是,对于具有API 23+的每台设备,都会使用vector drawable。

希望我这个答案可以帮助遇到同一问题的人!

答案 2 :(得分:3)

经过编辑的解决方案,可使您的SplashScreen在包括API21到API23在内的所有API上都看起来不错

首先阅读this文章,并遵循制作初始屏幕的良好方法。

如果徽标变形或不合适,而您仅针对APIs24 +,则可以直接在其xml文件中直接按比例缩小矢量可绘制对象,如下所示:

<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="640"
android:viewportHeight="640"
android:width="240dp"
android:height="240dp">
<path
    android:pathData="M320.96 55.9L477.14 345L161.67 345L320.96 55.9Z"
    android:strokeColor="#292929"
    android:strokeWidth="24" />
</vector>

在上面的代码中,我将在640x640画布上绘制的可绘制对象重新缩放为240x240。然后像这样将其放在我的启动屏幕可绘制对象中,效果很好:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"
android:paddingBottom="20dp" android:paddingRight="20dp" android:paddingLeft="20dp" android:paddingTop="20dp">

<!-- The background color, preferably the same as your normal theme -->
<item>
    <shape>
        <size android:height="120dp" android:width="120dp"/>
        <solid android:color="@android:color/white"/>
    </shape>
</item>

<!-- Your product logo - 144dp color version of your app icon -->
<item
    android:drawable="@drawable/logo_vect"
    android:gravity="center">

</item>
</layer-list>

我的代码实际上仅在底部的图片中绘制三角形,但是在这里您可以看到可以实现的目标。与使用位图时获得的像素化边缘相反,分辨率最终是很大的。因此,请务必使用可绘制的矢量(有一个名为vectr的网站,我曾用它来创建我的网站,而无需下载专门软件。)

编辑以使其也可以在API21-22-23上使用

虽然以上解决方案适用于运行API24 +的设备,但在将应用程序安装为运行API22的设备后,我感到非常失望。我注意到启动画面再次试图填充整个视图,看起来像狗屎一样。经过半天的折磨,我终于通过纯粹的意志力强行提出了解决方案。

您需要创建第二个文件,其名称与splashscreen xml完全一样(假设为splash_screen.xml),并将其放入2个名为drawable-v22和drawable-v21的文件夹中,这些文件将在res /文件夹中创建(为了看到他们,您必须将项目视图从Android更改为Project)。每当相关设备运行与可绘制文件夹see this link中的-vXX后缀相对应的API时,这便可以告诉您的手机重定向到这些文件夹中放置的文件。将以下代码放在在这些文件夹中创建的splash_screen.xml文件的“层”列表中:

<item>
<shape>
    <size android:height="120dp" android:width="120dp"/>
    <solid android:color="@android:color/white"/>
</shape>
</item>

<!-- Your product logo - 144dp color version of your app icon -->
<item android:gravity="center">
    <bitmap android:gravity="center"
        android:src="logo_vect"/>

</item>

these is how the folders look

由于这些API的某些原因,您必须将可绘制对象包装在位图中,以使其起作用并进行喷射,最终结果看起来相同。问题是您必须使用带有常规可绘制文件夹的方法,因为第二个版本的splash_screen.xml文件将导致您的启动屏幕在运行高于23的API的设备上根本不显示。您可能还必须放置android默认将splash_screen.xml的第一个版本添加到drawable-v24中,默认为它可以找到资源的最近的drawable-vXX文件夹。

my splashscreen

答案 3 :(得分:1)

使用位图并在src中定义图像。将重力设置为中心

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">

    <item android:drawable="@color/color_background_splash_screen"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/ic_logo_splash"/>
    </item>
</layer-list>

splash screen

答案 4 :(得分:0)

我不认为这可以用于矢量,对于设备&lt; API 23,因为无法在drawable上设置android:height和android:width属性。

要使用居中图标实现启动画面,我必须为每个屏幕尺寸将徽标的矢量导出到.png,并在图层列表中嵌入位图:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@drawable/splash_screen_background" />

    <item
        android:left="20dp"
        android:right="20dp">
        <bitmap
            android:gravity="center"
            android:scaleType="centerInside"
            android:src="@drawable/logo_rasterised" />
    </item>

</layer-list>

理想情况下,我不希望在我的资源中有任何位图图像,但至少启动屏幕是我必须使用位图的唯一地方。

答案 5 :(得分:0)

我发现解决此问题的最简单方法是使用矢量资产。 这将确保 apk 大小不会太大。如果您没有这样做或没有用于徽标的 svg 文件,您应该手动更改图片的 DP 强度并手动添加它们。您也可以使用自动为您执行此操作的插件。

如果您使用矢量文件并最终得到放大的图像,请添加 android:top、left right 和 bottom 以使徽标大小正确。

答案 6 :(得分:-4)

不要将图像放在窗口中:Background(style.xml),

而不是

在布局文件中添加imageView,

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@drawable/vector_najdiflet_logo"
      android:scaleType="centerCrop"/>

尝试改变,

android:scaleType="centerInside"

答案 7 :(得分:-5)

不要将你的图像放在窗口中:背景,而是添加为android:src,用于SplashScreenActivity布局的imageview。