Android最全螢幕適配的幾個重要概念(三)
前言
螢幕適配的一種方法,生成多個XML檔案,來實現螢幕適配的方法。
提供一個工具類如下:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class MakeXml {
private final static String rootPath = "D:\\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;
System.out.println("w= " + w + " , h= " + h + " , cellw= " + cellw + " , dw= " + dw);
for (int i = 1; i < 320; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
change(cellw * i) + ""));
System.out.println("i = " + i + " , cellw*i = " + 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;
}
}
相關文章
- android 螢幕適配Android
- Android 螢幕適配:最全面的解決方案Android
- android螢幕適配三:通過畫素密度適配Android
- Android 主流螢幕以及適配Android
- Android螢幕適配(理論適配100%機型)Android
- Android 螢幕適配終結者Android
- 螢幕適配
- android 今日頭條的螢幕適配理解Android
- Android螢幕適配總結和思考Android
- Android技能樹 — 螢幕適配小結Android
- AutoLayout螢幕適配
- Flutter螢幕適配Flutter
- Android螢幕適配前先了解這些Android
- android 螢幕適配一:通過自定義View的方式實現適配AndroidView
- flutter 螢幕尺寸適配 字型大小適配Flutter
- Android dp方式的螢幕適配工具使用(Android Studio外掛方式)Android
- android 螢幕適配二:手寫百分比佈局適配Android
- 移動APP測試-Android螢幕適配問題(一)APPAndroid
- 移動APP測試:Android螢幕適配問題二APPAndroid
- Android螢幕適配方案Android
- android螢幕適配方法Android
- Android 螢幕自適應Android
- @media 移動端螢幕適配
- 【postcss-px-to-viewport】螢幕適配CSSView
- Flutter螢幕適配 - 等比縮放Flutter
- 極其簡單的Flutter 螢幕適配Flutter
- Android螢幕適配很難嘛?其實也就那麼回事Android
- 移動 web 端螢幕適配 – remWebREM
- H5 分層螢幕適配H5
- Android螢幕適配方案分析Android
- Android APP全方位效能調優之螢幕適配終結者AndroidAPP
- 詳解 Android 12L|更好地適配大螢幕裝置Android
- 淺談-web螢幕適配的解決方案Web
- Flutter螢幕適配,簡單粗暴的全域性適配方式Flutter
- Andoid螢幕適配終極手段(小編用過最得勁的dp適配)
- Android 螢幕適配從未如斯簡單(已廢棄該使用方式)Android
- 更好地適配大螢幕裝置 | 2021 Android 開發者峰會Android
- Android螢幕適配很麻煩嗎?不!太簡單了。。。(持續更新)Android