Skip to content

Instantly share code, notes, and snippets.

@benvium
Last active October 19, 2016 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benvium/8352993 to your computer and use it in GitHub Desktop.
Save benvium/8352993 to your computer and use it in GitHub Desktop.
Android app script for Android Studio / Android SDK. This script resizes android xhdpi-sized png image assets into the ldpi, mdpi, hdpi, xhdpi folders. Now just maintain one folder of images rather than 4 or 5. Updated to create directories if needed and also to separately convert images in the mipmap folder (used for app icons)
#!/bin/bash -e
# Ensure we're running in location of script.
cd "`dirname $0`"
function fail {
echo "FAILED with $1"
exit 1
}
function doConversion() {
local folderName=$1
echo "Using $folderName..."
cd "res/$folderName-xhdpi" || fail "No $folderName-xhdpi folder"
for f in *; do
if [[ ${f} == *.png ]];
then
echo "...$f"
if [[ ${f} != *.9.png ]];
then
# ensure folders exist
mkdir -p "../$folderName-xhdpi" || fail "can't make folder"
mkdir -p "../$folderName-hdpi" || fail "can't make folder"
mkdir -p "../$folderName-mdpi" || fail "can't make folder"
mkdir -p "../$folderName-ldpi" || fail "can't make folder"
convert ${f} -resize 75% "../$folderName-hdpi/${f}" || fail "Failed with ../$folderName-hdpi/${f}"
convert ${f} -resize 50% "../$folderName-mdpi/${f}" || fail "Failed with ../$folderName-mdpi/${f}"
convert ${f} -resize 37.5% "../$folderName-ldpi/${f}" || fail "Failed with ../$folderName-ldpi/${f}"
else
echo "not resizing ${f} as it's a 9-patch"
fi
fi
done
cd -
}
# Convert graphic images
doConversion "drawable"
# Convert app icon (now goes in the mipmap folder)
doConversion "mipmap"
echo Complete
@benvium
Copy link
Author

benvium commented Jan 10, 2014

You should place this script at the same level as the 'res' folder. Put your master images in the xhdpi folder and then run the script.

@alin-rosu
Copy link

I tried to run it on mac and it says: convert: command not found. Am I missing something?

@benvium
Copy link
Author

benvium commented Aug 7, 2015

This script depends on ImageMagick - try this http://cactuslab.com/imagemagick/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment