Skip to content

Instantly share code, notes, and snippets.

@RicardoBelchior
Forked from deepankarb/svg-to-android.sh
Last active December 8, 2017 21:32
Show Gist options
  • Save RicardoBelchior/e684ce6a03cb6090ad7e to your computer and use it in GitHub Desktop.
Save RicardoBelchior/e684ce6a03cb6090ad7e to your computer and use it in GitHub Desktop.
A script to generate android assets from a SVG file. Requires inkscape to resize and convert.
#!/bin/bash
USAGE="Usage is: ./svg-to-android.sh <svg_file> <exported_png_name> <width_dp> <height_dp> [resources_dir] \n \
If 'resources_dir' is ommited, a new folder is created in the current directory, named after the svg name.\n \
Param 'exported_png_name' does not need extension.\n \
On Mac OS specify full path for svg file and resources directory.\n \
Exiting."
if [[ -z "$1" ]];
then
echo "No SVG file specified."
echo -e $USAGE
exit -1
fi
ispng=$(file $1)
echo $ispng | grep -q SVG
if [[ $? -ne 0 ]];
then
echo "Invalid SVG file."
echo -e $USAGE
exit -2
fi
# Parse PNG final name
if [[ -z "$2" ]];
then
echo "Invalid PNG name."
echo -e $USAGE
exit 0
else
BASE="$2"
fi
# Parse image DP Width (aka, pixel size at mdpi)
if [[ -z "$3" ]];
then
echo "Missing width!"
echo -e $USAGE
exit -1
else
W="$3"
fi
# Parse image DP Height (aka, pixel size at mdpi)
if [[ -z "$4" ]];
then
echo "Missing height!"
echo -e $USAGE
exit -1
else
H="$4"
fi
# Parse directory to export
if [[ -z "$5" ]];
then
resroot=`basename $1`
else
resroot="$5"
fi
DPINAME=("mdpi" "hdpi" "xhdpi" "xxhdpi" "xxxhdpi")
MULT_FACTOR=(1.0 1.5 2.0 3.0 4.0)
for ((ii=0;ii<5;ii++));
do
echo "Processing $1 for ${DPINAME[$ii]}@${W[$ii]} px"
suffix=${DPINAME[$ii]}
dirname=$resroot/res/drawable-${DPINAME[$ii]}
mkdir -p "$dirname"
fname="$dirname"/"$BASE".png
mult=${MULT_FACTOR[$ii]}
width=$(bc -l <<< "(${mult} * ${W})")
height=$(bc -l <<< "(${mult} * ${H})")
inkscape -z -f=$1 --export-png="$fname" \
-w=${width} -h=${height}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment