如何使用svg而不是png实现Android启动画面?

Cod*_*eBy 5 svg android react-native

我制作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>
Run Code Online (Sandbox Code Playgroud)

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

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>
Run Code Online (Sandbox Code Playgroud)

也在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>
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,但我想制作SVG文件而不是PNG(mipmap / ic_launcher)。SVG文件可能是这样的 https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html (至少没有动画)。如何实现呢?

Níc*_*mer 8

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

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

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

在上,splash_screen.xml我们需要使用我们的向量(drawable与的gravity集合一起)添加一个项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>
Run Code Online (Sandbox Code Playgroud)

  • 不过 svg2android 工具已被弃用 (2认同)
  • 2021 Represso 不再为 Android 提供 SVG 转换 (2认同)

小智 7

在 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>
Run Code Online (Sandbox Code Playgroud)


小智 -5

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

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

    <item
        android:drawable="@color/off_white"/>
    
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/yoursvg"/>
    </item>
    
    Run Code Online (Sandbox Code Playgroud)

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