Android px适配生成Px文件
来源:互联网 发布:demo软件 编辑:程序博客网 时间:2024/04/30 14:11
package com.kede.yanjing.elibrary.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
* 屏幕适配(编写自动生成文件的程序)
* 博文地址 http://blog.csdn.net/lmj623565791/article/details/45460089
*/
public class GenerateValueFiles {
private int baseW;
private int baseH;
private String dirStr = "E:\\WorkSpace\\Eshop\\elibrary\\src\\main\\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 = "1080,1776;1851,1080;320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;768,1280;1200,1920;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) {
/*
* 常见android手机分辨率(4:3 5:3 16:9 *VGA *HD 720p 1080p )
*
* 4:3
* VGA 640*480 (VideoGraphics Array)
* QVGA 320*240 (Quarter VGA)
* HVGA 480*320 (Half-size VGA)
* SVGA 800*600 (Super VGA)
*
* 5:3
* WVGA 800*480 (Wide VGA)
*
* 16:9
* FWVGA 854*480 (Full Wide VGA)
* HD 1920*1080 High Definition
* QHD 960*540
* 720p 1280*720 标清
* 1080p 1920*1080 高清
*
* 手机:
* iphone 4/4s 960*640 (3:2)
* iphone5 1136*640
* 小米1 854*480(FWVGA)
* 小米2 1280*720
*
* 分辨率对应DPI
* "HVGA mdpi"
* "WVGA hdpi "
* "FWVGA hdpi "
* "QHD hdpi "
* "720P xhdpi"
* "1080P xxhdpi "
*/
//以 720p作为标准 1920*1080 高清,填写ui标注的宽高
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();
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
* 屏幕适配(编写自动生成文件的程序)
* 博文地址 http://blog.csdn.net/lmj623565791/article/details/45460089
*/
public class GenerateValueFiles {
private int baseW;
private int baseH;
private String dirStr = "E:\\WorkSpace\\Eshop\\elibrary\\src\\main\\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 = "1080,1776;1851,1080;320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;768,1280;1200,1920;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) {
/*
* 常见android手机分辨率(4:3 5:3 16:9 *VGA *HD 720p 1080p )
*
* 4:3
* VGA 640*480 (VideoGraphics Array)
* QVGA 320*240 (Quarter VGA)
* HVGA 480*320 (Half-size VGA)
* SVGA 800*600 (Super VGA)
*
* 5:3
* WVGA 800*480 (Wide VGA)
*
* 16:9
* FWVGA 854*480 (Full Wide VGA)
* HD 1920*1080 High Definition
* QHD 960*540
* 720p 1280*720 标清
* 1080p 1920*1080 高清
*
* 手机:
* iphone 4/4s 960*640 (3:2)
* iphone5 1136*640
* 小米1 854*480(FWVGA)
* 小米2 1280*720
*
* 分辨率对应DPI
* "HVGA mdpi"
* "WVGA hdpi "
* "FWVGA hdpi "
* "QHD hdpi "
* "720P xhdpi"
* "1080P xxhdpi "
*/
//以 720p作为标准 1920*1080 高清,填写ui标注的宽高
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();
}
}
阅读全文
0 0
- Android px适配生成Px文件
- Android屏幕适配Px
- px
- px
- 屏幕适配px-dip
- android px 转换成 dip 做代码适配
- Android屏幕适配 dp px 之间转换
- android适配之dp,px深度解析
- android屏幕适配dip,dpi,px,sp相关
- Android之——屏幕适配px转dip
- 弄懂Android手机、pad适配的dp与px
- Android屏幕适配dp、px两套解决办法
- Android 屏幕适配(dp和px转换)
- Android屏幕适配dp、px两套解决办法
- Android适配疑难问题(dp,px,sp,icon图标)
- Android屏幕适配dp、px两套解决办法
- Android屏幕适配px和dp适配两种方式
- Android屏幕适配dp、px两套解决办法
- css 的表格的添加删除
- Maven多模块项目介绍和搭建
- 【hdu2929】【高精度+动规】CQYZ_Vijos_P2102 越大越好
- 埃拉托斯特尼筛法算法复杂度(n*lnlnn)的证明([欧拉数学]素数倒数之和)
- 四位计算机的原理及其实现
- Android px适配生成Px文件
- SearchBar、Tableview右边的英文A-Z背景色设置为任意颜色
- 记第一次成功的逆向(ctf)
- UVA
- Topological Sort(25 分)
- 高精度板子
- .NET Framework 3.5 无法安装以下功能 安装错误:0x800F0906
- 图片压缩按比例压缩
- 成绩转换