博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android适配文件dimen自动生成代码
阅读量:7041 次
发布时间:2019-06-28

本文共 5950 字,大约阅读时间需要 19 分钟。

1:保存下,别人的code

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;/** *   */public class GenerateValueFiles {    private int baseW;    private int baseH;    private String dirStr = "./res";    private final static String WTemplate = "
{1}px
\n"; private final static String WTemplate1 = "
{1}px
\n"; private final static String HTemplate = "
{1}px
\n"; private final static String HTemplate1 = "
{1}px
\n"; /** * {0}-HEIGHT */ private final static String VALUE_TEMPLATE = "values-{0}x{1}"; private static final String SUPPORT_DIMESION = "320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;768,1280;800,1280;1080,1812;1080,1920;1440,2560;"; private String supportStr = SUPPORT_DIMESION; public GenerateValueFiles(int baseX, int baseY, String supportStr) { this.baseW = baseX; this.baseH = baseY; if (!this.supportStr.contains(baseX + "," + baseY)) { this.supportStr += baseX + "," + baseY + ";"; } this.supportStr += validateInput(supportStr); System.out.println(supportStr); File dir = new File(dirStr); if (!dir.exists()) { dir.mkdir(); } System.out.println(dir.getAbsoluteFile()); } /** * @param supportStr * w,h_...w,h; * @return */ private String validateInput(String supportStr) { StringBuffer sb = new StringBuffer(); String[] vals = supportStr.split("_"); int w = -1; int h = -1; String[] wh; for (String val : vals) { try { if (val == null || val.trim().length() == 0) continue; wh = val.split(","); w = Integer.parseInt(wh[0]); h = Integer.parseInt(wh[1]); } catch (Exception e) { System.out.println("skip invalidate params : w,h = " + val); continue; } sb.append(w + "," + h + ";"); } return sb.toString(); } public void generate() { String[] vals = supportStr.split(";"); for (String val : vals) { String[] wh = val.split(","); generateXmlFile(Integer.parseInt(wh[0]), Integer.parseInt(wh[1])); } } private void generateXmlFile(int w, int h) { StringBuffer sbForWidth = new StringBuffer(); sbForWidth.append("
\n"); sbForWidth.append("
"); float cellw = w * 1.0f / baseW; System.out.println("width : " + w + "," + baseW + "," + cellw); for (int i = 1; i < baseW; i++) { sbForWidth.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + "")); } sbForWidth.append(WTemplate.replace("{0}", baseW + "").replace("{1}", w + "")); for (int i = 1; i < baseW; i++) { sbForWidth.append(WTemplate1.replace("{0}", i + "").replace("{1}", change(cellw * (-i)) + "")); } sbForWidth.append(WTemplate1.replace("{0}", baseW + "").replace("{1}", -w + "")); sbForWidth.append("
"); StringBuffer sbForHeight = new StringBuffer(); sbForHeight.append("
\n"); sbForHeight.append("
"); float cellh = h *1.0f/ baseH; System.out.println("height : "+ h + "," + baseH + "," + cellh); for (int i = 1; i < baseH; i++) { sbForHeight.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + "")); } sbForHeight.append(HTemplate.replace("{0}", baseH + "").replace("{1}", h + "")); for (int i = 1; i < baseH; i++) { sbForHeight.append(HTemplate1.replace("{0}", i + "").replace("{1}", change(cellh * (-i)) + "")); } sbForHeight.append(HTemplate1.replace("{0}", baseH + "").replace("{1}", -h + "")); sbForHeight.append("
"); File fileDir = new File(dirStr + File.separator + VALUE_TEMPLATE.replace("{0}", h + "")// .replace("{1}", w + "")); fileDir.mkdir(); File layxFile = new File(fileDir.getAbsolutePath(), "lay_x.xml"); File layyFile = new File(fileDir.getAbsolutePath(), "lay_y.xml"); try { PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile)); pw.print(sbForWidth.toString()); pw.close(); pw = new PrintWriter(new FileOutputStream(layyFile)); pw.print(sbForHeight.toString()); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static float change(float a) { int temp = (int) (a * 100); return temp / 100f; } public static void main(String[] args) { int baseW = 320; int baseH = 480; String addition = ""; try { if (args.length >= 3) { baseW = Integer.parseInt(args[0]); baseH = Integer.parseInt(args[1]); addition = args[2]; } else if (args.length >= 2) { baseW = Integer.parseInt(args[0]); baseH = Integer.parseInt(args[1]); } else if (args.length >= 1) { addition = args[0]; } } catch (NumberFormatException e) { System.err .println("right input params : java -jar xxx.jar width height w,h_w,h_..._w,h;"); e.printStackTrace(); System.exit(-1); } new GenerateValueFiles(baseW, baseH, addition).generate(); }}

2:jar包

链接: https://pan.baidu.com/s/1qYhZMrE 密码: gv2x

 

转载于:https://www.cnblogs.com/galibujianbusana/p/7612371.html

你可能感兴趣的文章
八皇后问题--递归与非递归的实现
查看>>
JAVA系列之JVM加载
查看>>
[LeetCode] replace into a Cyclic Sorted List
查看>>
$.each 和$(selector).each()的区别
查看>>
在Xcode中使用Git进行源码版本控制
查看>>
Lync 2013无法登录到lync,提示服务器暂时不可用
查看>>
Linux下PHP网站安全加固配置
查看>>
做自动化测试之前需要了解的
查看>>
Linux终端中文字错位解决
查看>>
再谈swap
查看>>
文本处理三剑客之-sed基础用法
查看>>
宏正ATEN发行全球首款Cat 5双滑轨19寸LCD KVM切换器
查看>>
consui(二)集群配置
查看>>
Windows Cluster 常用命令
查看>>
AndroidStudio生成jar、so、aar以及上传远程库jcenter
查看>>
Redis 过期键的设置、获取和删除过期时间
查看>>
我的友情链接
查看>>
word,excel,网页上如何打x的n次方
查看>>
Cacti(系统监控)
查看>>
Ubuntu 12.04 修改/etc/resolv.conf重启后还原成修改前状态解决办法
查看>>