Android icons creation tool
Are you developing an Android application and have you to convert an image to a set of icons ? Do you want to avoid as much as possible repetitive manual operations ? Here you can find a tool to convert a single image to the set of icons needed by Android for various device screen resolutions. Before starting you need to have:
- a Linux system (or any other unix-like system)
- a working installation of ImageMagick tools
#!/bin/sh
#
# Davide Airaghi
# Simple "icon creator" for Android development
# Image sizes taken from http://iconhandbook.co.uk/reference/chart/android/
#
echo ""
TYPE="$1"
IMAGE="$2"
RESNAME="$3"
OUTDIR="$4"
SIZES=""
if [ "$RESNAME" != "" ]; then
RESNAME=$( echo $RESNAME | sed -es/\[^a-zA-Z0-9\\_\]/_/g )
fi
function usage() {
echo ""
echo "Usage:"
echo "android_icon.sh TYPE ORIGINAL_IMAGE RESOURCE_NAME OUTPUT_DIR"
echo ""
echo "TYPE can be one of: launcher, actionbar, dialog, tab, contextual, notification"
echo ""
echo "Example"
echo ""
echo "android_launger_icon.sh launcher ~/images/my-icon.png my_icon ~/progs/my-app/app/src/main/res"
echo ""
echo ""
}
if [ "$TYPE" = "" ]; then
echo "Type not given"
usage
exit 1
fi
case $TYPE in
launcher)
SIZES="mdpi-48 hdpi-72 xhdpi-96 xxhdpi-144 xxxhdpi-192"
;;
actionbar)
SIZES="mdpi-32 hdpi-48 xhdpi-64 xxhdpi-96 xxxhdpi-128"
;;
dialog)
SIZES="mdpi-32 hdpi-48 xhdpi-64 xxhdpi-96 xxxhdpi-128"
;;
tab)
SIZES="mdpi-32 hdpi-48 xhdpi-64 xxhdpi-96 xxxhdpi-128"
;;
contextual)
SIZES="mdpi-16 hdpi-24 xhdpi-32 xxhdpi-48 xxxhdpi-64"
;;
notification)
SIZES="mdpi-24 hdpi-36 xhdpi-48 xxhdpi-72 xxxhdpi-96"
;;
*)
echo "Type not valid"
usage
exit 2
;;
esac
if [ "$IMAGE" = "" ]; then
echo "Image file not given"
usage
exit 3
fi
if [ "$RESNAME" = "" ]; then
echo "Resource name not given"
usage
exit 4
fi
if [ "$OUTDIR" = "" ]; then
echo "Output dir not given"
usage
exit 5
fi
if [ ! -f $IMAGE ]; then
echo "Image file not found"
exit 6
fi
if [ ! -d $OUTDIR ]; then
echo "Output dir not found"
exit 7
fi
IMAGENAME=$(basename $IMAGE)
IMAGENAME="${IMAGENAME%.*}"
PREFIX="mipmap-"
IMG_OK=""
IMG_ERR=""
for i in $SIZES;
do
ERR=0
OUTFILE=${OUTDIR}/mipmap-$(echo $i|cut -d- -f1)
if [ ! -d ${OUTFILE} ]; then
mkdir -p ${OUTFILE}
if [ "$?" != "0" ]; then
echo "Unable to create ${OUTFILE}"
ERR=1
fi
fi
OUTFILE=${OUTFILE}/${RESNAME}.png
if [ "$ERR" = "0" ]; then
SIZE=$(echo $i|cut -d- -f2)
convert $IMAGE -resize ${SIZE}x${SIZE} -gravity center -extent ${SIZE}x${SIZE} ${OUTFILE}
if [ "$?" != "0" ]; then
ERR=1
echo "Unable to create ${OUTFILE}"
fi
else
echo "Unable to create ${OUTFILE}"
fi
if [ "$ERR" = "1" ]; then
IMG_ERR="${IMG_ERR}$OUTFILE "
else
IMG_OK="${IMG_OK}$OUTFILE "
fi
done
echo ""
echo "Images created: ${IMG_OK}"
echo "Images not created: ${IMG_ERR}"
echo ""
if [ "${IMG_OK}" = "" ]; then
exit 255
else
exit 0
fi
After saving the file you have to make it executable ( chmod +x /usr/local/bin/android_icon.sh ). Now you are ready to convert images into set of icons. The tool needs 4 parameters to do its job:- TYPE (first parameter) : the type of icon to create (see below)
- ORIGINAL_IMAGE (second parameter) : the path of the image you want to convert
- RESOURCE_NAME (third parameter) : the name of the resource to create. be aware that the tool overwrite any previous resource with the same name!
- OUTPUT_DIR (fourth parameter) : existing path in which save the set of icons (very often this will be the directory app/src/main/res inside your project)
- launcher : icons used in the home screen or in the applications list pages.
- actionbar: icons used in the navigation toolbar displayed in the upper part of the screen
- dialog: icons used in modal boxes
- tab: icons used the tabbed toolbar displayed in the bottom part of the screen
- contextual: icons used for contextual action voices
- notification: icons used in notification popup boxes