初学Android适配——生成适配文件的工具类MakeXml
2016-12-02 17:33
441 查看
package sliding.pz.com.makexml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; /** * Created by lenovo on 2016/12/2. */ public class MakeXml { // 生成地址 C盘layoutroot目录下 private final static String rootPath = "C:\\layoutroot\\values-{0}x{1}\\"; /** * 设置基准分辨率 * 一般标注按照多大的图标,这里我们就设置多大尺寸 */ private final static float dw = 720f; private final static float dh = 1280f; private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n"; private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n"; // 手机分辨率 public static void main(String [] args){ makeString(320, 480); makeString(480, 800); makeString(480, 854); makeString(540, 960); makeString(600, 1024); makeString(720, 1184); makeString(720, 1196); makeString(720, 1280); makeString(768, 1024); makeString(800, 1280); makeString(900, 1440); makeString(1080, 1812); makeString(1080, 1920); makeString(1440, 2560); } public static void makeString(int w, int h) { StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); sb.append("<resources>"); float cellw = w / dw; for (int i = 1; i < dw; i++) { sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + "")); } sb.append(WTemplate.replace("{0}", "720").replace("{1}", w + "")); sb.append("</resources>"); StringBuffer sb2 = new StringBuffer(); sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); sb2.append("<resources>"); float cellh = h / dh; for (int i = 1; i < dh; i++) { sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + "")); } sb2.append(HTemplate.replace("{0}", "1280").replace("{1}", h + "")); sb2.append("</resources>"); String path = rootPath.replace("{0}", h + "").replace("{1}", w + ""); File rootFile = new File(path); if (!rootFile.exists()) { rootFile.mkdirs(); } File layxFile = new File(path + "lay_x.xml"); File layyFile = new File(path + "lay_y.xml"); try { PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile)); pw.print(sb.toString()); pw.close(); pw = new PrintWriter(new FileOutputStream(layyFile)); pw.print(sb2.toString()); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static float change(float a) { int temp = (int) (a * 100); return temp / 100f; } }
相关文章推荐
- 自动生成Android屏幕适配的dimens.xml文件工具类
- Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件
- Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件
- Android开发——自动生成Android屏幕适配的dimens.xml文件
- Android程序调试时生成main.out.xml文件问题,及完美解决方案.
- android利用Pull解析器解析、生成XMl文件
- 【Android网络开发の3】XML之PULL方式 解析和生成XML文件
- Android-Xml序列化器生成短信xml文件
- Android 上 xml Layout view 生成pdf文件
- Android_xml_pull解析和生成xml文件
- Android开发之XML文件生成
- 自动生成Android屏幕适配的dimens.xml
- 根据Android Layout XM文件自动生成自定义View代码的工具类
- android屏幕适配,自动生成不同的dimens.xml详解
- Android程序调试时生成main.out.xml文件,报错: Resource entry main is already defined
- android编译时生成了一个 main.out.xml文件,无法编译的解决办法
- android 生成、pull解析xml文件
- Android下使用pull解析器生成XML文件、读取XML文件
- android解析和生成XML文件
- 自动生成android dimens.xml适配屏幕