Android屏幕适配V1.0 ---来自火星的Gank

说道Android屏幕适配问题,首先说下Android机型过多,各种分辨率的手机层出不穷,本文已1280_720 基准来适配机型;

注意:此方法由于适配的分辨率较多,所以生成的文件较多,造成文件体积较大。

单用正数约4M,正负全用约8M,需考虑得失或对部分分辨率的适配进行相应删减;

一:在文件路经打开 dom 窗口 (windom +R :cmd 或 shaift+鼠标右键)

下文代码的主函数中填写基准宽高后再进行以下操作

步骤:

1.javac xxx.java 生成 .class文件;

2.java xxx;

就可在当前文件夹生成res目录,将其合并到项目的res目录中

java文件:正数

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
  * Created by
zll .
  */
public class GenerateValueFiles {
    private int baseW;
    private int baseH;
    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";
    /**
      * {0}-HEIGHT
      */
    private final static String VALUE_TEMPLATE = "values-{0}x{1}";
    private static final String SUPPORT_DIMESION = "240,320;240,400;320,400;240,432;320,480;440,468;520,576;480,640;480,768;480,800;600,800;480,854;540,960;640,960;576,1024;600,1024;768,1024;864,1152;720,1184;720,1196;720,1280;768,1280;800,1280;854,1280;960,1280;1024,1280;768,1366;1050,1400;900,1440;900,1600;1024,1600;1200,1600;1050,1680;1080,1766;1080,1800;1080,1812;1200,1824;1080,1830;1080,1920;1152,1920;1200,1920;1400,1920;1152,2048;1536,2048;1400,2560;1440,2392;1536,2560;1600,2560;2048,2560;2100,2800;2048,3200;2400,3200;2160,3840;2400,3840;2304,4096;3072,4096;3200,5120;4096,5120;4096,6400;4800,6400;4320,7680;4800,7680;";
    private String supportStr = SUPPORT_DIMESION;
    public GenerateValueFiles(int baseX, int baseY, String supportStr) {
        this.baseW = baseX;
        this.baseH = baseY;
        if (!this.supportStr.contains(baseX + "," + baseY)) {
            this.supportStr += baseX + "," + baseY + ";";
        }
        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]));
        }
    }
    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(WTemplate.replace("{0}", baseW + "").replace("{1}",
                w + ""));
        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) + ""));
        }
        sbForHeight.append(HTemplate.replace("{0}", baseH + "").replace("{1}",
                h + ""));
        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();
        }
    }
    public static float change(float a) {
        int temp = (int) (a * 100);
        return temp / 100f;
    }
    public static void main(String[] args) {
        int baseW = 720;
        int baseH = 1280;
        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(baseW, baseH, addition).generate();
    }
}


java文件负数

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintWriter;
  5. /**
  6. * Created by zll.
  7. */
  8. publicclassGenerateValueFiles{
  9. privateint baseW;
  10. privateint baseH;
  11. privateString dirStr="./res";
  12. privatefinalstaticStringWTemplate="<dimen name=\"minus_x{0}\">-{1}px</dimen>\n";
  13. privatefinalstaticStringHTemplate="<dimen name=\"minus_y{0}\">-{1}px</dimen>\n";
  14. /**
  15. * {0}-HEIGHT
  16. */
  17. privatefinalstaticString VALUE_TEMPLATE="values-{0}x{1}";
  18. privatestaticfinalString SUPPORT_DIMESION="240,320;240,400;320,400;240,432;320,480;440,468;520,576;480,640;480,768;480,800;600,800;480,854;540,960;640,960;576,1024;600,1024;768,1024;864,1152;720,1184;720,1196;720,1280;768,1280;800,1280;854,1280;960,1280;1024,1280;768,1366;1050,1400;900,1440;900,1600;1024,1600;1200,1600;1050,1680;1080,1766;1080,1800;1080,1812;1200,1824;1080,1830;1080,1920;1152,1920;1200,1920;1400,1920;1152,2048;1536,2048;1400,2560;1536,2560;1600,2560;2048,2560;2100,2800;2048,3200;2400,3200;2160,3840;2400,3840;2304,4096;3072,4096;3200,5120;4096,5120;4096,6400;4800,6400;4320,7680;4800,7680;";
  19. privateString supportStr= SUPPORT_DIMESION;
  20. publicGenerateValueFiles(int baseX,int baseY,String supportStr){
  21. this.baseW= baseX;
  22. this.baseH= baseY;
  23. if(!this.supportStr.contains(baseX+","+ baseY)){
  24. this.supportStr+= baseX+","+ baseY+";";
  25. }
  26. this.supportStr+= validateInput(supportStr);
  27. System.out.println(supportStr);
  28. File dir =newFile(dirStr);
  29. if(!dir.exists()){
  30. dir.mkdir();
  31. }
  32. System.out.println(dir.getAbsoluteFile());
  33. }
  34. /**
  35. * @param supportStr
  36. * w,h_...w,h;
  37. * @return
  38. */
  39. privateString validateInput(String supportStr){
  40. StringBuffer sb =newStringBuffer();
  41. String[] vals = supportStr.split("_");
  42. int w =-1;
  43. int h =-1;
  44. String[] wh;
  45. for(String val: vals){
  46. try{
  47. if(val==null|| val.trim().length()==0)
  48. continue;
  49. wh= val.split(",");
  50. w=Integer.parseInt(wh[0]);
  51. h=Integer.parseInt(wh[1]);
  52. }catch(Exception e){
  53. System.out.println("skip invalidate params : w,h = "+ val);
  54. continue;
  55. }
  56. sb.append(w+","+ h+";");
  57. }
  58. return sb.toString();
  59. }
  60. publicvoid generate(){
  61. String[] vals = supportStr.split(";");
  62. for(String val: vals){
  63. String[] wh = val.split(",");
  64. generateXmlFile(Integer.parseInt(wh[0]),Integer.parseInt(wh[1]));
  65. }
  66. }
  67. privatevoid generateXmlFile(int w,int h){
  68. StringBuffer sbForWidth =newStringBuffer();
  69. sbForWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  70. sbForWidth.append("<resources>");
  71. float cellw = w*1.0f/ baseW;
  72. System.out.println("width : " + w+","+ baseW+","+ cellw);
  73. for(int i=1; i< baseW; i++){
  74. sbForWidth.append(WTemplate.replace("{0}", i +"").replace("{1}",
  75. change(cellw* i)+""));
  76. }
  77. sbForWidth.append(WTemplate.replace("{0}", baseW +"").replace("{1}",
  78. w+""));
  79. sbForWidth.append("</resources>");
  80. StringBuffer sbForHeight =newStringBuffer();
  81. sbForHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  82. sbForHeight.append("<resources>");
  83. float cellh = h*1.0f/ baseH;
  84. System.out.println("height : "+ h+","+ baseH+","+ cellh);
  85. for(int i=1; i< baseH; i++){
  86. sbForHeight.append(HTemplate.replace("{0}", i +"").replace("{1}",
  87. change(cellh* i)+""));
  88. }
  89. sbForHeight.append(HTemplate.replace("{0}", baseH +"").replace("{1}",
  90. h+""));
  91. sbForHeight.append("</resources>");
  92. File fileDir =newFile(dirStr+File.separator
  93. + VALUE_TEMPLATE.replace("{0}", h +"")//
  94. .replace("{1}", w +""));
  95. fileDir.mkdir();
  96. File layxFile =newFile(fileDir.getAbsolutePath(),"lay_minus_x.xml");
  97. File layyFile =newFile(fileDir.getAbsolutePath(),"lay_minus_y.xml");
  98. try{
  99. PrintWriter pw =newPrintWriter(newFileOutputStream(layxFile));
  100. pw.print(sbForWidth.toString());
  101. pw.close();
  102. pw=newPrintWriter(newFileOutputStream(layyFile));
  103. pw.print(sbForHeight.toString());
  104. pw.close();
  105. }catch(FileNotFoundException e){
  106. e.printStackTrace();
  107. }
  108. }
  109. publicstaticfloat change(float a){
  110. int temp =(int)(a*100);
  111. return temp /100f;
  112. }
  113. publicstaticvoid main(String[] args){
  114. int baseW =720;
  115. int baseH =1280;
  116. String addition ="";
  117. try{
  118. if(args.length>=3){
  119. baseW=Integer.parseInt(args[0]);
  120. baseH=Integer.parseInt(args[1]);
  121. addition= args[2];
  122. }elseif(args.length>=2){
  123. baseW=Integer.parseInt(args[0]);
  124. baseH=Integer.parseInt(args[1]);
  125. }elseif(args.length>=1){
  126. addition= args[0];
  127. }
  128. }catch(NumberFormatException e){
  129. System.err
  130. .println("right input params : java -jar xxx.jar width height w,h_w,h_..._w,h;");
  131. e.printStackTrace();
  132. System.exit(-1);
  133. }
  134. newGenerateValueFiles(baseW, baseH, addition).generate();
  135. }
  136. }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值