SVG Vs PNG on Android

29,084

Solution 1

Lollipop (API 21) does not support SVG. It support a subset of SVG path drawing functionality through the VectorDrawable class. This class is not currently supported by appcompat, so it is only available on API 21.

You will still need PNG images for older platforms, so the ideal workflow is to have vector-based source images that you export to PNG for various DPI buckets and convert to VectorDrawable format for API 21 devices using a project like svg2android.

Solution 2

You can use Android Support Library 23.2 or higher. The VectorDrawableCompat class in the Support Library allows you to support VectorDrawable in Android 2.1 (API level 7) and higher.For this you need to change your build.gradle file before you run Vector Asset Studio, as described in Support Library Backward Compatibility.

//For Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

    //For Gradle Plugin 1.5 or below
    android {
      defaultConfig {
        // Stops the Gradle plugin’s automatic rasterization of vectors
        generatedDensities = []
      }
      // Flag notifies aapt to keep the attribute IDs around
      aaptOptions {
        additionalParameters "--no-version-vectors"
      }
    }

In order to support Vector Drawable and Animated Vector Drawable on devices running Android versions prior to version 5.0 (API level 21), VectorDrawableCompat and AnimatedVectorDrawableCompat are available through two new Support Libraries: support-vector-drawable and animated-vector-drawable, respectively.

Android Studio 1.4 introduced limited compatibility support for vector drawables by generating PNG files at build time. However, the vector drawable and animated vector drawable support Libraries offer both flexibility and broad compatibility — it's a support library, so you can use it with all Android platform versions back to Android 2.1 (API level 7+). To configure your app to use vector support libraries, add the vectorDrawables element to your build.gradle file in the app module.

Solution 3

Lollipop cannot handle SVG files without third-party libs.

The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+.

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version

Links point to readmes, which provide enough information on how to use the lib and the converter.

Share:
29,084
sravanalakshmi.sunkara
Author by

sravanalakshmi.sunkara

[Android Developer]

Updated on April 26, 2020

Comments

  • sravanalakshmi.sunkara
    sravanalakshmi.sunkara about 4 years

    Which is the best way of dealing with images in Android. Recently in Android Lollipop We had given support for SVG(Scalable Vector Graphics) concept. Which is the best way of working with images to support all resolutions PNG(placing images in particular drawable resources) or SVG(Small file sizes that compress well, Scales to any size without losing clarity (except very tiny)).