代码生成dimens.xml文件

2020-03-11  本文已影响0人  光羽隼

自己生成dimens.xml文件,我只写了一个,想要扩展的话可以研究下代码,很容易就写出自己的一套资源文件。直接运行main方法就可以了

package com.suipao.runhome;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class DimenTool {
    private static final float UNIT_DIMENS = 0.5f;

    public static void gen() {

        File file = new File("./app/src/main/res/values/dimens.xml");
        BufferedReader reader = null;
        StringBuilder sw480 = new StringBuilder();
        StringBuilder sw600 = new StringBuilder();
        StringBuilder sw720 = new StringBuilder();
        StringBuilder sw800 = new StringBuilder();
        StringBuilder w820 = new StringBuilder();
        int numInt;
        float numFloat;
        System.out.println("生成不同分辨率:");
        int line = 1;
        // 一次读入一行,直到读入null为文件结束
        sw480.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<resources>");
        for (int i = 0; i < 2000; i++) {
            if (i % 2 == 0) {
                sw480.append("<dimen name=\"dimes_").append((int) (i * UNIT_DIMENS)).append("\">").append((int) (i * UNIT_DIMENS)*2).append("px</dimen>");
            } else {
                sw480.append("<dimen name=\"dimes_").append((i * UNIT_DIMENS)).append("\">").append((int)(i * UNIT_DIMENS*2)).append("px</dimen>");
            }
            line++;
        }
        sw480.append("</resources>");
        String sw480file = "./app/src/main/res/values/dimens.xml";
        writeFile(sw480file, sw480.toString());

    }

    public static void writeFile(String file, String text) {
        PrintWriter out = null;
        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }

        out.close();
    }

    public static void main(String[] args) {
        gen();
    }
}

上一篇下一篇

猜你喜欢

热点阅读