Android屏幕适配方案(百分比)

http://blog.csdn.net/lmj623565791/article/details/45460089
本文基础:【张鸿洋的博客】
在这个基础上自己稍微修改了一下,原文是以320×480这个分辨率为基础来生成的values文件。我在基础上增加了完全按照百分比的形式生成values文件。

增加两个变量,用来生成对应values文件。

    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 String WPercent = "<dimen name=\"w{0}\">{1}px</dimen>\n";
    private final static String HPercent = "<dimen name=\"h{0}\">{1}px</dimen>\n";

修改了这个生成文件

    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 = 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 < 1000; i++) {
            if (i % 10 == 0) {
                sbForWidth.append(WPercent.replace("{0}", i / 10 + "").replace("{1}",
                        change(i * 0.001f * w) + ""));
            }else{
                sbForWidth.append(WPercent.replace("{0}", i / 10 + "." + i % 10).replace("{1}",
                        change(i * 0.001f * w) + ""));
            }
        }
        sbForWidth.append(WPercent.replace("{0}", "100.0").replace("{1}",
                change(1f * 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 = 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 < 1000; i++) {
            if (i % 10 == 0) {
                sbForHeight.append(HPercent.replace("{0}", i / 10 + "").replace("{1}",
                        change(i * 0.001f * h) + ""));
            }else {
                sbForHeight.append(HPercent.replace("{0}", i / 10 + "." + i % 10).replace("{1}",
                        change(i * 0.001f * h) + ""));
            }
        }
        sbForHeight.append(HPercent.replace("{0}", "100").replace("{1}",
                change(1f * 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();
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值