Android -屏幕适配全攻略
CSDN 赵凯强:http://blog.csdn.net/zhaokaiqiang1992/article/details/45419023
慕课网视频:http://www.imooc.com/learn/484
一、基础知识
1、屏幕尺寸:屏幕对角线长度,例如:4.95inch
2、分辨率:1920X1085
3、DPI:屏幕像素密度:445,计算过程:(1920*1920+1085*1085 再开方)/ 4.95 = 445 即每英寸像素点数量
4、density = 当前机子DPI/160 , 160DPI作为参考标准,160DPI下,1dp=1px 以下举例说明:
480*320,160DPI :density=1,1dp=1px, px_max=320px,dp_max=320dp
800*480,240DPI :density=240/160=1.5,1dp=1.5px, px_max=480px,dp_max=320dp
5、sp,根据字体首选项进行缩放,建议12,14,18,22
二、屏幕适配解决方案
1、支持各种屏幕尺寸
1) 使用wrap_content,match_parent,weight
2) 使用相对布局
3) 使用限定符,layout-sw600dp(small width 最小宽度)
V3.2前平板 layout-large;V3.2后平板 layout-sw600dp;
4) 点九图详细介绍
2、支持各种屏幕密度
1) 使用非密度制约单位 dp,sp
2) 使用多套对应密度位图
3) 使用dimen ,我们将一个屏幕宽度分为320份,高度480份,然后按照实际像素对每一个单位进行复制,放在对应values-widthxheight文件夹下面的lax.xml和lay.xml里面
1080*1960分辨率下是什么样子呢?我们可以看下,由于1080和320是3.37倍的关系,所以x1=3.37px
<?xml version="1.0" encoding="utf-8"?> <resources><dimen name="x1">3.37px</dimen> <dimen name="x2">6.75px</dimen> <dimen name="x3">10.12px</dimen> <dimen name="x4">13.5px</dimen> <dimen name="x5">16.87px</dimen> <dimen name="x6">20.25px</dimen> <dimen name="x7">23.62px</dimen> <dimen name="x8">27.0px</dimen> <dimen name="x9">30.37px</dimen> <dimen name="x10">33.75px</dimen> ...省略好多行 <dimen name="x300">1012.5px</dimen> <dimen name="x301">1015.87px</dimen> <dimen name="x302">1019.25px</dimen> <dimen name="x303">1022.62px</dimen> <dimen name="x304">1026.0px</dimen> <dimen name="x305">1029.37px</dimen> <dimen name="x306">1032.75px</dimen> <dimen name="x307">1036.12px</dimen> <dimen name="x308">1039.5px</dimen> <dimen name="x309">1042.87px</dimen> <dimen name="x310">1046.25px</dimen> <dimen name="x311">1049.62px</dimen> <dimen name="x312">1053.0px</dimen> <dimen name="x313">1056.37px</dimen> <dimen name="x314">1059.75px</dimen> <dimen name="x315">1063.12px</dimen> <dimen name="x316">1066.5px</dimen> <dimen name="x317">1069.87px</dimen> <dimen name="x318">1073.25px</dimen> <dimen name="x319">1076.62px</dimen> <dimen name="x320">1080px</dimen> </resources>
实现JAVA代码:
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;
}
}
3、实现自适应用户界面流程(平板)
浙公网安备 33010602011771号