Since nacho-coloma's answer helped me, I've taken his excellent script and made it slightly easier to use on a daily basis.
First:
- Create directory
drawable-svg
next to your res
directory.
- Place your svg files and this script in
drawable-svg
.
- Make the script executable.
- Run it. In Ubuntu you can simply double-click it in Nautilus and make it run in a terminal.
And later when you get new svg files:
- Place new svg files in
drawable-svg
and run the script again.
By default it will do what you want: Scale every svg file into png files and put them into ../res/drawable-mdpi
, ../res/drawable-hdpi
etc.
The script takes two parameters:
- The svg file pattern to scale, default:
*.svg
- The base directory for put, default
../res/
(i.e. your res
directory with the above mentioned setup).
You can experiment by scaling a single svg into pngs in the current directory like this:
$ ./svg2png test.svg .
Or simply process all images:
$ ./svg2png
I guess you could place the drawable-svg
inside the res directory, but I haven't looked into what gets wrapped up in the final APK. Also, my svg files have -
in their names, which Android doesn't like, and my script takes care of renaming the png files to something valid on Android.
I'm using ImageMagick for the conversion which is slightly more standard that Inkscape (though I liked the approach). Both methods are included in the script for reference.
Here's the script:
#!/bin/bash
scalesvg ()
{
svgfile="$1"
pngdir="$2"
pngscale="$3"
qualifier="$4"
svgwidthxheight=$(identify "$svgfile" | cut -d ' ' -f 3)
svgwidth=${svgwidthxheight%x*}
svgheight=${svgwidthxheight#*x}
pngfile="$(basename $svgfile)" # Strip path.
pngfile="${pngfile/.svg/.png}" # Replace extension.
pngfile="${pngfile/[^A-Za-z0-9._]/_}" # Replace invalid characters.
pngfile="$pngdir/$qualifier/$pngfile" # Prepend output path.
if [ ! -d $(dirname "$pngfile") ]; then
echo "WARNING: Output directory does not exist: $(dirname "$pngfile")"
#echo "Exiting"
#exit 1
echo "Outputting here instead: $pngfile"
pngfile="$qualifier-${svgfile/.svg/.png}"
fi
pngwidth=$(echo "scale=0; $svgwidth*$pngscale" | bc -l)
pngheight=$(echo "scale=0; $svgheight*$pngscale" | bc -l)
pngdensity=$(echo "scale=0; 72*$pngscale" | bc -l) # 72 is default,
echo "$svgfile ${svgwidth}×${svgheight}px -> $pngfile ${pngwidth}×${pngheight}px @ $pngdensity dpi"
convert -background transparent -density $pngdensity "$svgfile" "$pngfile"
#inkscape -w${pngwidth} --export-background-opacity=0 --export-png="$pngfile" "$svgfile" > /dev/null
#convert "$svgfile" -background transparent -scale ${pngwidth}x${pngheight} "$pngfile"
}
svgfiles="$1"
svgfiles="${svgfiles:=*.svg}" # Default to input all *.svg in current dir.
pngdir="$2"
pngdir="${pngdir:=../res}" # Default to place output pngs to ../res, ie. ../res/drawable-hdpi etc.
for svgfile in $svgfiles; do
echo "Scaling $svgfile ..."
scalesvg "$svgfile" "$pngdir" 0.75 drawable-ldpi
scalesvg "$svgfile" "$pngdir" 1 drawable-mdpi
scalesvg "$svgfile" "$pngdir" 1.5 drawable-hdpi
scalesvg "$svgfile" "$pngdir" 2 drawable-xhdpi
scalesvg "$svgfile" "$pngdir" 3 drawable-xxhdpi
scalesvg "$svgfile" "$pngdir" 4 drawable-xxxhdpi
done
echo -n "Done."
read # I've made it wait for Enter -- convenient when run from Nautilus.