Android自适应不同分辨率的屏幕

来源:互联网 发布:大龄软件工程硕士 编辑:程序博客网 时间:2024/05/11 20:04

其实现主要是参考这篇博文:Android 屏幕适配方案。



由于在项目需要对应用进行不同分辨率的机器的适配,所以采用了以上的博文的方法,下面主要是写实现的过程。

其实现原理其实是基于百分比,和android中的weight属性一样的原理。但是weight属性只能使用到LinearLayout的布局中,局限性很大。而这里的实现是把屏幕的分辨率分成若干份,你希望控件的长度占屏幕的百分之多少,就填写对应值的Id。然而,那个对应的Id的值在不同的分辨率下是对应不同的值,但占屏幕的百分比是一样的,这样就可以做到了再不同分辨率下的自适应。


1、生成不同分辨率下的对应的值。下面是对应的java代码:

//package com.zhy.blogcodes.genvalues;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 = "679,480;722,480;800,480;854,480;1500,480;1024,600";//modify by Garmentprivate 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}", w + "")//modify by Garment,switch the place of w and h 2016/11/24.replace("{1}", h + ""));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 = 800;     //modify by Garment,chang 320 to 800int baseH = 480;//modify by Garment,change 400 to 480String 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)生成的文件夹的名称,生成的文件夹的名称必须是分辨率的大值的放在前面(android规定)。由于我的项目中用到的机器宽度比高度是大的,所以宽度放在前面。

3)基础分辨率,就是说以哪个分辨率作为模板分辨率,我们先按这个模板的分辨率进行适配,然后其他分辨率也就自动适配了。


运行javac xxx.java,然后再运行java xxx;就可以得到对应的不同分辨率的文件,把它拷贝到项目的resource文件夹目录下。


2、在布局文件中使用该分辨率值。

比如我现在的基础分辨率是800*480(w*h),我希望我的图片大小占屏幕宽度的四分之一,占屏幕高度的二分之一,则如下:

    <ImageView      android:id="@+id/personal_message_detail_img"      android:layout_width="@dimen/x200"      android:layout_height="@dimen/y240" />



3、文字的自适应

我们可以在不同的的分辨率下定义不同的大小。

    <dimen name="login_text_title_mobile_font">24sp</dimen>    <dimen name="login_edit_mobile_font">24sp</dimen>    <dimen name="login_edit_vericode_font">24sp</dimen>

最后,这个方案也是可以适用不同的DPI的机器。因为该方案使用的分辨率的值是px,而且该值已经是根据机器的分辨率进行百分比的换算。


0 0