用dimens去实现 屏幕适配
来源:互联网 发布:cnstorm和淘宝比哪个好 编辑:程序博客网 时间:2024/04/28 13:32
屏幕适配很简单
没想到到现在了还有人在各种问屏幕适配的问题。我真是服了。
15年的时候国内就有很多大神分享了各种屏幕适配的方法。
最近突然被十几个人问。我依然为了应付回答来写博客了。哈哈
我们公司就是dimens方法。最low、最简单、最实用。
下面是代码直上代码。这个代码运行一下就会出现一堆res文件。然后复制到 工程里面就好了
package com.example;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;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 = 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(); }}
运行完之后出现res文件夹,他下面会出现一堆values文件。直接复制到as里面这样的
这个方法立竿见影。直接用就好了。不懂得看看代码也能明白
尊重原创:http://write.blog.csdn.net/mdeditor#!postId=51131403
2 0
- 用dimens去实现 屏幕适配
- 用dimens去实现 屏幕适配
- Android屏幕适配---dimens转换工具
- android屏幕适配之dimens适配
- 安卓用dimens做屏幕适配
- Android 屏幕适配之dimens适配
- Android 开发之屏幕适配 dimens
- Android 屏幕适配之 dimens 适配
- 使用dimens来进行屏幕适配
- Android 屏幕适配之dimens适配
- 自动生成Android屏幕适配的dimens.xml
- 自动生成android dimens.xml适配屏幕
- 自动生成Android屏幕适配的dimens.xml
- android之屏幕适配(三)实践dimens.xml尺寸适配不同的平板
- android之屏幕适配,实践dimens.xml尺寸适配不同的平板
- 自动生成Android屏幕适配的dimens.xml文件工具类
- android屏幕适配,自动生成不同的dimens.xml详解
- Android快速屏幕尺寸适配 快速生成 res/dimens.xml
- android 下载安装并打开apk
- openwrt之WIFI驱动结构的一点简单分析
- 范数与距离的关系
- Frscon的使用
- nginx安装lua模块
- 用dimens去实现 屏幕适配
- jQuery 中 attr() 和 prop() 方法的区别
- SynchronizedMap和ConcurrentHashMap的深入分析
- JDBC分层开发
- USB全速和低速设备的检测
- 查看ADB被占用的情况
- IT杂谈之一:野蛮生长到精细分工
- Hibernate - Batch update returned unexpected row count from update: 0 actual row count: 0 expected:
- POJ#1002_487-3279