标签:
看之前希望先看:
http://blog.csdn.net/zhaokaiqiang1992/article/details/45419023
此方案是看完上面的了博客总结出来的
方案1与方案2兼容~ 因为都是x? y? font?为名称
//values-xhdpi.. Dp的适配 与 values-xhdpi.. Sp的适配
这种适合 默认的那种 要是想非常精确 就考虑额外添加下面那个 的方案
按照以上计算方式,大致可以将市场上的手机划分为5个像素密度等级,具体如下:
(1) ldpi:120dpi,像素密度与dp转换关系为:1dp = 0.75px
(2) mdpi:160dpi ,像素密度与dp转换关系为:1dp = 1px
(3) hdpi:240dpi,像素密度与dp转换关系为:1dp = 1.5px
(4) xhdpi:320dpi,像素密度与dp转换关系为:1dp = 2px
(5) xxhdpi:480dpi,像素密度与dp转换关系为:1dp = 3px
基准: 不当成像素 当成多少份 已320*480 生成的文件夹values-ldpi为例
dw:320 dh:480
x1(代表 基准中的一份既 1/320) :
case ldpi:
resultDp=dx/0.75F;
break;
case mdpi:
resultDp=dx;
break;
case hdpi:
resultDp=dx/1.5F;
break;
case xhdpi:
resultDp=dx/2F;
break;
case xxhdpi:
resultDp= dx/3F;
<dimen name="font21px">21.0sp</dimen>//font
<dimen name="x2">2.0dp</dimen>//lay_x
<dimen name="y2">2.0dp</dimen>//lay_y
以此为例://values-1024x600… 的适配
因为这个是严格按照1024*600的 所以碰到差不多的机型但是不完全匹配的时候 还的依靠xdpi xxdpi mdpi那四个文件夹内的属性
基准: 不当成像素 当成多少份 已320*480 生成的文件夹values-1920x1080为例
x1(代表 基准中的一份既 1/320) :1080/320*1(以1080 分成320份在取一份)=3.375px;
y1同上1920/480=4px;
package 适配;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
//values-xhdpi.. Dp的适配
public class MakeXml_Dp {
private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\{0}\\";
private final static String WTemplate = "<dimen name=\"x{0}\">{1}dp</dimen>\n";
private final static String HTemplate = "<dimen name=\"y{0}\">{1}dp</dimen>\n";
private final static float dw = 320f;//w迭代的像素
private final static float dh = 480f;//h迭代的像素
public enum DPI{
ldpi("values-ldpi") ,mdpi("values-mdpi"),hdpi("values-hdpi"),xhdpi("values-xhdpi"),xxhdpi("values-xxhdpi");
public String fileName;
DPI(String fileName){
this.fileName=fileName;
};
public float dx2dp(int dx){
float resultDp=0F;
switch (this) {
case ldpi:
resultDp=dx/0.75F;
break;
case mdpi:
resultDp=dx;
break;
case hdpi:
resultDp=dx/1.5F;
break;
case xhdpi:
resultDp=dx/2F;
break;
case xxhdpi:
resultDp= dx/3F;
break;
}
return change(resultDp);
}
}
//保留两位小数
public static float change(float a) {
return Math.round( a * 100 ) / 100.0F;
}
public static void main(String[] args) {
System.out.println(change(0.33F));
for (DPI item : DPI.values())
makeFile(item);
}
public static void makeFile(DPI item) {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sb.append("<resources>");
for (int i = 1; i <=dw; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",item.dx2dp(i) + ""));
}
sb.append("</resources>");
StringBuffer sb2 = new StringBuffer();
sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sb2.append("<resources>");
for (int i = 1; i <= dh; i++) {
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", item.dx2dp(i) + ""));
}
sb2.append("</resources>");
String path = rootPath.replace("{0}", item.fileName );
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();
}
}
}
package 适配;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
//values-xhdpi.. Sp的适配
public class MakeXml_Sp {
private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\{0}\\";
private final static String WTemplate = "<dimen name=\"font{0}px\">{1}sp</dimen>\n";
//font px 迭代 from -to
private final static int from = 20;//w迭代的像素
private final static int to =50;//h迭代的像素
public enum DPI{
ldpi("values-ldpi") ,mdpi("values-mdpi"),hdpi("values-hdpi"),xhdpi("values-xhdpi"),xxhdpi("values-xxhdpi");
public String fileName;
DPI(String fileName){
this.fileName=fileName;
};
public float dx2sp(int dx){
float resultSp=0F;
switch (this) {
case ldpi:
resultSp=dx/0.75F;
break;
case mdpi:
resultSp=dx;
break;
case hdpi:
resultSp=dx/1.5F;
break;
case xhdpi:
resultSp=dx/2F;
break;
case xxhdpi:
resultSp= dx/3F;
break;
}
return change(resultSp);
}
}
//保留两位小数
public static float change(float a) {
return Math.round( a * 100 ) / 100.0F;
}
public static void main(String[] args) {
//生成文件
for (DPI item : DPI.values())
makeFile(item);
//单个分辨率的转换
int fontPx=10;//10px转换sp 各个分辨率的
for (DPI item : DPI.values()) {
System.out.println(item.fileName+"------>font"+fontPx+"px-->"+item.dx2sp(fontPx)+"sp");
}
}
public static void makeFile(DPI item) {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sb.append("<resources>");
for (int i = from; i <=to; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",item.dx2sp(i) + ""));
}
sb.append("</resources>");
String path = rootPath.replace("{0}", item.fileName );
File rootFile = new File(path);
if (!rootFile.exists()) {
rootFile.mkdirs();
}
File layxFile = new File(path + "font.xml");
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
pw.print(sb.toString());
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package 适配;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
//values-1024x600... 适配
public class MakeXml2 {
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) {
makeFile(320, 480);
makeFile(480,800);
makeFile(480, 854);
makeFile(540, 960);
makeFile(600, 1024);
makeFile(720, 1184);
makeFile(720, 1196);
makeFile(720, 1280);
makeFile(768, 1024);
makeFile(800, 1280);
makeFile(1080, 1812);
makeFile(1080, 1920);//x1(代表 基准中的一份既 1/320) :1080/320*1(以1080 分成320份在取一份)=3.375px;
makeFile(1440, 2560);
}
public static void makeFile(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 <= dw; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
change(cellw * i) + ""));
}
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 <=dh; i++) {
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
change(cellh * i) + ""));
}
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) {
return Math.round( a * 100 ) / 100.0F;
}
}
Android 屏幕适配方案(包含生成程序dp sp与解决方案)
标签:
原文地址:http://blog.csdn.net/qq_23231809/article/details/51329329