package com.kang.makedimen;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MakeDimen {
// //resoluton format: H*W
private static final String[][] RESOLUTION_ARRAYS = { { "640", "480" },
{ "854", "480" }, { "800", "480" }, { "800", "600" },
{ "960", "540" }, { "1280", "720" }, { "1920", "1080" } };
private static final String XML_BEGIN = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>";
private static final String XML_END = "</resources>";
// ///
private static final String BASE_RESOLUTION[] = RESOLUTION_ARRAYS[0];// set
// baseline
// type
private static final boolean IS_NEED_HEIGHT = true;
private static final String OUTPUT_DIR = "D:\\Android_dimen_kangx\\";
// ///
/**
* @param args
*/
public static void main(String[] args) {
createAllDimenFile();
}
private static void createAllDimenFile() {
for (int i = 0; i < RESOLUTION_ARRAYS.length; i++) {
String path = getDimenFilePath(RESOLUTION_ARRAYS[i]);
String fileContent = makeContent(RESOLUTION_ARRAYS[i]);
createFile(path, fileContent);
}
String path = getDefaultDimenFilePath();
String fileContent = makeContent(RESOLUTION_ARRAYS[0]);
createFile(path, fileContent);
}
private static void createFile(String path, String fileContent) {
File file = new File(path);
if (file.exists()) {
file.delete();
}
try {
file.getParentFile().mkdirs();
if (file.createNewFile()) {
FileWriter writer = new FileWriter(file);
writer.write(fileContent);
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String makeContent(String[] resolution) {
int maxHeigth = Integer.valueOf(resolution[0]).intValue();
int maxWidth = Integer.valueOf(resolution[1]).intValue();
int baseLineMaxHeigth = Integer.valueOf(BASE_RESOLUTION[0]).intValue();
int baseLineMaxWidth = Integer.valueOf(BASE_RESOLUTION[1]).intValue();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(XML_BEGIN).append("\n");
for (int i = 1; i < maxWidth + 1; i++) {
int width = maxWidth * i / baseLineMaxWidth;
String item = makeWItem(i, width);
stringBuffer.append(item).append("\n");
}
if (IS_NEED_HEIGHT) {
for (int i = 1; i < maxHeigth + 1; i++) {
int height = maxHeigth * i / baseLineMaxHeigth;
String item = makeHItem(i, height);
stringBuffer.append(item).append("\n");
}
}
stringBuffer.append(XML_END);
return stringBuffer.toString();
}
private static String makeWItem(int index, int px) {
return "<dimen name=\"W" + index + "px\">" + px + "px</dimen>";
}
private static String makeHItem(int index, int px) {
return "<dimen name=\"H" + index + "px\">" + px + "px</dimen>";
}
private static String getDimenFilePath(String[] resolution) {
String resolutionFloder = "values-" + resolution[0] + "x"
+ resolution[1] + "\\";
return OUTPUT_DIR + resolutionFloder + "dimen.xml";
}
private static String getDefaultDimenFilePath() {
String resolutionFloder = "values" + "\\";
return OUTPUT_DIR + resolutionFloder + "dimen.xml";
}
}
自动生成dimen
最新推荐文章于 2021-01-26 14:57:04 发布