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
Once you have your system up and running and the tools installed you can go on.
Create in /usr/local/bin (or any other path you are using to store your scripts/executables) a file named android_icon.sh and inside it put the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#!/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)
Currently supported icon type are:
- 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