├── README.md ├── sample ├── colors.conf ├── gen-png.sh └── raw │ ├── window_background_statusbar_toolbar_tab.9.shsvg │ └── window_background_statusbar_toolbar_tab.9.shsvg.conf ├── shsvgdpi.sh └── svgdpi.sh /README.md: -------------------------------------------------------------------------------- 1 | # Android SVG Scripts 2 | 3 | Bash scripts to automate and extend the process of using SVG as Android image asset. 4 | 5 | This project is also used by [zhanghai/MaterialColdStart](https://github.com/zhanghai/MaterialColdStart). 6 | 7 | ## Dependencies 8 | 9 | - inkscape: For PNG genration 10 | - gcalccmd (shsvgdpi.sh): For (friendly) mathematical calculation. 11 | 12 | ## svgdpi.sh 13 | 14 | ``` 15 | Usage: svgdpi.sh SVG_FILE [DIR_PREFIX [DIR_SUFFIX]] 16 | ``` 17 | 18 | This script can automatically generate PNG asset of different Dips from SVG arranged properly for direct usage in Android application. 19 | 20 | For instance: 21 | 22 | ``` 23 | ./svgdpi.sh path/to/your/file.avg 24 | ``` 25 | 26 | Will output the following files: 27 | 28 | ``` 29 | drawable-mdpi/file.png 30 | drawable-hdpi/file.png 31 | drawable-xhdpi/file.png 32 | drawable-xxhdpi/file.png 33 | drawable-xxxhdpi/file.png 34 | ``` 35 | 36 | You can also add prefix and suffix to the drawable directory name with command line arguments. See the script source for more detail. 37 | 38 | ## shsvgdpi.sh 39 | 40 | ``` 41 | Usage: shsvgdpi.sh SHSVG_FILE [DIR_PREFIX [DIR_SUFFIX]] 42 | ``` 43 | 44 | This script implemented a powerful new file format called ShellSVG (`.shsvg`), which allows you to embed any shell command/parameter substitution inside your SVG file. 45 | 46 | `dp` (Device-independent Pixel) is supported as an exported function `dp()`, and mathematical expression is supported as an exported function `calc()`. You need to wrap `dp()` inside a `calc()` if it is used. 47 | 48 | This makes drawing Nine-patch bitmaps where the surrounding lines are always `1px` while the content must be drawn in `dp` easy. 49 | 50 | What's more, you can now use parameters inside your SVG, a dimension, a color, a filter, everything is OK. Just do it as in shell, for instance `${SOME_PARAMETER}`. Parameter definitions can be written in a `.conf` file which will be `source`-ed before generation of a standard SVG file, which means this file can have all the shell semantics apart from regular key-value pair, such as `source`-ing another `.conf`. 51 | 52 | This script has taken rounding on HDPI (with scaling factor 1.5) into account, so that nine-patch bitmaps won't have an anti-aliased border. It is recommended that you call `dp()` for every single `dp` dimension separately to avoid accumulated rounding inconsistency. 53 | 54 | The usage of this script is just like `svgdpi.sh`. 55 | 56 | For a detailed example, see the [sample/](sample/) directory. 57 | 58 | ## License 59 | 60 | ``` 61 | Copyright (c) 2015 Zhang Hai 62 | 63 | This program is free software: you can redistribute it and/or modify 64 | it under the terms of the GNU General Public License as published by 65 | the Free Software Foundation, either version 3 of the License, or 66 | (at your option) any later version. 67 | 68 | This program is distributed in the hope that it will be useful, but 69 | WITHOUT ANY WARRANTY; without even the implied warranty of 70 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 71 | General Public License for more details. 72 | 73 | You should have received a copy of the GNU General Public License 74 | along with this program. If not, see 75 | . 76 | ``` 77 | -------------------------------------------------------------------------------- /sample/colors.conf: -------------------------------------------------------------------------------- 1 | COLOR_PRIMARY_DARK=#388e3c 2 | COLOR_PRIMARY=#4caf50 3 | COLOR_WINDOW_BACKGROUND=#fafafa 4 | -------------------------------------------------------------------------------- /sample/gen-png.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ../shsvgdpi.sh raw/window_background_statusbar_toolbar_tab.9.shsvg 3 | -------------------------------------------------------------------------------- /sample/raw/window_background_statusbar_toolbar_tab.9.shsvg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample/raw/window_background_statusbar_toolbar_tab.9.shsvg.conf: -------------------------------------------------------------------------------- 1 | source "$(dirname "$0")/colors.conf" 2 | 3 | STATUS_BAR_HEIGHT=25 4 | APPBAR_HEIGHT=104 5 | -------------------------------------------------------------------------------- /shsvgdpi.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # shsvgdpi.sh: Script for generating image assets of different DPIs 4 | # for Android from SHSVG source file, using Inkscape. 5 | # 6 | # Copyright (c) 2015 Zhang Hai 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, but 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | # General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see 20 | # . 21 | # 22 | 23 | calc() { 24 | input="${@//pi/π}" 25 | # Round as in TypedValue.complexToDimensionPixelSize() 26 | # Not using `xargs printf '%.0f'` because 27 | # `echo 196.5 | xargs printf '%.0f'` gives 196 instead of 197 due 28 | # to floating point. 29 | echo -ne "${input}\nquit" | gcalccmd | sed 's:^> ::g' | xargs printf 'scale=0;(%s+0.5)/1\n' | bc 30 | } 31 | dp() { 32 | echo "(($@) * ${scale})" 33 | } 34 | export -f calc dp 35 | 36 | usage() { 37 | local name 38 | name="$(basename "$0")" 39 | cat <"${dir}/${name}.svg" << EOF2 63 | $(cat "$1") 64 | EOF2 65 | EOF 66 | 67 | inkscape --export-png="${dir}/${name}.png" "${dir}/${name}.svg" 68 | 69 | rm "${dir}/${name}.svg" 70 | } 71 | 72 | main() { 73 | 74 | if [[ "$#" -lt 1 ]]; then 75 | usage 76 | exit 1 77 | fi 78 | 79 | gen-png "$1" "drawable$2-mdpi$3" '1' 80 | gen-png "$1" "drawable$2-hdpi$3" '1.5' 81 | gen-png "$1" "drawable$2-xhdpi$3" '2' 82 | gen-png "$1" "drawable$2-xxhdpi$3" '3' 83 | gen-png "$1" "drawable$2-xxxhdpi$3" '4' 84 | } 85 | 86 | main "$@" 87 | -------------------------------------------------------------------------------- /svgdpi.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # svgdpi.sh: Script for generating image assets of different DPIs for 4 | # Android from SVG source file, using Inkscape. 5 | # 6 | # Copyright (c) 2015 Zhang Hai 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, but 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | # General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see 20 | # . 21 | # 22 | 23 | usage() { 24 | local name 25 | name="$(basename "$0")" 26 | cat <