简体   繁体   中英

Xamarin.Android: vector asset not supported by Android 5.x

I work on a Xamarin.Android project where I've created a Layout that contains a vectorial asset as background.

This works fine, but on Android 5.x, I get the exception "Could not activate JNI Handle" when the layout is instantiated.

I've created the vector asset through Android Studio, from a.svg.

Is there another way to create the asset, to make it compatible with Android 5.x?

The layout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="wrap_content"
    android:layout_height="wrap_content"
    android:paddingHorizontal="@dimen/card_padding_horizontal"
    android:id="@+id/CardLayout">

    <ImageView
        android:id="@+id/CardBackgroundImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:src="@drawable/card_background"/>
    
    <!-- other items -->
</androidx.constraintlayout.widget.ConstraintLayout>

The vector asset looks like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:width="349dp"
    android:height="211dp"
    android:viewportWidth="349"
    android:viewportHeight="211">
<path
    android:pathData="M19,0L330,0A16,16 0,0 1,346 16L346,190.39A16,16 0,0 1,330 206.39L19,206.39A16,16 0,0 1,3 190.39L3,16A16,16 0,0 1,19 0z">
    <aapt:attr name="android:fillColor">
    <gradient 
        android:startY="0"
        android:startX="3"
        android:endY="216.335"
        android:endX="339.608"
        android:type="linear">
        <item android:offset="0" android:color="#FF35373A"/>
        <item android:offset="0.337274" android:color="#FF5F6367"/>
        <item android:offset="0.591875" android:color="#FF5F6367"/>
        <item android:offset="0.929681" android:color="#FF35373A"/>
    </gradient>
    </aapt:attr>
</path>
<path
    android:pathData="M246.565,84.052C233.962,84.052 225.75,92.525 225.75,103.808C225.75,116.802 234.938,124.46 247.988,124.46C251.931,124.46 255.834,123.93 259.087,122.586L260.022,113.584C257.135,114.928 254.045,115.906 250.712,115.906C245.467,115.906 239.369,113.462 238.271,107.067H267.624C267.908,93.095 260.794,84.052 246.565,84.052ZM237.946,99.49C238.922,95.254 242.012,92.077 246.931,92.077C251.525,92.077 254.777,94.806 255.387,99.49H237.946Z"
    android:strokeAlpha="0.1"
    android:fillColor="#F7F8F9"
    android:fillAlpha="0.1"/>
</vector>

The problem was related to the <gradient> property of the vector asset .

To fix it, I've finally used app:srcCompat instead of android:src for the ImageView .

<ImageView
    android:id="@+id/CardBackgroundImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    app:srcCompat="@drawable/card_background"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM