生成不同尺寸dimen的xml文件以及文件夹

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class MakeXml {
    private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\";

    private final static float dw = 480f;
    private final static float dh = 800f;

    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";

    private final static int[] otherPxX = new int[]{};
    private final static int[] otherPxY = new int[]{};

    public static void main(String[] args) {
//        makeString(320, 480);
        makeString(480, 800);
        makeString(480, 640);
//        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(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;
        int tempxCount = (int) (dw * 2 / 3);
        for (int i = 1; i < tempxCount + 1; i++) {
            sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellw * i) + ""));
        }

        for (int i = 0; i < otherPxX.length; i++) {
            int temp = otherPxX[i];
            if (temp < dw && temp > tempxCount) {
                sb.append(WTemplate.replace("{0}", temp + "").replace("{1}",
                        change(cellw * temp) + ""));
            }
        }

//        sb.append(WTemplate.replace("{0}", "320").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;
        int tempyCount = (int) (dh / 3);
        for (int i = 1; i < tempyCount + 1; i++) {
            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellh * i) + ""));
        }

        for (int i = 0; i < otherPxY.length; i++) {
            int temp = otherPxY[i];
            if (temp <= dh && temp > tempyCount) {
                sb.append(WTemplate.replace("{0}", temp + "").replace("{1}",
                        change(cellw * temp) + ""));
            }
        }

//        sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + ""));
        sb2.append("</resources>");

        String path = rootPath.replace("{0}", w + "").replace("{1}", h + "");
        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");
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new FileOutputStream(layxFile));
            pw.print(sb.toString());
            pw.close();
            pw = new PrintWriter(new FileOutputStream(layyFile));
            pw.print(sb2.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (pw != null)
                pw.close();
        }

    }

    public static float change(float a) {
        int temp = (int) (a * 100);
        return temp / 100f;
    }
}
时间: 2024-07-28 20:17:36

生成不同尺寸dimen的xml文件以及文件夹的相关文章

eclipse或adt-bundle创建的android项目没有自动生成MainActivity.java和activity_main.xml等文件解决办法

以前我电脑一直以来都是用的eclipse3.7来开发android项目的,创建android项目也能正常生成MainActivity.java和activity_main.xml等文件.后来不知道什么原因,电脑几个盘上的文件很多打开都显示乱码.找了很多方法都无法解决,所以就把硬盘给格式化重装了系统. 重装系统后,重新配置了eclipse的android开发环境,但发现创建的android项目不能正常生成MainActivity.java和activity_main.xml等文件,网上找了很多方法

沫沫金-Mybatis工具类,生成dao层xml、mapper文件和实体类entity层

Mybatis Generator工具使用起来,总感觉不太灵活加上初次环境配置麻烦,特编写java文件 单文件不依赖,直接生成.源码如下(此为Oracle数据库版) package net.icarefx.booking.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Outpu

Nginx+PHP实时生成不同尺寸图片

原来图片服务器采用Windows .net架构,鉴于需求需要生成各种尺寸图片. 流程说明: 用户从Nginx请求对应的图片,判断是否存在_200x300的对应参数,如果没有就直接请求到对应目录的原图,否则继续判断是否在本地已经生成了对应的缓存图片,如果存在返回已经生成过的定制尺寸图片,否则请求PHP动态生成. Nginx部分配置:     server {         listen       80;         server_name  pics.abc.com;         lo

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ?Copyright 蕃薯耀 2017年9月13日 http://www.cnblogs.com/fanshuyao/ 直接上代码: import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.ref

python-操作xml格式的文件

xml文件的格式如下: <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year updated="yes">2009</year> <gdppc>141100</gdppc> <neighbor direction="E" name=&q

Caused by: org.xml.sax.SAXParseException: 文件提前结束。

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.2019-05-11 12:24:20.657 ERROR 59648 --- [ main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans

C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码

本文介绍在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码.网上文章大多只是简单介绍内置参数的设置,根据我的使用目的,增加了自定义目标二维码图片尺寸和白边边框.有需要的朋友们可以试一下,如有bug欢迎指正. 首先,将 ThoughtWorks.QRCode.dll 放在 bin 目录后,在页面中引用: using ThoughtWorks.QRCode.Codec; 生成二维码图片: 1 2 3 4 5 6 7 8 9 10 11 12 13 14

js便签笔记(8)——js加载XML字符串或文件

1. 加载XML文件 方法1:ajax方式.代码如下: var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); xhr.open("GET", "data.xml", false); xhr.send(null); var xmlDoc = xhr.responseXML; console.log(xmlDoc

php生成百度站点地图sitemap.xml

<?php header("Content-type:text/html;charset=utf-8"); //php生成百度站点地图sitemap.xml //http://www.baidu.com/search/sitemaptools_help.html include("../config.inc.php"); mysql_connect($CFG['db_host'] ,$CFG['db_user'],$CFG['db_pass']) or die