Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use fillType="evenOdd" on Android 21

I have an SVG image that I want to include in an Android project.

The minSdk version for the project is 9. Android Studio is generating PNG files for the SVG.

However, for API 21+, Android studio includes the SVG in a drawable-anydpi-v21 resource folder.

That means that any device running API 21 and up will use the SVG instead of the PNGs.

And that is the problem: my SVG requires android:fillType="evenOdd".

The PNG are properly generated, but for the SVG, because that attribute is API 24+, it is ignored on API 21 devices, and the image appears filled instead of being a wireframe-like image.

Is there anyway to convince Android studio to only include the PNG and drop the SVG completely?

Is there an other solution for this?

like image 538
njzk2 Avatar asked Dec 16 '16 16:12

njzk2


People also ask

Can we use SVG in Android?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

What is viewportHeight Android?

android:viewportHeight. Used to define the height of the viewport space. Viewport is basically the virtual canvas where the paths are drawn on.

What is FillType Evenodd?

Enum values. Path.FillType. EVEN_ODD. Specifies that "inside" is computed by an odd number of edge crossings.


3 Answers

Attribute android:fillType is only used in API level 24 and higher. For below API level, follow these steps.

  1. add this to your Gradle file.

    //Gradle Plugin 2.0+
    android {
      defaultConfig {
         ...
         vectorDrawables.useSupportLibrary true
      }
    }
    
  2. If you are using ImageView, change its property android:src to app:srcCompat.

  3. If you are using as android:drawableStart or android:drawableEnd in TextView, change it to app:drawableStartCompat or app:drawableEndCompat.

like image 85
Kirit Khant Avatar answered Oct 09 '22 08:10

Kirit Khant


Solution 1

Flattern image in Sketch and use this site to convert SVG to xml for Android

Solution 2

I use nonZero instead of evenOdd and open it in Sketch to reverse Order after reverse it will change pathData and remove android:fillType and everything work fine on Android 21+.

Solution 3

PNG

TLDR

After some research I found that there are two fill-rule property methods for Vector graphics, SVGs, the “evenodd” vs “nonzero”

I opened the SVG icon in Sketch and inspected the hole at the top of the icon. As expected it uses fill-rule:evenodd property. Now I have to change the fill-rule to use “nonzero” property. How? Select the path. In the right side, there is a settings icon at the “Fills” property. Click it and choose “non-zero”.

From the main menu, choose Layer → Paths → Reverse Order. I got the hole back at the top of the icon and got the hole in the app, too.

For more detail

like image 21
UmAnusorn Avatar answered Oct 09 '22 07:10

UmAnusorn


SVGs with filltype="evenOdd" seems to work and display properly now with Android gradle plugin 3.2. You may also need to use Support Library 28 or AndroidX 1.0.

Make sure it's at least this version in your top level build.gradle:

classpath 'com.android.tools.build:gradle:3.2.0'
like image 38
Gak2 Avatar answered Oct 09 '22 06:10

Gak2