关于屏幕适配的一些看法
2016-03-24 15:34
190 查看
因为分辨率不一样,所以不能用px;因为屏幕宽度不一样,所以要小心的用dp,那么我们可不可以用另外一种方法来统一单位,不管分辨率是多大,屏幕宽度用一个固定的值的单位来统计呢?
答案是:当然可以。
我们假设手机屏幕的宽度都是320某单位,那么我们将一个屏幕宽度的总像素数平均分成320份,每一份对应具体的像素就可以了
代码应该很好懂,我们将一个屏幕宽度分为320份,高度480份,然后按照实际像素对每一个单位进行复制,放在对应values-widthxheight文件夹下面的lax.xml和lay.xml里面,这样就可以统一所有你想要的分辨率的单位了,下面是生成的一个320*480分辨率的文件,因为宽高分割之后总分数和像素数相同,所以x1就是1px,以此类推。
具体详情:http://www.cocoachina.com/android/20151030/13971.html
答案是:当然可以。
我们假设手机屏幕的宽度都是320某单位,那么我们将一个屏幕宽度的总像素数平均分成320份,每一份对应具体的像素就可以了
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; public class MakeXml { private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\"; private final static float dw = 320f; private final static float dh = 480f; 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"; public static void main(String[] args) { makeString(320, 480); makeString(480,800); makeString(480, 854); makeString(540, 960); makeString(600, 1024); makeString(720, 1184); makeString(720, 1196); makeString(720, 1280); makeString(768, 1024); makeString(800, 1280); makeString(1080, 1812); makeString(1080, 1920); makeString(1440, 2560); } public static void makeString(int w, int h) { StringBuffer sb = new StringBuffer(); sb.append("[?xml version=\"1.0\" encoding=\"utf-8\"?]\n"); sb.append("[resources]"); float cellw = w / dw; for (int i = 1; i < 320; i++) { sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + "")); } sb.append(WTemplate.replace("{0}", "320").replace("{1}", w + "")); sb.append("[/resources]"); StringBuffer sb2 = new StringBuffer(); sb2.append("[?xml version=\"1.0\" encoding=\"utf-8\"?]\n"); sb2.append("[resources]"); float cellh = h / dh; for (int i = 1; i < 480; i++) { sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + "")); } sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + "")); sb2.append("[/resources]"); String path = rootPath.replace("{0}", h + "").replace("{1}", w + ""); File rootFile = new File(path); if (!rootFile.exists()) { rootFile.mkdirs(); } File layxFile = new File(path + "lay_x.xml"); File layyFile = new File(path + "lay_y.xml"); try { PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile)); pw.print(sb.toString()); pw.close(); pw = new PrintWriter(new FileOutputStream(layyFile)); pw.print(sb2.toString()); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static float change(float a) { int temp = (int) (a * 100); return temp / 100f; } }
代码应该很好懂,我们将一个屏幕宽度分为320份,高度480份,然后按照实际像素对每一个单位进行复制,放在对应values-widthxheight文件夹下面的lax.xml和lay.xml里面,这样就可以统一所有你想要的分辨率的单位了,下面是生成的一个320*480分辨率的文件,因为宽高分割之后总分数和像素数相同,所以x1就是1px,以此类推。
具体详情:http://www.cocoachina.com/android/20151030/13971.html
相关文章推荐
- 谈谈java中的WeakReference
- 安卓开发如何提高Android程序的效率(转)
- 关于AccessibilityService的用法(转)
- Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?(转)
- 关于java.lang.UnsatisfiedLinkError的处理
- 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
- 浏览器客户端的数据存储
- unity2d之2d帧动画创建
- GeoServer安装和部署shp文件
- 监听键盘是否在界面上
- 享元模式(Flyweight)
- 一些简单常见的html标签
- jqgrid项目使用记录/注释版/未整理/
- WinXP下如何安装及御载MySQL服务
- .NET微信公众号开发之创建自定义菜单
- 理解Mysql语句:insert 表名 select null 及 select last_insert_id()
- 热电效应
- 【matlab】:matlab的linspace函数解析
- sql 聚合函数
- unbantu安装wmvare