Android 使用正则表达式来快速生成多套dpi及分分辨率适配的UI
2015-06-24 09:34
519 查看
在Android开发中,往往为了适配dpi和分辨率,要写多套layout或者values,而鉴于dpi和分辨率的计算方式,很多时候适配的大部分工作都是简单的计算方面。譬如DPI从1.5到2.0,就是将所有dip、sp为单位的数值都乘以0.75(也就是1.5/2)。所以我们完成可以自己写一个小工具来批量读取layout或者values文件,然后进行批量操作并保存。
对文件的批量打开读取操作我这里就不浪费周章了,大家可以自行查阅资料,这里主要贴出核心代码。
通过上述代码,我们可以发现逻辑很简单,就是找出“xxxdp”里面xxx的值,然后乘以一个转化系数CHANGE_RATIO而已,当我们要将dpi从1.5转成2.0的时候,就可以这么做了~该正则表达式适用于批量修改layout里的数值。如果是修改values里的dimens数值的话,我们可以将对应语句换为
对文件的批量打开读取操作我这里就不浪费周章了,大家可以自行查阅资料,这里主要贴出核心代码。
<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 21.6000003814697px; font-family: 'Courier New' !important;"> FileInputStream inputStream = <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> FileInputStream(subFile); BufferedReader reader </span>= <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span> BufferedReader(<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> InputStreamReader( inputStream)); </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (sb == <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">null</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">) { sb </span>= <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> StringBuffer(); } </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (String s = reader.readLine(); s != <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">null</span>; s =<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> reader.readLine()) { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;"> 使用的是逐行读取,所以xml的格式必须每行都是完整的,不然会出现各种意想不到的结果。</span> String temp =<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> s; Pattern dpPattern </span>= Pattern.compile("\"(.+?)dp\""<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); Matcher dpMatcher </span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> dpPattern.matcher(temp); System.out.println(s); </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> (dpMatcher.find()) { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> gNum = 1; gNum <= dpMatcher.groupCount(); gNum++<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">) { System.out.println(</span>"dp"<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); System.out.println(dpMatcher.group(gNum)); temp </span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> temp.replaceAll(dpMatcher.group(gNum), Float.parseFloat(dpMatcher.group(gNum)) </span>* CHANGE_RATIO + ""<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); } } Pattern dipPattern </span>= Pattern.compile("\"(.+?)dip\""<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); Matcher dipMatcher </span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> dipPattern.matcher(temp); </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> (dipMatcher.find()) { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> gNum = 1; gNum <= dipMatcher.groupCount(); gNum++<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">) { System.out.println(</span>"dip"<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); System.out.println(dipMatcher.group(gNum)); temp </span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> temp.replaceAll(dipMatcher.group(gNum), Float.parseFloat(dipMatcher.group(gNum)) </span>* CHANGE_RATIO + ""<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); } } Pattern spPattern </span>= Pattern.compile("\"(.+?)sp\""<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); Matcher spMatcher </span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> spPattern.matcher(temp); </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> (spMatcher.find()) { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> gNum = 1; gNum <= spMatcher.groupCount(); gNum++<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">) { System.out.println(</span>"sp"<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); System.out.println(spMatcher.group(gNum)); temp </span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> temp.replaceAll(spMatcher.group(gNum), Float.parseFloat(spMatcher.group(gNum)) </span>* CHANGE_RATIO + ""<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); } } sb.append(temp </span>+ "\n"<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); }</span>
通过上述代码,我们可以发现逻辑很简单,就是找出“xxxdp”里面xxx的值,然后乘以一个转化系数CHANGE_RATIO而已,当我们要将dpi从1.5转成2.0的时候,就可以这么做了~该正则表达式适用于批量修改layout里的数值。如果是修改values里的dimens数值的话,我们可以将对应语句换为
Pattern dpPattern = Pattern.compile(">(.+?)dp<");大家可以多开发一点这样的小工具,来提高平时的工作效率~
相关文章推荐
- UIControl
- 10 DelayQueue 延时队列类——Live555源码阅读(一)基本组件类
- UIButton 设置圆角 边框颜色 点击回调方法 &&图片在右文字在左
- 9 DelayQueueEntry 延时队列节点类——Live555源码阅读(一)基本组件类
- 【to be continue】五金配件,省得以后不认识
- iOS UI Label控件
- MSBuild 常用命令(Copy,Zip)
- UE4项目学习——2D跑酷游戏尝试(蓝图脚本)1
- Flash Builder找不到所需的调试器版本
- 多线程之dispatch_queue_t
- zkoss框架异常:org.zkoss.zk.ui.metainfo.PropertyNotFoundException: Method setLable not found for class or
- iOS UI 视图
- MsBuild 入门
- HDU 1423(Greatest Common Increasing Subsequence)公共最长不降子序列
- 自定义UIPageControl圆点的图片
- 自定义UIPageControl
- U9单据UI开发--单据类型UI开发
- QT国际化 一 (lupdate/linguits/lrelease)
- iOS 9: UIStackView入门
- ios 使用UIImagePickerController 打开图片库和相机选择图片修改头像