Android屏幕适配万能尺寸生成方法
本方法主要是根据各机型的屏幕像素比例来进行适配,当然你也可以根据自己的需求改成以dp为单位的尺寸适配,但是我个人觉得还是以px为单位好弄些
将此代码复制进自己的Android项目中 根据自己的需求修改后,执行main方法即可.
代码块
public static void main(String[] args)
throws Exception {
float[] base = {
480,
854};
copys(resStr, base,
"320,480;480,800;480,854;");
}
private final static String VALUE_TEMPLATE =
"values-{0}x{1}";
private static final String SUPPORT_DIMESION =
"320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;768,1280;800,1280;1080,1812;1080,1920;1440,2560;";
/**
* 主要原理就是将已经写好适配数据的dimens文件复制进需要适配的机型响应的values文件夹里,并在复制的过程中修改里面的尺寸值.
* 但你要确保你的已经写好的适配文件里的数值是以此格式写的
* x轴的数据(即控件的宽度)以"x_"开头
* y轴的数据(即控件的高度)以"y_"开头
* 值以px为单位,当然也可以自己将此方法改成以dp为单位
* 其中文字大小可以用px为单位来写,以什么格式开头("x_"/"y_")随你,这样适配效果更好
* <dimen name="x_187.5">187.5px</dimen>
* <dimen name="x_77.3">77.3px</dimen>
* <dimen name="y_62.7">62.7px</dimen>
* <dimen name="y_102.7">102.7px</dimen>
* @param resStr res文件夹路径
* @param base 标准宽度与标准高度
* @param supportDimesion 需要生成的尺寸 可以直接在{@link ValuesFormat#SUPPORT_DIMESION}里找到自己需要适配的尺寸,加入supportDimesion中,也可以自定义尺寸添加
* @throws IOException
*/
private static void copys(File resStr,
float[] base, String supportDimesion)
throws IOException {
File file =
new File(
new File(resStr + File.separator +
"values"),
"dimens.xml");
float baseW = base[
0];
float baseH = base[
1];
int x =
0;
int y =
0;
String[] vals = supportDimesion.split(
";");
for (String val : vals) {
String[] wh = val.split(
",");
x = Integer.parseInt(wh[
0]);
y = Integer.parseInt(wh[
1]);
File valuesDir =
new File(resStr + File.separator +
VALUE_TEMPLATE.replace(
"{0}", x +
"").replace(
"{1}", y +
""));
File valuesFile =
new File(valuesDir,
"dimens.xml");
if (!valuesDir.exists()) {
valuesDir.mkdir();
}
float scaleW = x / baseW;
float scaleH = y / baseH;
float scale;
BufferedReader bufferedReader =
new BufferedReader(
new FileReader(file));
BufferedWriter bufferedWriter =
new BufferedWriter(
new FileWriter(valuesFile));
String line;
String lines;
while ((line = bufferedReader.readLine()) !=
null) {
int x_start = line.indexOf(
"x_");
int y_start = line.indexOf(
"y_");
int xy_start = line.indexOf(
"x_");
int xy_end = line.indexOf(
"\"", xy_start);
int x_end = line.indexOf(
"\"", x_start);
int y_end = line.indexOf(
"\"", y_start);
if (x_start != -
1 || y_start != -
1) {
if (x_start != -
1) {
scale = scaleW;
xy_start = x_start;
xy_end = x_end;
}
else {
xy_start = y_start;
xy_end = y_end;
scale = scaleH;
}
int value_start = line.indexOf(
">", xy_start) +
1;
int value_end = line.indexOf(
"px");
String value = line.substring(xy_start +
2, xy_end);
String value_a_s = line.substring(value_start, value_end);
float value_a = Float.valueOf(value);
float value_b = value_a * scale;
String value_b_s =
new Formatter().format(
"%.2f", value_b).toString();
String replace = line.replace(value_a_s +
"px", value_b_s +
"px");
System.out.println(line);
System.out.println(replace);
System.out.println((x_start != -
1 ?
"X" :
"Y") +
"方向上" +
"尺寸适配比例:" + scale +
"起始位置:" + xy_start +
"结束位置:" + xy_end +
"取到的值名:" + value
+
"取到的值:" + value_a_s +
"标准值:" + value_a +
"转换后的值:" + value_b_s +
":" + value_b);
lines = replace;
}
else {
lines = line;
}
bufferedWriter.write(lines);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
}
}
代码下载
http://download.csdn.net/detail/prczhb/9922455