Created
May 19, 2014 16:32
-
-
Save akhy/7adf6595fccfa390e780 to your computer and use it in GitHub Desktop.
Android State XML Drawable Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
script by @akhy | |
generate android xml state drawables from png | |
""" | |
from sys import argv | |
from os import listdir | |
from os.path import isfile, join, splitext | |
suffix_off = "_off" | |
suffix_on = "_on" | |
targetdir = argv[1] | |
template = """<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:drawable="@drawable/{{base}}{{suffix_on}}" android:state_pressed="true"/> | |
<item android:drawable="@drawable/{{base}}{{suffix_off}}"/> | |
</selector> | |
""" | |
for f in listdir(targetdir): | |
(name, ext) = splitext(f) | |
if name.endswith(suffix_off): | |
base = name[0:-len(suffix_off)] | |
filename = "%s/%s.xml" % (targetdir, base) | |
out = open(filename, "w") | |
print filename | |
text = template | |
text = text.replace('{{base}}', base) | |
text = text.replace('{{suffix_on}}', suffix_on) | |
text = text.replace('{{suffix_off}}', suffix_off) | |
out.write(text) | |
out.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample usage:
python drawablegen.py path-to-project/drawable/xhdpi
search for files with specified suffix (e.g. _off) and generate xml drawables accordingly