安卓自动适配屏幕之尺寸适配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32320807/article/details/66971675

对于安卓这个杂乱的屏幕,N多的屏幕让我们眼花缭乱。面对美工对dp,sp的不理解,面对他们只给我们一套图片还是基于苹果的图片,让安卓程序员承受了成吨的伤害。下面我就介绍一个屏幕尺寸适配的方法来解决这个问题:

    首先考虑有一套图片是无法在所有安卓机器上正常显示的,这样我们就需要把图片设置成背景图片,而不是src。图片的大小由我们自行来指定,然后根据不同的屏幕选择不同的尺寸。

   图片的问题解决了我们就需要对文字,控件进行处理。把所有的大小都手动设置,然后根据屏幕不同屏幕选择不同的尺寸。

   这就是屏幕适配的思路,当然我们还会有一个疑问,那就是如何根据不同屏幕选择不同尺寸呢:

  1、拿着这个代码复制到自己的工程里面

  2、

 
       这是美工给你的分辨率的设计图尺寸,如果SUPPORT_DIMESION数组中没有添加,那么需要手动添加上

 3.运行程序,控制台会告诉你生成values文件的位置,然后复制到你的工程即可。


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;768,1280;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 = 1080;
		int baseH = 1920;
		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();
	}

}


生成文件之后,打开文件你会发现文件夹中有两个文件,lay_x,lay_y两个文件。打开你会发现这是一种以px为单位的方法设置的,按照屏幕比例选择不同的像素。

猜你喜欢

转载自blog.csdn.net/qq_32320807/article/details/66971675