Since nacho-coloma's answer helped me, I took its excellent script and made it somewhat easier to use on a daily basis.
Firstly:
- Create a
drawable-svg directory next to your res directory. - Put your svg and script files in
drawable-svg . - Make the script executable.
- Run it. In Ubuntu, you can simply double-click it in Nautilus and make it run in the terminal.
And later, when you get the new svg files:
- Put the new svg files in
drawable-svg and run the script again.
By default, it will do what you want: scale each svg file in png files and place them in ../res/drawable-mdpi , ../res/drawable-hdpi , etc.
The script takes two parameters:
- Scale svg file template, default:
*.svg - The base directory for put, by default
../res/ (i.e. your res directory with the above setting).
You can experiment by scaling one svg to pngs in the current directory as follows:
$ ./svg2png test.svg .
Or just process all the images:
$ ./svg2png
I think you could place drawable-svg inside the res directory, but I haven't looked at what is wrapped in the latest APK. In addition, my svg files have - in their names that Android does not like, and my script takes care of renaming png files to something that works on Android.
I use ImageMagick for conversion, which is a bit more standard than Inkscape (although 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.
Stephan Henningsen Mar 08 '15 at 20:48 2015-03-08 20:48
source share