Android-------手机屏幕适配之文件适配

时间:2022-02-25 16:04:23

public class Main {
        //定义文件本地存储路径,可按照需求更改
        private final static String rootPath = "D:\\workfiles\\layoutroot\\values-{0}x{1}";
    
        /** 横轴方向分为320份 */
        private final static float dw = 320f;
        /** 纵轴方向分为480份 */
        private final static float dh = 480f;
    
        /** 宽度的模板 */
        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";
    
        public static void main(String[] args) {
            //就适配了三个分辨率的手机屏幕
            makeString(320, 480);
            makeString(720, 1280);
            makeString(1080, 1920);
        }
        
        /**
         * 生成对应分辨率的dimen文件
         * 
         * @param width
         *            手机屏幕x方向的像素数量
         * @param height
         *            手机屏幕y方向的像素数量
         */
        private static void makeString(int width, int height) {
            StringBuffer sbWidth = new StringBuffer();
            sbWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbWidth.append("<resources>");
            float cellw = width / dw;
            for (int i = 0; i < dw; i++) {
                sbWidth.append(WTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellw * i) + ""));
            }
            sbWidth.append(WTemplate.replace("{0}", "320").replace("{1}",
                    width + ""));
            sbWidth.append("</resources>");
    
            StringBuffer sbHeight = new StringBuffer();
            sbHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbHeight.append("<resources>");
            float cellh = height / dh;
            for (int i = 0; i < dh; i++) {
                sbHeight.append(HTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellh * i) + ""));
            }
            sbHeight.append(HTemplate.replace("{0}", "480").replace("{1}",
                    height + ""));
            sbHeight.append("</resources>");
    
            String path = rootPath.replace("{0}", height + "").replace("{1}",
                    width + "");
            File rootFile = new File(path);
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
    
            File layxFile = new File(path + "\\lay_x.xml");
            File layyFile = new File(path + "\\lay_y.xml");
    
            try {
                PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
                pw.print(sbWidth.toString());
                pw.close();
                pw = new PrintWriter(new FileOutputStream(layyFile));
                pw.print(sbHeight.toString());
                pw.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /**
         * 将浮点数小数点后保留两位小数
         */
        private static float change(float a) {
            int temp = (int) (a * 100);
            return temp / 100f;
        }

}

将生成的values文件复制到工程的res下,

使用用例:
android:layout_width="@dimen/x160"