2
\$\begingroup\$

Making an Android app involves making a lot of images of various sizes:

  • The app's launcher icon, in high/low/medium resolution
  • Buttons, menu buttons, if any, in high/low/medium resolution
  • Images for listing on Google Play: main icon, feature graphics

To simplify this, I use high-resolution source images + ImageMagick to cut to the various sizes + Makefile to only regenerate the images whose source image has changed.

For the feature graphics I cheat: I just generate a transparent canvas with the required dimensions, and overlay on top of it the app's icon, as big as it fits.

src_dir:=src
hdpi_dir:=res/drawable-hdpi
ldpi_dir:=res/drawable-ldpi
mdpi_dir:=res/drawable-mdpi

names:=$(patsubst $(src_dir)/%,%,$(wildcard $(src_dir)/*.png $(src_dir)/*.jpg))
hdpi_target:=$(patsubst %,$(hdpi_dir)/%,$(names))
ldpi_target:=$(patsubst %,$(ldpi_dir)/%,$(names))
mdpi_target:=$(patsubst %,$(mdpi_dir)/%,$(names))

appicon:=googleplay/appicon.png
feature:=googleplay/feature.png
canvas:=googleplay/canvas.png
work:=googleplay/work.png

default: all

hdpi: $(hdpi_dir) $(hdpi_target)

ldpi: $(ldpi_dir) $(ldpi_target)

mdpi: $(mdpi_dir) $(mdpi_target)

googleplay: $(appicon) $(feature)

all: hdpi ldpi mdpi googleplay

clean: 
    rm $(hdpi_target) $(ldpi_target) $(mdpi_target) $(appicon) $(feature)

$(hdpi_dir):
    @mkdir -p $@

$(ldpi_dir):
    @mkdir -p $@

$(mdpi_dir):
    @mkdir -p $@

$(hdpi_dir)/btn_%.png: $(src_dir)/btn_%.png
    convert -geometry 48x $< $@
    identify $@

$(ldpi_dir)/btn_%.png: $(src_dir)/btn_%.png
    convert -geometry 24x $< $@
    identify $@

$(mdpi_dir)/btn_%.png: $(src_dir)/btn_%.png
    convert -geometry 36x $< $@
    identify $@

$(hdpi_dir)/launcher_%.png: $(src_dir)/launcher_%.png
    convert -geometry 72x $< $@
    identify $@

$(ldpi_dir)/launcher_%.png: $(src_dir)/launcher_%.png
    convert -geometry 36x $< $@
    identify $@

$(mdpi_dir)/launcher_%.png: $(src_dir)/launcher_%.png
    convert -geometry 48x $< $@
    identify $@

$(appicon): $(src_dir)/launcher_main.png
    @mkdir -p $(@D)
    convert -geometry 512x $< $@
    identify $@

$(feature): $(src_dir)/launcher_main.png
    @mkdir -p $(@D)
    convert -size 1024x500 xc:transparent $(canvas)
    convert -geometry 1024x500 $< $(work)
    convert -composite $(canvas) $(work) -gravity west $@
    identify $@

If you want to play with this, save this script as Makefile, put an image file in src/launcher_main.png, and assuming you have ImageMagick installed, simply run make to have the images of various sizes generated in the res directory. In Android projects I have res pointing to the real resources directory of the project.

This works well, but it's a bit repetitive at some places. I'm wondering if this can be more DRY, or if there are other ways to improve.

\$\endgroup\$
2
\$\begingroup\$

Use target- and pattern-specific variables:

$(ldpi_dir)/btn_%.png : GEOMETRY := 24x
$(mdpi_dir)/btn_%.png : GEOMETRY := 36x
$(hdpi_dir)/btn_%.png : GEOMETRY := 72x

and similar definitions for launcher_%.png

Then you can combine all the individual recopies into a single one:

$(ldpi_dir)/%.png $(mdpi_dir)/%.png $(hdpi_dir)/%.png) :
    convert -geometry $(GEOMETRY) $< $@
    identify $@
\$\endgroup\$
  • \$\begingroup\$ Maybe I'm missing something but I can't get this to work. Can you please include a more complete example? \$\endgroup\$ – janos Mar 2 '15 at 22:06
  • \$\begingroup\$ Also, the ) at the end of $(ldpi_dir)/%.png $(mdpi_dir)/%.png $(hdpi_dir)/%.png) is a typo right? \$\endgroup\$ – janos Mar 2 '15 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.