Last active
December 19, 2015 10:09
-
-
Save moltak/5938120 to your computer and use it in GitHub Desktop.
android selector generagtor
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
from os import listdir | |
import sys | |
import os | |
import string | |
xml_prefix = '<?xml version="1.0" encoding="utf-8"?>\n' \ | |
+ '<selector xmlns:android="http://schemas.android.com/apk/res/android">\n' \ | |
+ '<item android:drawable="@drawable/' | |
pressed_postfix = '" android:state_pressed="true"></item>\n' | |
normal_prefix = '<item android:drawable="@drawable/' | |
normal_postfix = '"></item>\n' \ | |
+ '</selector>' | |
dst_drawable_dir = './drawable/' | |
src_drawable_dir = './drawable-xhdpi' | |
def generage_selector(normal, pressed): | |
f = open(dst_drawable_dir + normal + '.xml', 'w') | |
f.write(xml_prefix) | |
f.write(pressed) | |
f.write(pressed_postfix) | |
f.write(normal_prefix) | |
f.write(normal) | |
f.write(normal_postfix) | |
f.close() | |
pngs = listdir(src_drawable_dir) | |
files = [p.split('.')[0] for p in pngs if string.upper(p.split('.')[1]) == 'PNG'] | |
if len(files) % 2 != 0: | |
print 'files are invalid' | |
sys.exit() | |
if os.path.exists(dst_drawable_dir) == False: | |
os.makedirs(dst_drawable_dir) | |
for i in range(len(files)/2): | |
generage_selector(files[i * 2], files[i * 2 + 1]) | |
print 'done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment