根据帧图自动生成android 动画xml文件--python脚本

本文介绍了一种使用Python脚本自动生成Android项目的XML动画文件的方法。该脚本通过用户输入来定制动画效果,如顺序、循环与否及持续时间等参数,并从指定文件夹中获取图片资源,最终生成符合Android规范的动画列表。

利用python脚本自动生成 android xml动画文件

import sys
import os
#imageName = sys.argv[1]
#num = sys.argv[2]
#order = sys.argv[1]
#duration = sys.argv[2]
#outfile = sys.argv[3]

order = raw_input("请输入顺序(True正序 False 逆序,默认False):\n")
if order.replace(' ', '')=="":
    order = False

oneshot = raw_input("是否循环(true循环 false不循环,默认true):\n")
if oneshot.replace(' ', '')=="":
    oneshot = "true"

duration = raw_input("请输入duration(默认100):\n")
if duration.replace(' ', '')=="":
    duration = 100
outfile = raw_input("请输入文件夹:\n")
outfile = outfile.replace(' ', '')
while outfile=="":
    outfile = raw_input("请输入文件夹:\n")
    outfile = outfile.replace(' ', '')



num = 10
imageName = ""
order = False
for rt, dirs, files in os.walk(outfile):
    num = len(files)
    f = files[num/2]
    imageName = os.path.splitext(f)[0]
    pos = imageName.rfind("_")
    imageName = imageName[:pos] 


#num = int(num)
duration = int(duration)
order = order == str(True)

xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<animation-list xmlns:android=\"http://schemas.android.com/apk/res/android\" android:oneshot=\"%s\">"%oneshot
n = num
while n >= 0:
    if order:
        i = num-n
    else:
        i = n

    if i<10:
        xml = xml + "\n<item android:drawable=\"@mipmap/%s_000%d\" android:duration=\"%d\" />" % (imageName,i,duration)
    else:
        xml = xml + "\n<item android:drawable=\"@mipmap/%s_00%d\" android:duration=\"%d\" />" % (imageName,i,duration)
    n = n - 1
xml = xml + "\n</animation-list>"    

print xml
with open('%s/out_xml'%outfile, 'w') as f:
    f.write(xml)

命令行中运行python脚本

$ /Users/mangues/Desktop/python_android_script/git2xml.py 
请输入顺序(True正序 False 逆序,默认False):
False
是否循环(true循环 false不循环,默认true):
true
请输入duration(默认100):
100
请输入文件夹:
/Users/mangues/Desktop/ww 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值