针对不同的屏幕尺寸生成不同的大小

//存取文件的路径
private static String filePath = "d:\\layout\\values-{0}x{1}\\";
//手机屏幕的宽度为320
private static float dw = 320f;
//手机屏幕的高度为480
private static float dh = 480f;

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(1080, 1812);  
    makeString(1080, 1920);  
    makeString(1440, 2560);  
    makeString(2160, 3840);
    makeString(1200, 1920);
    makeString(2560, 1600);
    makeString(768, 1280);
}

public static void makeString(int w,int h){
    float cellw = w / dw;
    float cellh = h / dh;
    //获取任意宽度对应的像素值
    StringBuffer sbw = new StringBuffer();
    sbw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sbw.append("<resources>");
    for(int i = 1; i < 320; i ++){
        sbw.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));
    }
    sbw.append(WTemplate.replace("{0}", "320").replace("{1}", w + ""));
    sbw.append("</resources>");

    //获取任意高度对应的像素值
    StringBuffer sbh = new StringBuffer();
    sbh.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sbh.append("<resources>");
    for(int i = 1; i < 480; i ++){
        sbh.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));
    }
    sbh.append(HTemplate.replace("{0}", "480").replace("{1}", h + ""));
    sbh.append("</resources>");

    String path = filePath.replace("{0}", h + "").replace("{1}", w + "");

    File rootFile = new File(path);
    if(!rootFile.exists()){
        rootFile.mkdirs();
    }

    try{

        File wFile = new File(rootFile, "lay_x.xml");
        File hFile = new File(rootFile, "lay_y.xml");

        PrintWriter pw = new PrintWriter(new FileOutputStream(wFile));
        pw.print(sbw.toString());
        pw.close();

        pw = new PrintWriter(new FileOutputStream(hFile));
        pw.print(sbh.toString());
        pw.close();

    }catch (Exception e) {
        e.printStackTrace();
    }

}
public static float change(float a) {  
    int temp = (int) (a * 100);  
    return temp / 100f;  
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值