• android屏幕适配,生成不同分辨率的dimen.xml文件


    一.在项目下新建moudle,选择Java Library

    二.DimenUtils类

    public class DimenUtils {
        //文件保存的路径  是在该项目下根路径下创建  比如该项目创建的路径是E:project3.0,
        // 则保存的文件路径是E:project3.0DimenDemoappsrcmain
    esvalues-1920x1080dimen.xml
        private final static String rootPath = "app/src/main/res/values-{0}x{1}";
    
    
        private final static float dw = 1080f;//默认布局的宽
        private final static float dh = 720f;//默认布局的高
    
        private final static String WTemplate = "<dimen name="x{0}">{1}px</dimen>
    ";
        private final static String HTemplate = "<dimen name="y{0}">{1}px</dimen>
    ";
        public static void main(String[] args) {
            makeString(1080, 720);
            makeString(1920, 1080);
            makeString(1366, 768);
        }
    
        //获取dimen.xml的文本内容
        public static void makeString(int w,int h){
            System.out.println("1111111111");
            StringBuffer sb=new StringBuffer();
            sb.append("<?xml version="1.0" encoding="utf-8"?>
    ");
            sb.append("<resources>");
    
            //遍历获取一系列宽的值
            float cellw =w / dw;//宽的比例
            for (int i = 0; i <dw ; i++) {
                sb.append(WTemplate.replace("{0}",i + "").replace("{1}",
                        change(cellw * i) + ""));
            }
    
            sb.append(WTemplate.replace("{0}",dw+"").replace("{1}", w + ""));
    
            //遍历获取一系列高的值
            float cellh=h/dh;//高的比例
            for (int i = 0; i <dh ; i++) {
                sb.append(HTemplate.replace("{0}",i + "").replace("{1}",
                        change(cellh * i) + ""));
            }
    
            sb.append(HTemplate.replace("{0}",dh+"").replace("{1}", h+ ""));
            sb.append("</resources>");
    
            makeFile(w,h,sb.toString());
        }
    
        //创建文件并写入内容
        private static void makeFile(int w,int h,String text){
            System.out.println("22222222222222");
            String path = rootPath.replace("{0}",w+ "").replace("{1}",h+ "");
            File rootFile = new File(path);
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
            File file=new File(path,"dimen.xml");
            System.out.println("333333333333333:"+file.getAbsolutePath());
            try {
                PrintWriter pw=new PrintWriter(new FileOutputStream(file));
                pw.println(text);
                pw.close();
                System.out.println("4444444444444444");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("5555555555555555");
            }
        }
    
    
    
        public static float change(float a) {
            int temp = (int) (a * 100);
            return temp / 100f;
        }
    }

    三.运行生成文件

  • 相关阅读:
    11.菜单(一)
    线性表之顺序存储详解
    SVN 撤回已提交的代码
    线性表1
    顶层父类
    异常类之派生类
    new和delete重载
    异常类之基类
    Qt中多线程问题
    智能指针实例
  • 原文地址:https://www.cnblogs.com/wangjiaghe/p/8601756.html
Copyright © 2020-2023  润新知