Android屏幕适配万能尺寸生成方法

xiaoxiao2021-02-28  137

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}"; //"x1,y1;x2,y2;" 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 { //标准文件位置 ".\app\src\main\res\values\dimens.xml" 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) { //<dimen name="xy10">187.5px</dimen> //转换定位 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);//10 String value_a_s = line.substring(value_start, value_end);//187.5 //标准值 float value_a = Float.valueOf(value);//10.0 //转换比例后目标值 float value_b = value_a * scale;//15.0 //保留两位小数 String value_b_s = new Formatter().format("%.2f", value_b).toString(); //数值转换后的字符串 String replace = line.replace(value_a_s + "px", value_b_s + "px"); //标准dimens里的每一行需要更改的内容 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); // <dimen name="x_187.5">187.5px</dimen> // <dimen name="x_187.5">281.25px</dimen> // 起始位置:17结束位置:24取到的值名:187.5取到的值:187.5标准值:187.5转换后的值:281.25:281.25 lines = replace; } else { lines = line; } bufferedWriter.write(lines); bufferedWriter.newLine(); bufferedWriter.flush(); } bufferedReader.close(); bufferedWriter.close(); } }

代码下载

http://download.csdn.net/detail/prczhb/9922455

转载请注明原文地址: https://www.6miu.com/read-42610.html

最新回复(0)