VectorDrawable

VectorDrawable PNG

Or The Curious Case of the Disappearing Vectors

Regular readers of Styling Android may remember a post a couple of months ago which dealt with some strange renderings of VectorDrawables because of fill winding issues. Recently I encountered another issue with some puzzling rendering of a VectorDrawable, and in this post we’ll take a look at some potential gotchas.

Firstly a little background. On the project where I encountered the issue we’re going to look at we’d recently made the decision to drop VectorDrawableCompat from the project. The reasoning for this was that we had quite a complex AnimatedVectorDrawable which was rendering perfectly when using native AnimatedVectorDrawable, but when we switched to the Compat version the animation wasn’t running on some Lollipop devices (which had worked perfectly well natively). Simple AnimatedVectorDrawables worked fine, but the more complex one did not – my guess was memory issues when using the Compat library. So a decision was made to keep the animation for Lollipop+ users, but use a static image for earlier devices (which actually constituted a really small percentage of the user-base for the app).

To achieve this, we turned off the VectorDrawable support library in our build.gradle:

By doing this we would default back to the older behaviour which is built in to the build toolchain: Converting our VectorDrawable assets to PNGs during the build and using these on pre-Lollipop devices. All was going well until we started getting defect reports that some, but not all, of our VectorDrawable assets had disappeared on pre-Lollipop devices. The problem turned out to be really easy to fix, but it took some head scratching to actually work out what was going wrong.

For the sample code I have simply imported one of the material icons in the Android Studio Vector Asset import tool, and then manually tweaked the colouring.

If this is rendered on a Nougat device then everything is good:

problem1-nougat

But the problem becomes apparent if we render it on a KitKat device:

problem1-kitkat

So what on earth is going on? In order to understand the issue we need to look at the actual PNG asset that has been generated during the build:

generated-png

The PNG that was generated is actually white. The reason that it is disappearing is because we are drawing a white asset over a white background and it is invisible as a result. So why is it rendering as green on a Nougat device, but the generated PNG is white? To understand that let’s take a look at at the VectorDrawable asset itself:

To get the colouring we want we’re drawing the vector path using a white fill colour and then applying a tint to the correct shade of green in the parent vector element. This works fine when we render as a VectorDrawable even on KitKat when using the Compat library. However when we generate PNGs during the build the tint value is not applied.

So the fix is actually really simple, just use the correct android:fillColor value, and ditch the android:tint:

If we do that we can see that the correct colouring gets applied to the generated PNG (and it will now render correctly in-app):

generated-png-fixed

Before we finish it is worth mentioning another potential gotcha. In the fixed VectorDrawable asset we’ve used a raw hex colour value: #1E9618. It may seem tempting to clean things up by using a colour resource instead:

Be warned: THIS DOES NOT WORK:

generated-png-resource

While colour resources are supported in Lollipop and later, they are not supported prior to that, and for the specific case we’re looking at, when auto-generating PNG assets. This issue is not quite as puzzling as the tint issue – mainly because you will actually get a lint warning if you try to use a colour resource.

Auto-generation of PNG assets is a really useful tool which makes your code-base easier to maintain because you only need to worry about vectors, and your density specific PNG assets will be generated from them. There are some gotchas along the way but, hopefully, you’ll be wiser to them now.

The source code for this article is available here.

© 2016, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 VectorDrawable PNG by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

4 Comments

  1. Yeah, Vectors on Android have been pissing me off lately. It is possible to use them pre-lollipop, but it is a pain. Another issue I’ve noticed is that using vectors with an attribute like android:drawableTop will not work on KitKat or older versions.

    1. Perhaps you should try reading the article before commenting. The entire point is that I’m not using the compat library and therefore AppCompatImageView isn’t available.

  2. Great post!
    I’ve been having some issues when attempting to migrate some Sketch cuts to vector assets, maybe with this newfound knowledge I’ll be able to fix it 🙂

    This whole blog is a gem, the series on Constraint Layout was really good too 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *


*