android 屏幕适配(2)

这段代码展示了如何在Java中创建一个方法,用于生成XML文件,处理输入参数验证,并根据给定的宽度和高度生成布局文件。它涉及到了字符串操作、XML模板替换以及文件系统管理。
摘要由CSDN通过智能技术生成

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(“”);

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(“”);

StringBuffer sbForHeight = new StringBuffer();

sbForHeight.append(“<?xml version=\"1.0\" encoding=\"utf-8\"?>\n”);

sbForHeight.append(“”);

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(“”);

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) {

int baseW = 320;

int baseH = 480;

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];

}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。


《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。


[外链图片转存中…(img-awFR2aTj-1713754899133)]

[外链图片转存中…(img-GlGanjeq-1713754899134)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 30
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值