有这样一个需求,需要将一个页面等比例缩放,适应不同分辨率下的展示效果。
例如:在800×600下显示的某div的宽度是100px;在1024×768下div的宽度要200px。
原来的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.hh {
font-size: 18px;
width: 100px;
background-color: #f0f;
}
</style>
</head>
<body>
<div>
<span style="font-size: 12px;">哈哈</span>
<div class="hh">
呵呵
</div>
</div>
</body>
</html>
放大2倍后的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<style>
.hh {
font-size: 36px;
width: 200px;
background-color: #f0f;
}
</style>
</head>
<body>
<div>
<span style="font-size: 24px;">哈哈</span>
<div class="hh">呵呵</div></div>
</body>
</html>
java工具类
package mysqlTest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Reversor2 {
public static String[] LENGTH_UNIT = {"pt", "px"};
public static void main(String[] args) {
try {
String pathname = "D:\\www\\static\\zxd.html";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(
new FileInputStream(filename), "utf-8");
BufferedReader br = new BufferedReader(reader);
StringBuilder res = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line != null && !line.equals("")) {
res.append(line);
}
}
br.close();
String html = dealWithLine(res.toString(), 2); //放大2倍
File writename = new File("D:\\www\\zx\\zx-1.html"); // 相对路径,如果没有则要建立一个新的output。txt文件
writename.createNewFile(); // 创建新文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(writename),"utf-8"));
out.write(html);
out.flush(); // 把缓存区内容压入文件
out.close(); // 最后记得关闭文件
} catch (Exception e) {
e.printStackTrace();
}
}
public static String dealWithLine(String str, float scale) {
char[] cs = str.toCharArray();
StringBuilder sb = new StringBuilder();
boolean isChecking = false;
String checkBox = "";
int len = cs.length;
for (int i = 0; i < len; i++) {
char c = cs[i];
if ((c >= 48 && c <= 57) || c == 46) {
isChecking = true;
checkBox += c;
} else {
if (isChecking && i < len-1) {
for (String unit : LENGTH_UNIT) {
if ((c + "" + cs[i+unit.length()-1]).equals(unit)) {
checkBox = subZeroAndDot((Float.parseFloat(checkBox) * scale) + "");
}
}
}
sb.append(checkBox += c);
checkBox = "";
isChecking = false;
}
}
sb.append(checkBox);
return sb.toString();
}
/**
* 使用java正则表达式去掉多余的.与0
* @param s
* @return
*/
public static String subZeroAndDot(String s){
if(s.indexOf(".") > 0){
s = s.replaceAll("0+?$", "");//去掉多余的0
s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
}
return s;
}
}