【问题标题】:How to implement Android splash screen using svg instead of png?如何使用 svg 而不是 png 实现 Android 启动画面?
【发布时间】:2017-07-24 12:50:16
【问题描述】:

我制作 RN 应用程序。对于我制作的Android版本 /drawable/splash_screen.xml 包含以下内容

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

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

  <item>
    <bitmap
      android:gravity="center"
      android:src="@mipmap/ic_launcher"/>
  </item>

</layer-list>

并在 res/values/styles.xml 中链接到该文件

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

也在 AndoirdManifest.xml 中

<application
  android:name=".MainApplication"
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/SplashTheme"
    android:screenOrientation="landscape"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

这很好用,但我想制作 SVG 文件而不是 PNG(mipmap/ic_launcher)。 SVG 文件可能是这样的 https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html (至少没有动画)。 如何做到这一点?

【问题讨论】:

  • 我尝试了一个比例drawable,但它的默认级别是0,不能在drawable xml中设置。这是一种耻辱,因为这将是解决方案。

标签: android svg react-native


【解决方案1】:

Android Studio 中默认的 SVG 导入器不能很好地工作,所以我建议使用 SVG 到 VectorDrawable 转换器,例如 svg2android

现在我们需要使用 svg2android 生成的代码创建一个可绘制文件。

res/drawable --> 新建 --> 可绘制资源文件 --> 将矢量代码粘贴到那里

splash_screen.xml 上,我们需要使用我们的向量添加一个项目 作为drawablegravity 设置为center

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

    <!-- background with solid color -->
    <item android:drawable="@android:color/white"/>

    <!-- logo -->
    <item
        android:drawable="@drawable/your_logo"
        android:gravity="center"/>

</layer-list>

-- 已编辑(2019 年 12 月)--

正如@petyr 在 cmets 中指出的那样,svg2android 现在已被弃用,但正如@Krisztián 所建议的那样,Respresso Image Converter 几乎可以做 svg2android 可以做的所有事情。 只需将 SVG 图像添加到其中,将其保留在 auto 中,它就会生成与 Android 兼容的 SVG。

【讨论】:

  • 工具 svg2android 已被弃用
  • 你可以试试Respresso's image converter。它比 Android Studio 维护得更好。它可以处理不同的 API 级别功能(例如:渐变)。
  • @KrisztiánVelez 但是这个 Respresso 图像转换器没有相同的功能:不兼容 SVG > 到 > android 兼容 SVG。这就是 svg2android 的魔力。
  • @petyr 您可以使用 Android Studio 中的内置矢量资源,也可以继续使用 svg2android。如果有人对此有更好的选择,我会编辑帖子并添加它:)
  • @NícolasSchirmer 我曾在 Respresso 团队工作。据我所知,他们比较了这些工具的功能。 svg2android 的所有功能都已实现。我敢肯定,如果您向 info@respresso.io 发送一些不起作用的样品,他们会很感激的。
【解决方案2】:

在xml文件中你只需要使用drawable属性而不是位图标签

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

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

  <item android:drawable="@mipmap/ic_launcher"/>

</layer-list>

【讨论】:

  • 所以我需要将我的 SVG 放入每个 mipmap-Xdpi 文件夹中?
  • 不一定,可以使用资产导入工具导入一个SVG文件
  • 谢谢,我一直在探索互联网,直到我得到这个答案才开始工作
【解决方案3】:

第一次转换为 xml,第二次命名为 svg 中的托管元素。 (VectorDrawable) android_svg.xml:

   <vector android:height="400dp" android:viewportHeight="206.132".....>
   ...............
    <path android:name = "arrow"
        android:fillColor="#00000000"
        android:pathData="M 47.30268,56.147897 37.688314,45.379807"
        android:strokeAlpha="1" android:strokeColor="#151414"
        android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="1"/>
   </vector>

第三次使用AnimatedVectorDrawable对VectorDrawable进行动画处理:

 <animated-vector
        xmlns:android="http://schemas.android.com/apk/res/android"
        ............................>
    <target android:name="arrow">
        <aapt:attr name="android:animation">
            <set android:ordering="sequentially">
                <objectAnimator
                    android:duration="1000"
                    android:propertyName="pathData"
                    android:valueFrom="M 47.30268,56.147897 37.688314,45.379807"
                    android:valueTo=  "M 47.30268,56.147897 37.688314,45.379807"
                    android:valueType="pathType"
                    tools:targetApi="lollipop" />
                <objectAnimator
                    android:duration="1000"
                    android:propertyName="pathData"
                    android:valueFrom="M 47.30268,56.147897 37.688314,45.379807"
                    android:valueTo=  "M 47.30268,56.147897 57.686195,45.187519"
                    android:valueType="pathType"
                    tools:targetApi="lollipop" />
            </set>

        </aapt:attr>
    </target>
</animated-vector>

这里是https://www.ap-impulse.com/animaciya-svg-dlya-web-i-android-splash-screen-shag-105/,用于说明如何使用上述内容。 这是项目本身https://github.com/nik137/Diagnostics

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案4】:

您好,您可以按照以下步骤将您的 svg 导入到您的项目中:

  1. 在android studio中右键drawable文件夹。
  2. 选择新建 > 矢量绘图 > 本地文件
  3. 导入后文件会复制到drawable文件夹中。
  4. 现在你可以像这样引用drawable了

    <item
        android:drawable="@color/off_white"/>
    
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/yoursvg"/>
    </item>
    

在此之后,您应该能够在屏幕上看到您的图像。如果对您有帮助,请测试并接受它作为答案。

【讨论】:

  • 不幸的是,这不起作用。应用程序在启动时崩溃(弹出消息:“Myapp 已停止”)。我已经尝试使用 AnimatedVectorDrawable 中的 svg 和我的 svg 文件。
  • 在你正在测试的设备上,如果它低于 lolipop,则应将此行添加到 build.gradle 文件 vectorDrawables.useSupportLibrary = true
  • 我用的是安卓5.0+
  • 矢量不是位图。因此它不会加载到位图标签中。
  • 这个不行,drawable文件夹不能包含svg文件,只有xml和png
猜你喜欢
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2022-08-19
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多