Android适配文件dimen自动生成代码
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 = "<dimen name=\"x{0}\">{1}px</dimen>\n";
private final static String WTemplate1 = "<dimen name=\"_x{0}\">{1}px</dimen>\n";
private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
private final static String HTemplate1 = "<dimen name=\"_y{0}\">{1}px</dimen>\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 = -;
int h = -;
String[] wh;
for (String val : vals) {
try {
if (val == null || val.trim().length() == )
continue; wh = val.split(",");
w = Integer.parseInt(wh[]);
h = Integer.parseInt(wh[]);
} 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[]), Integer.parseInt(wh[]));
} } private void generateXmlFile(int w, int h) { StringBuffer sbForWidth = new StringBuffer();
sbForWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sbForWidth.append("<resources>");
float cellw = w * 1.0f / baseW; System.out.println("width : " + w + "," + baseW + "," + cellw);
for (int i = ; 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 = ; 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("</resources>"); StringBuffer sbForHeight = new StringBuffer();
sbForHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sbForHeight.append("<resources>");
float cellh = h *1.0f/ baseH;
System.out.println("height : "+ h + "," + baseH + "," + cellh);
for (int i = ; 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 = ; 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("</resources>"); 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 * );
return temp / 100f;
} public static void main(String[] args) {
int baseW = ;
int baseH = ;
String addition = "";
try {
if (args.length >= ) {
baseW = Integer.parseInt(args[]);
baseH = Integer.parseInt(args[]);
addition = args[];
} else if (args.length >= ) {
baseW = Integer.parseInt(args[]);
baseH = Integer.parseInt(args[]);
} else if (args.length >= ) {
addition = args[];
}
} 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(-);
} new GenerateValueFiles(baseW, baseH, addition).generate();
} }
2:jar包
链接: https://pan.baidu.com/s/1qYhZMrE 密码: gv2x
Android适配文件dimen自动生成代码的更多相关文章
- 安卓自动生成代码插件-Android code Generator(转)
编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 介绍 A ...
- mybatis 自动生成代码(mybatis generator)
pom.xml 文件配置 引入 mybatis generator <properties> <mysql.connector.version>5.1.44</mysql ...
- ButterKnife的使用以及不能自动生成代码问题的解决
ButterKnife的使用以及不能自动生成代码问题的解决 转载请注明出处:http://www.cnblogs.com/zhengjunfei/p/5910497.html 最近换了个工作刚入职,又 ...
- Eclipse 使用mybatis generator插件自动生成代码
Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...
- 组件化框架设计之apt编译时期自动生成代码&动态类加载(二)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 本篇文章将继续从以下两个内容来介绍组件化框架设计: apt编译时 ...
- mybatis generator maven插件自动生成代码
如果你正为无聊Dao代码的编写感到苦恼,如果你正为怕一个单词拼错导致Dao操作失败而感到苦恼,那么就可以考虑一些Mybatis generator这个差价,它会帮我们自动生成代码,类似于Hiberna ...
- java如何在eclipse编译时自动生成代码
用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...
- MyBatis自动生成代码示例
在项目中使用到mybatis时,都会选择自动生成实体类,Mapper,SqlMap这三个东东. 手头上在用的又不方便,找了下网上,其实有很多文章,但有些引用外部文件时不成功,也不方便,所以重新整理了下 ...
- Eclipse中R文件不能自动生成
R文件不能自动生成主要是因为编译有错误,这时你想什么办法都是没有用的,clean, fix properties,都不是从根上解决问题. R文件主要是自动生成资源文件的id的,里边静态子类 ...
随机推荐
- BZOJ 1594: [Usaco2008 Jan]猜数游戏 线段树 + 思维 + 二分
Code: #include<bits/stdc++.h> #define maxn 3000000 using namespace std; void setIO(string s) { ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...
- eas快捷键
ctrl+shift+c 获取分录行的id ctrl+alt+[ 获取任意界面操作的信息
- 编写输出Hellow word!
打开java运行环境(eclipse),然后输入以下语句,点击编译运行后即可输出“Hello World,”. public class HelloWorld { public static void ...
- PHP和zookeeper结合实践
Zookeeper 简单介绍 Apache Zookeeper是开发和维护开源服务器的服务,它能够实现高度可靠的分布式协调. 安装Zookeeper(无需安装) wget http://mirror. ...
- ecshop ad调用指定广告的方法 邓士鹏
在include/lib_goods.php文件下面新增:function getads($cat,$num){$time = gmtime();$sql = "SELECT * FRO ...
- 图论·Dijkstra·HDU2066
这道题刚做的时候用的Floyd,果断超时,于是去学了Dijkstra,主函数和Floyd很像. 原理: 从起点开始,找最近的又未标记的点,记录距离,标记此点,再找此点附近相连的未标记的点,记录下距离, ...
- jplogic v1.0案例开发之知识库管理(文档管理等)(二)
jplogic v1.0开发陆续更新,意在和广大网友分享交流.通过寻求合作伙伴,交流群.以下是jplogic的关于知识库模块的部分功能,例如以下进行功能展示: 知识库主界面: 新增知识类别: wate ...
- redis之Hash存储与String存储内存消耗对照
存储对象User String存储方式: SET media:1155315 939 GET media:1155315 > 939 String结构存储该对象 User243 243600 存 ...
- Codeforces 115A- Party(DFS)
A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...