原文链接:在xml中“直接”使用px的小技巧和工具
注:这个idea出自知乎某个回答,但是来源找不到了。。。工具是自己写的。
在项目开发中,UI给过来的设计图上的尺寸标识,一般以px(像素)为单位。Android工程师需根据设计图的尺寸将px换成dp,填到layout.xml中。
有了这个工具,可以很方便地实现这一点。
首先,确认设计图属于下列哪种规格:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
然后安装python,运行一个脚本:
import argparse
g_type_scale_dict = {
"mdpi": 1,
"hdpi": 0.67,
"xhdpi": 0.5,
"xxhdpi": 0.33,
"xxxhdpi": 0.25
}
def main(start, end, device_type):
if device_type not in g_type_scale_dict:
print "invaild device_type", device_type
return
scale = g_type_scale_dict[device_type]
print("<!-- wrapper for px -->")
for i in range(start, end+1):
print('<dimen name="_%dpx">%.1fdp</dimen>' % (i, i*scale))
print("")
for i in range(start, end+1):
print('<dimen name="_%dspx">%.1fsp</dimen>' % (i, i*scale))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(dest="start", type=int)
parser.add_argument(dest="end", type=int)
parser.add_argument(dest="device_type")
args = parser.parse_args()
main(args.start, args.end, args.device_type)
然后在控制台中输入:
python dp_convertor.py 1 100 xxhdpi
可得到:
<!-- wrapper for px -->
<dimen name="_1px">0.3dp</dimen>
<dimen name="_2px">0.7dp</dimen>
<dimen name="_3px">1.0dp</dimen>
<dimen name="_4px">1.3dp</dimen>
<dimen name="_5px">1.7dp</dimen>
<dimen name="_6px">2.0dp</dimen>
<dimen name="_7px">2.3dp</dimen>
<dimen name="_8px">2.6dp</dimen>
<dimen name="_9px">3.0dp</dimen>
<dimen name="_10px">3.3dp</dimen>
<dimen name="_11px">3.6dp</dimen>
<dimen name="_12px">4.0dp</dimen>
<dimen name="_13px">4.3dp</dimen>
<dimen name="_14px">4.6dp</dimen>
<dimen name="_15px">5.0dp</dimen>
<dimen name="_16px">5.3dp</dimen>
<dimen name="_17px">5.6dp</dimen>
<dimen name="_18px">5.9dp</dimen>
<dimen name="_19px">6.3dp</dimen>
<dimen name="_20px">6.6dp</dimen>
<dimen name="_21px">6.9dp</dimen>
<dimen name="_22px">7.3dp</dimen>
<dimen name="_23px">7.6dp</dimen>
<dimen name="_24px">7.9dp</dimen>
<dimen name="_25px">8.2dp</dimen>
<dimen name="_26px">8.6dp</dimen>
<dimen name="_27px">8.9dp</dimen>
<dimen name="_28px">9.2dp</dimen>
<dimen name="_29px">9.6dp</dimen>
<dimen name="_30px">9.9dp</dimen>
<dimen name="_31px">10.2dp</dimen>
<dimen name="_32px">10.6dp</dimen>
<dimen name="_33px">10.9dp</dimen>
<dimen name="_34px">11.2dp</dimen>
<dimen name="_35px">11.6dp</dimen>
<dimen name="_36px">11.9dp</dimen>
<dimen name="_37px">12.2dp</dimen>
<dimen name="_38px">12.5dp</dimen>
<dimen name="_39px">12.9dp</dimen>
<dimen name="_40px">13.2dp</dimen>
<dimen name="_41px">13.5dp</dimen>
<dimen name="_42px">13.9dp</dimen>
<dimen name="_43px">14.2dp</dimen>
<dimen name="_44px">14.5dp</dimen>
<dimen name="_45px">14.9dp</dimen>
<dimen name="_46px">15.2dp</dimen>
<dimen name="_47px">15.5dp</dimen>
<dimen name="_48px">15.8dp</dimen>
<dimen name="_49px">16.2dp</dimen>
<dimen name="_50px">16.5dp</dimen>
<dimen name="_51px">16.8dp</dimen>
<dimen name="_52px">17.2dp</dimen>
<dimen name="_53px">17.5dp</dimen>
<dimen name="_54px">17.8dp</dimen>
<dimen name="_55px">18.2dp</dimen>
<dimen name="_56px">18.5dp</dimen>
<dimen name="_57px">18.8dp</dimen>
<dimen name="_58px">19.1dp</dimen>
<dimen name="_59px">19.5dp</dimen>
<dimen name="_60px">19.8dp</dimen>
<dimen name="_61px">20.1dp</dimen>
<dimen name="_62px">20.5dp</dimen>
<dimen name="_63px">20.8dp</dimen>
<dimen name="_64px">21.1dp</dimen>
<dimen name="_65px">21.4dp</dimen>
<dimen name="_66px">21.8dp</dimen>
<dimen name="_67px">22.1dp</dimen>
<dimen name="_68px">22.4dp</dimen>
<dimen name="_69px">22.8dp</dimen>
<dimen name="_70px">23.1dp</dimen>
<dimen name="_71px">23.4dp</dimen>
<dimen name="_72px">23.8dp</dimen>
<dimen name="_73px">24.1dp</dimen>
<dimen name="_74px">24.4dp</dimen>
<dimen name="_75px">24.8dp</dimen>
<dimen name="_76px">25.1dp</dimen>
<dimen name="_77px">25.4dp</dimen>
<dimen name="_78px">25.7dp</dimen>
<dimen name="_79px">26.1dp</dimen>
<dimen name="_80px">26.4dp</dimen>
<dimen name="_81px">26.7dp</dimen>
<dimen name="_82px">27.1dp</dimen>
<dimen name="_83px">27.4dp</dimen>
<dimen name="_84px">27.7dp</dimen>
<dimen name="_85px">28.1dp</dimen>
<dimen name="_86px">28.4dp</dimen>
<dimen name="_87px">28.7dp</dimen>
<dimen name="_88px">29.0dp</dimen>
<dimen name="_89px">29.4dp</dimen>
<dimen name="_90px">29.7dp</dimen>
<dimen name="_91px">30.0dp</dimen>
<dimen name="_92px">30.4dp</dimen>
<dimen name="_93px">30.7dp</dimen>
<dimen name="_94px">31.0dp</dimen>
<dimen name="_95px">31.4dp</dimen>
<dimen name="_96px">31.7dp</dimen>
<dimen name="_97px">32.0dp</dimen>
<dimen name="_98px">32.3dp</dimen>
<dimen name="_99px">32.7dp</dimen>
<dimen name="_100px">33.0dp</dimen>
<dimen name="_1spx">0.3sp</dimen>
<dimen name="_2spx">0.7sp</dimen>
<dimen name="_3spx">1.0sp</dimen>
<dimen name="_4spx">1.3sp</dimen>
<dimen name="_5spx">1.7sp</dimen>
<dimen name="_6spx">2.0sp</dimen>
<dimen name="_7spx">2.3sp</dimen>
<dimen name="_8spx">2.6sp</dimen>
<dimen name="_9spx">3.0sp</dimen>
<dimen name="_10spx">3.3sp</dimen>
<dimen name="_11spx">3.6sp</dimen>
<dimen name="_12spx">4.0sp</dimen>
<dimen name="_13spx">4.3sp</dimen>
<dimen name="_14spx">4.6sp</dimen>
<dimen name="_15spx">5.0sp</dimen>
<dimen name="_16spx">5.3sp</dimen>
<dimen name="_17spx">5.6sp</dimen>
<dimen name="_18spx">5.9sp</dimen>
<dimen name="_19spx">6.3sp</dimen>
<dimen name="_20spx">6.6sp</dimen>
<dimen name="_21spx">6.9sp</dimen>
<dimen name="_22spx">7.3sp</dimen>
<dimen name="_23spx">7.6sp</dimen>
<dimen name="_24spx">7.9sp</dimen>
<dimen name="_25spx">8.2sp</dimen>
<dimen name="_26spx">8.6sp</dimen>
<dimen name="_27spx">8.9sp</dimen>
<dimen name="_28spx">9.2sp</dimen>
<dimen name="_29spx">9.6sp</dimen>
<dimen name="_30spx">9.9sp</dimen>
<dimen name="_31spx">10.2sp</dimen>
<dimen name="_32spx">10.6sp</dimen>
<dimen name="_33spx">10.9sp</dimen>
<dimen name="_34spx">11.2sp</dimen>
<dimen name="_35spx">11.6sp</dimen>
<dimen name="_36spx">11.9sp</dimen>
<dimen name="_37spx">12.2sp</dimen>
<dimen name="_38spx">12.5sp</dimen>
<dimen name="_39spx">12.9sp</dimen>
<dimen name="_40spx">13.2sp</dimen>
<dimen name="_41spx">13.5sp</dimen>
<dimen name="_42spx">13.9sp</dimen>
<dimen name="_43spx">14.2sp</dimen>
<dimen name="_44spx">14.5sp</dimen>
<dimen name="_45spx">14.9sp</dimen>
<dimen name="_46spx">15.2sp</dimen>
<dimen name="_47spx">15.5sp</dimen>
<dimen name="_48spx">15.8sp</dimen>
<dimen name="_49spx">16.2sp</dimen>
<dimen name="_50spx">16.5sp</dimen>
<dimen name="_51spx">16.8sp</dimen>
<dimen name="_52spx">17.2sp</dimen>
<dimen name="_53spx">17.5sp</dimen>
<dimen name="_54spx">17.8sp</dimen>
<dimen name="_55spx">18.2sp</dimen>
<dimen name="_56spx">18.5sp</dimen>
<dimen name="_57spx">18.8sp</dimen>
<dimen name="_58spx">19.1sp</dimen>
<dimen name="_59spx">19.5sp</dimen>
<dimen name="_60spx">19.8sp</dimen>
<dimen name="_61spx">20.1sp</dimen>
<dimen name="_62spx">20.5sp</dimen>
<dimen name="_63spx">20.8sp</dimen>
<dimen name="_64spx">21.1sp</dimen>
<dimen name="_65spx">21.4sp</dimen>
<dimen name="_66spx">21.8sp</dimen>
<dimen name="_67spx">22.1sp</dimen>
<dimen name="_68spx">22.4sp</dimen>
<dimen name="_69spx">22.8sp</dimen>
<dimen name="_70spx">23.1sp</dimen>
<dimen name="_71spx">23.4sp</dimen>
<dimen name="_72spx">23.8sp</dimen>
<dimen name="_73spx">24.1sp</dimen>
<dimen name="_74spx">24.4sp</dimen>
<dimen name="_75spx">24.8sp</dimen>
<dimen name="_76spx">25.1sp</dimen>
<dimen name="_77spx">25.4sp</dimen>
<dimen name="_78spx">25.7sp</dimen>
<dimen name="_79spx">26.1sp</dimen>
<dimen name="_80spx">26.4sp</dimen>
<dimen name="_81spx">26.7sp</dimen>
<dimen name="_82spx">27.1sp</dimen>
<dimen name="_83spx">27.4sp</dimen>
<dimen name="_84spx">27.7sp</dimen>
<dimen name="_85spx">28.1sp</dimen>
<dimen name="_86spx">28.4sp</dimen>
<dimen name="_87spx">28.7sp</dimen>
<dimen name="_88spx">29.0sp</dimen>
<dimen name="_89spx">29.4sp</dimen>
<dimen name="_90spx">29.7sp</dimen>
<dimen name="_91spx">30.0sp</dimen>
<dimen name="_92spx">30.4sp</dimen>
<dimen name="_93spx">30.7sp</dimen>
<dimen name="_94spx">31.0sp</dimen>
<dimen name="_95spx">31.4sp</dimen>
<dimen name="_96spx">31.7sp</dimen>
<dimen name="_97spx">32.0sp</dimen>
<dimen name="_98spx">32.3sp</dimen>
<dimen name="_99spx">32.7sp</dimen>
<dimen name="_100spx">33.0sp</dimen>
将以上复制到你项目的dimens.xml中,以后可以直接按照设计图的px值,在layout.xml中写@dimen/_**px
了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。