What are best practices for using SVG icons on Android?

前端 未结 12 1331
臣服心动
臣服心动 2020-12-12 11:46

I am about to create my first Android native (so not browser based) app and looking for some good practices regarding icon creating/provisioning. Since it s

相关标签:
12条回答
  • 2020-12-12 11:51

    For Android older than Lollipop, your best practice for SVG on Android is going to be to use a tool to convert your SVG to PNG at the size(s) you're interested in. Existing SVG support for Android is not comprehensive of what you're likely to find in an SVG file, and even if it were, the support is not built into the OS so using them directly for icons is definitely out.

    Beginning with Lollipop (API 21) see What are best practices for using SVG icons on Android?. Thanks to @MarkWhitaker @AustynMahoney for pointing this out.

    0 讨论(0)
  • 2020-12-12 11:51

    This is what we are using to transform a SVG file into multiple resolutions. For example, to generate the launch icon: svg2png -w48 icon.svg

    #!/bin/bash -e
    # Transforms a SVG into a PNG for each platform
    # Sizes extracted from
    # http://developer.android.com/design/style/iconography.html
    
    [ -z $2 ] && echo -e "ERROR: filename and one dimension (-w or -h) is required, for example:\nsvg2png -w48 icon.svg\n" && exit 1;
    FILENAME=$2
    DEST_FILENAME=`echo $2 | sed s/\.svg/\.png/`
    FLAG=`echo $1 | cut -c1-2`
    ORIGINAL_VALUE=`echo $1 | cut -c3-`
    
    if [ "$FLAG" != "-w" ] && [ "$FLAG" != "-h" ]; then
        echo "Unknown parameter: $FLAG" 
        exit 1
    fi
    
    # PARAMETERS: {multiplier} {destination folder}
    function export {
      VALUE=$(echo "scale=0; $ORIGINAL_VALUE*$1" | bc -l)
      CMD="inkscape $FLAG$VALUE --export-background-opacity=0 --export-png=src/main/res/$2/$DEST_FILENAME src/main/svg/$FILENAME > /dev/null"
      echo $CMD
      eval $CMD
    } 
    
    export 1 drawable-mdpi
    export 1.5 drawable-hdpi
    export 2 drawable-xhdpi
    export 3 drawable-xxhdpi
    export 4 drawable-xxxhdpi
    
    0 讨论(0)
  • 2020-12-12 11:53

    svg is awesome. who want use svg:

    right click on drawable "new/Vector Asset" choose "material icon" for default icons and "locale SVG file" for your file in your computer hard drive and in "resource name" type name for svg file then click on "next" button and "finish"

    and you can use that in drawable. fillcolor must be hard code.

    simple example

    navigation_toggle.xml

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24.0"
            android:viewportHeight="24.0">
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M3,18h18v-2H3v2zm0,-5h18v-2H3v2zm0,-7v2h18V6H3z"/>
    </vector>
    
    0 讨论(0)
  • 2020-12-12 11:55

    Good news everyone! Since android support library 23.2 we can use svg-s till back to API level 7!

    If you wanna be compatible backwards only till Lollipop (API 21) check Mark Whitaker's answer, but if you want to go below you need to add these lines to your build.gradle:

    // Gradle Plugin 2.0+ (if you using older version check the library announcement link)
    android {  
        defaultConfig {  
            vectorDrawables.useSupportLibrary = true  
        }  
    }  
    

    Also keep in mind that:

    • instead of android:src you need to use the app:srcCompat attribute in ImageViews.
    • you cannot use svg-s in StateListDrawables or other xml drawables, create them programmatically instead.
    • you cannot use the android:background attribute or View.setBackgroundResource() function, use the View.setBackground() instead.
    • you cannot use svg-s in case of Notifications.
    0 讨论(0)
  • 2020-12-12 11:55

    Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables

    1. add vectorDrawables.useSupportLibrary = true to your build.gradle file.
    2. Use app:srcCompat="@drawable/ic_add" instead of android:src="..." or setImageResource() for your ImageView

    http://android-developers.blogspot.sk/2016/02/android-support-library-232.html

    0 讨论(0)
  • 2020-12-12 11:59

    From Lollipop (API 21) onwards, Android defines the VectorDrawable class, for defining drawables based on vector graphics. Android Studio 1.4 adds the "Vector Asset Studio" to make them easier to work with, including an SVG import feature and a new Gradle plugin that generates PNG versions of VectorDrawable icons at build time for API 20 and earlier. There's also a third-party tool for converting SVGs to VectorDrawables. Bear in mind that although vector drawables can be defined in XML, the file format is not SVG and not all SVG files can be successfully converted. Simple graphics like icons should work OK.

    If you still need to generate PNGs yourself, you'll need to generate your icons at various resolutions. For ease of generating those PNGs I design icons as SVG and then export to the various sizes using Inkscape which is free and cross-platform. It's got some nice features for designing icons, including the Icon Preview view (see below), and it generates nice crisp PNGs.

    enter image description here

    0 讨论(0)
提交回复
热议问题