package com.qu;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Aaaa {
public static void main(String[] args) {
String filePath = "E:\\AndroidStudioProjects\\aa.txt";
String buffer = pxTodp();
TextToFile(filePath,buffer);
}
public static void TextToFile(final String strFilename, final String strBuffer)
{
try
{
// 创建文件对象
File fileText = new File(strFilename);
// 向文件写入对象写入信息
FileWriter fileWriter = new FileWriter(fileText);
// 写文件
fileWriter.write(strBuffer);
// 关闭
fileWriter.close();
}
catch (IOException e)
{
//
e.printStackTrace();
}
}
public static String pxTodp(){
int width = 750;//屏幕宽度
int height = 1334;//屏幕高度
float screenInch = 4.7f;//屏幕尺寸
//设备密度公式
float density = (float) Math.sqrt(width * width + height * height) / screenInch / 160;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n");
for (int px = 0; px <= 1000; px += 2) {
//像素值除以density
String dp = px * 1.0f / density + "";
//拼接成资源文件的内容,方便引用
if (dp.indexOf(".") + 4 < dp.length()) {//保留3位小数
dp = dp.substring(0, dp.indexOf(".") + 4);
}
stringBuilder.append("<dimen name=\"px").append(px + "").append("\">").append(dp).append("dp</dimen>\n");
}
stringBuilder.append("</resources>");
return stringBuilder.toString();
}
}