18

I've used a couple of images which I've imported using Android Studio's Vector Asset Studio through a local SVG file.

The images loaded perfectly on my nexus 6p, however, on the Sony Xperia Z, its very pixelated, and on Samsung Galaxy s2, its even worse.

From my understanding, since the file is a xml (vector) and not a png file, it should have been able to resize automatically or am I missing out on something.

I've attached an image of the 3 cases. enter image description here

EDIT: Just some extra info: The file was originally a PNG, however, I've fully converted to an vector file using a program called Vector Magic. I can open it on illustrator and edit each line etc.

Solved by changing from VectorDrawable to VectorDrawableCompat through the VectorDrawableCompat.create(..) method. Thanks @pskink.

Petersicecream
  • 341
  • 4
  • 11
  • You probably converted the SVG to a PNG (even though it really looks like a JPEG with a high compression level). That's why it becomes pixellated, if you don't provide all the scaled versions of your resource. – Phantômaxx May 17 '16 at 08:38
  • Visit this page http://stackoverflow.com/questions/36562973/resize-svg-image-inside-view/36564537#36564537 – Ahmad Aghazadeh May 17 '16 at 08:40
  • 3
    And... yes, confirmed. That tool generates bitmaps from the original SVG files: `Android 4.4 (API level 20) and lower doesn't support vector drawables. If your minimum API level is set at one of these API levels, you have two options when using Vector Asset Studio: generate Portable Network Graphic (PNG) files (the default) or use the Support Library. For backward-compatibility, Vector Asset Studio generates raster images of the vector drawable` For your reference: https://developer.android.com/studio/write/vector-asset-studio.html. – Phantômaxx May 17 '16 at 08:42
  • 1
    check if [this](http://stackoverflow.com/questions/35756691/android-support-library-23-2-vector-drawables-are-blurry/35756898#35756898) helps – Blackbelt May 17 '16 at 08:48
  • 1
    @BobMalooga Thanks for the link, very useful. However, the xperia Z is on Android 5.0.2 so it should support vector drawables and therefore should not be blury. – Petersicecream May 17 '16 at 09:52
  • 1
    Yes, it supports... VectorDrawables. But if you give it Bitmaps, it will use Bitmaps. – Phantômaxx May 17 '16 at 10:02
  • 1
    first check if it is a `VectorDrawable` or `BitmapDrawable`: use `Log.d` to log the value of `View#getBackground` or `ImageView#getDrawable` (i dont know where you are using your image) – pskink May 17 '16 at 10:07
  • @pskink Just checked and it seems to show as a vector drawable. "android.graphics.drawable.VectorDrawable@261ec1eb". – Petersicecream May 17 '16 at 10:30
  • 2
    so use `VectorDrawableCompat` as `VectorDrawable` is broken on v5 devices – pskink May 17 '16 at 10:32
  • @pskink sorry, could you guide me on how I would do that? I've tried changing the name space to app:srcCompat – Petersicecream May 17 '16 at 10:46
  • 1
    `VectorDrawableCompat#create`? – pskink May 17 '16 at 10:46
  • ok wow, thanks @pskink, your solution worked perfectly. I wonder why it isnt set as the compat version initially. I can accept your answer if you make a post. – Petersicecream May 17 '16 at 11:00
  • 1
    Does this answer your question? [Why isn't my vector drawable scaling as expected?](https://stackoverflow.com/questions/34936590/why-isnt-my-vector-drawable-scaling-as-expected) – Mahozad Oct 14 '20 at 08:50

3 Answers3

10

Adding android:scaleType="fitXY" to the image solved it for me.

Le-roy Staines
  • 2,037
  • 2
  • 22
  • 40
8

Android supports SVG on API level 21 and higher. For API level 19, 20 it fallback to png generation. The SVG we had put in the app had a smaller size for these generations and hence you seem to get pixelated images on your screen. In my case increasing the

<vector android:height="70dp" android:width="70dp"

of the vector XML generated better png and it started to look better on older devices

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
2

For me the reason that caused vectors to be converted to png was android:fillType="evenodd" attribute in my vector drawable. When I removed all occurrences of this attribute in my drawable (thus the vector using the default nonzero fill type), next time the app was built everything was fine.

Mahozad
  • 18,032
  • 13
  • 118
  • 133