Android 屏幕适配方案

来源:互联网 发布:石原里美穿搭 知乎 编辑:程序博客网 时间:2024/05/02 12:11
http://blog.csdn.net/lmj623565791/article/details/45460089; 

本文出自:【张鸿洋的博客】


百分比的引入

1、引入

其实我们的解决方案,就是在项目中针对你所需要适配的手机屏幕的分辨率各自简历一个文件夹。

如下图:

然后我们根据一个基准,为基准的意思就是:

比如480*320的分辨率为基准

  • 宽度为320,将任何分辨率的宽度分为320份,取值为x1-x320
  • 高度为480,将任何分辨率的高度分为480份,取值为y1-y480

例如对于800*480的宽度480:

可以看到x1 = 480 / 基准 = 480 / 320 = 1.5 ;

其他分辨率类似~~ 
你可能会问,这么多文件,难道我们要手算,然后自己编写?不要怕,下文会说。

那么,你可能有个疑问,这么写有什么好处呢?

假设我现在需要在屏幕中心有个按钮,宽度和高度为我们屏幕宽度的1/2,我可以怎么编写布局文件呢?

<FrameLayout >    <Button        android:layout_gravity="center"        android:gravity="center"        android:text="@string/hello_world"        android:layout_width="@dimen/x160"        android:layout_height="@dimen/x160"/></FrameLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

可以看到我们的宽度和高度定义为x160,其实就是宽度的50%; 
那么效果图:

可以看到不论在什么分辨率的机型,我们的按钮的宽和高始终是屏幕宽度的一半。

  • 对于设计图

假设现在的UI的设计图是按照480*320设计的,且上面的宽和高的标识都是px的值,你可以直接将px转化为x[1-320],y[1-480],这样写出的布局基本就可以全分辨率适配了。

2、自动生成工具

好了,其实这样的文件夹手写也可以,按照你们需要支持的分辨率,然后编写一套,以后一直使用。

当然了,作为程序员的我们,怎么能做这么low的工作,肯定要程序来实现:

那么实现需要以下步骤:

  1. 分析需要的支持的分辨率

对于主流的分辨率我已经集成到了我们的程序中,当然对于特殊的,你可以通过参数指定。关于屏幕分辨率信息,可以通过该网站查询:http://screensiz.es/phone

  1. 编写自动生成文件的程序

代码如下

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;/** * Created by zhy on 15/5/3. */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 = "320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;800,1280;1080,1812;1080,1920;1440,2560;";    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 = 320;        int baseH = 400;        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();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166

同时我提供了jar包,默认情况下,双击即可生成,使用说明:

下载地址见文末,内置了常用的分辨率,默认基准为480*320,当然对于特殊需求,通过命令行指定即可:

例如:基准 1280 * 800 ,额外支持尺寸:1152 * 735;4500 * 3200;

按照

java -jar xx.jar width height width,height_width,height 

上述格式即可。

到此,我们通过编写一个工具,根据某基准尺寸,生成所有需要适配分辨率的values文件,做到了编写布局文件时,可以参考屏幕的分辨率;在UI给出的设计图,可以快速的按照其标识的px单位进行编写布局。基本解决了适配的问题。



原创粉丝点击