美文网首页
android屏幕适配方案

android屏幕适配方案

作者: CarlWu_S | 来源:发表于2017-09-04 16:45 被阅读0次

    根据UI提供标注图的标注尺寸,设置baseW(基准宽度)和baseH(基准高度)

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    
    
    public class GenerateValueFiles {
    
        private static int baseW = 360;//基准宽度
        private static int baseH = 800;//基准高度
    
        private String dirStr = "./res";
    
        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";
    
        private final static String VALUE_TEMPLATE = "values-{0}x{1}";
    
        private static final String SUPPORT_DIMESION = "320,480;"
                + "480,800;480,854;"
                + "540,854;540,960;"
                + "600,800;600,1024;"
                + "640,960;"
                + "720,1184;720,1196;720,1208;720,1280;"
                + "768,1024;768,1280;"
                + "800,1280;"
                + "1080,1700;1080,1776;1080,1794;1080,1800;1080,1812;1080,1920;"
                + "1440,2416;1440,2560;"
                + "1600,2560;";
    
        private String supportStr = SUPPORT_DIMESION;
    
        public GenerateValueFiles(String supportStr) {
            this.supportStr += validateInput(supportStr);
    
            System.out.println(supportStr);
    
            File dir = new File(dirStr);
            if (!dir.exists()) {
                dir.mkdir();
            }
            System.out.println(dir.getAbsoluteFile());
        }
    
        /**
         * @param supportStr
         *            w,h_...w,h;
         * @return
         */
        private String validateInput(String supportStr) {
            StringBuffer sb = new StringBuffer();
            String[] vals = supportStr.split("_");
            int w = -1;
            int h = -1;
            String[] wh;
            for (String val : vals) {
                try {
                    if (val == null || val.trim().length() == 0)
                        continue;
    
                    wh = val.split(",");
                    w = Integer.parseInt(wh[0]);
                    h = Integer.parseInt(wh[1]);
                } catch (Exception e) {
                    System.out.println("skip invalidate params : w,h = " + val);
                    continue;
                }
                sb.append(w + "," + h + ";");
            }
    
            return sb.toString();
        }
    
        public void generate() {
            String[] vals = supportStr.split(";");
            for (String val : vals) {
                String[] wh = val.split(",");
                generateXmlFile(Integer.parseInt(wh[0]), Integer.parseInt(wh[1]));
            }
            generateDefaultXmlFile(baseW,baseH);
        }
    
        private void generateXmlFile(int w, int h) {
    
            StringBuffer sbForWidth = new StringBuffer();
            sbForWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbForWidth.append("<resources>");
            float cellw = w * 1.0f / baseW;
    
            System.out.println("width : " + w + "," + baseW + "," + cellw);
            for (int i = 1; i <=baseW; i++) {
                sbForWidth.append(WTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellw * i) + ""));
            }
            sbForWidth.append("</resources>");
    
            StringBuffer sbForHeight = new StringBuffer();
            sbForHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbForHeight.append("<resources>");
            float cellh = w *1.0f/ baseW;
            System.out.println("height : "+ h + "," + baseH + "," + cellh);
            for (int i = 1; i <= baseH; i++) {
                sbForHeight.append(HTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellh * i) + ""));
            }
            sbForHeight.append("</resources>");
    
            File fileDir = new File(dirStr + File.separator
                    + VALUE_TEMPLATE.replace("{0}", h + "")//
                            .replace("{1}", w + ""));
            fileDir.mkdir();
    
            File layxFile = new File(fileDir.getAbsolutePath(), "lay_x.xml");
            File layyFile = new File(fileDir.getAbsolutePath(), "lay_y.xml");
            try {
                PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
                pw.print(sbForWidth.toString());
                pw.close();
                pw = new PrintWriter(new FileOutputStream(layyFile));
                pw.print(sbForHeight.toString());
                pw.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        private void generateDefaultXmlFile(int w, int h){
            
             StringBuffer sbForWidth = new StringBuffer();
             sbForWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
             sbForWidth.append("<resources>");
             float cellw = w * 1.0f / baseW;
    
             System.out.println("width : " + w + "," + baseW + "," + cellw);
             for (int i = 1; i < baseW; i++) {
                 sbForWidth.append(WTemplate.replace("{0}", i + "").replace("{1}",
                         change(cellw * i) + "").replace("px", "dp"));
             }
             sbForWidth.append(WTemplate.replace("{0}", baseW + "").replace("{1}",
                     w + "").replace("px", "dp"));
             sbForWidth.append("</resources>");
    
             StringBuffer sbForHeight = new StringBuffer();
             sbForHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
             sbForHeight.append("<resources>");
             float cellh = h *1.0f/ baseH;
             System.out.println("height : "+ h + "," + baseH + "," + cellh);
             for (int i = 1; i < baseH; i++) {
                 sbForHeight.append(HTemplate.replace("{0}", i + "").replace("{1}",
                         change(cellh * i) + "").replace("px", "dp"));
             }
             sbForHeight.append(HTemplate.replace("{0}", baseH + "").replace("{1}",
                     h + "").replace("px", "dp"));
             sbForHeight.append("</resources>");
    
             File fileDir = new File(dirStr + File.separator
                     + VALUE_TEMPLATE.substring(0, 6));
             fileDir.mkdir();
    
             File layxFile = new File(fileDir.getAbsolutePath(), "lay_x.xml");
             File layyFile = new File(fileDir.getAbsolutePath(), "lay_y.xml");
             try {
                 PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
                 pw.print(sbForWidth.toString());
                 pw.close();
                 pw = new PrintWriter(new FileOutputStream(layyFile));
                 pw.print(sbForHeight.toString());
                 pw.close();
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             }
        }
        public static float change(float a) {
            int temp = (int) (a * 100);
            return temp / 100f;
        }
    
        public static void main(String[] args) {
            String addition = "";
            try {
                if (args.length >= 3) {
                    baseW = Integer.parseInt(args[0]);
                    baseH = Integer.parseInt(args[1]);
                    addition = args[2];
                } else if (args.length >= 2) {
                    baseW = Integer.parseInt(args[0]);
                    baseH = Integer.parseInt(args[1]);
                } else if (args.length >= 1) {
                    addition = args[0];
                }
            } catch (NumberFormatException e) {
    
                System.err
                        .println("right input params : java -jar xxx.jar width height w,h_w,h_..._w,h;");
                e.printStackTrace();
                System.exit(-1);
            }
    
            new GenerateValueFiles(addition).generate();
        }
    
    }
    

    运行以上代码,会得到 res/values-x-x 文件,直接复制到项目的res路径下即可。

    如何使用?

    xml中指定width和height时,直接使用UI标注图中的尺寸,如UI标注128(不区分是宽度或高度,它们等效的),xml中就可以使用lay_x128。
    lay_y和lay_x是一样的内容,不需要的可以删除所有的lay_x文件,唯一区别是lay_y的范围可以自由设置(比如800),在UI中如果有一个控件的UI标注尺寸为400,在lay_x中并没有lay_x400,这时你就可以使用lay_y400了。

    有虚拟导航条的手机屏幕适配问题?

    比如一款手机屏幕的物理尺寸为1920*1080,如果没有虚拟导航条它适配了values-1920x1080,但是有虚拟导航条时,它所适配的values文件就不是values-1920x1080了,而是高度减去虚拟导航条的values文件,比如虚拟导航条的高度为108,这时它适配的就是values-1812x1080。需要注意的是,虚拟导航条可以手动设置为隐藏,当隐藏状态下时,它适配的就是values-1920x1080了。
    那么我们怎么知道虚拟导航条的高度?
    可以写一个测试demo运行在这款手机上,执行如下代码:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.Display;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            DisplayMetrics dm = new DisplayMetrics();
            Display display = getWindowManager().getDefaultDisplay();
            display.getMetrics(dm);
            TextView mTextView = (TextView) findViewById(R.id.main_tv);
            mTextView.setText("width:"+dm.widthPixels+",height:"+dm.heightPixels);
        }
    }
    

    运行该app就可以在textview中显示当前状态下适配屏幕所需要的宽和高了。

    如果SUPPORT_DIMESION 中没有你想要适配屏幕的尺寸,你可以自行添加进去(格式为 宽,高)。

    相关文章

      网友评论

          本文标题:android屏幕适配方案

          本文链接:https://www.haomeiwen.com/subject/popejxtx.html