java软件屏幕大小转换,android 用java写一个自动适配各种屏幕大小的工具

android 用java写一个自动适配各种屏幕大小的工具,假设我当前机器屏幕宽高为1920*1116,我页面某个组件的宽度为100px,那么在1280*800的机器上,按道理将应该是100/1920*1280=66.66px,那么讲道理思路就来了,如果我在1920*1116适配了,那么宽按照1280/1920,高按照800/1116换算不就可以了吗?

难点:写配置文件

1.在应用资源文件下形成文件,比如我当前机器为1920*1116,我想1280*800也能用,就是两个文件夹:values-1920x1116,values-1280x800

private final static String VALUE_TEMPLATE = "values-{0}x{1}";

System.out.print("TEST::"+w+"?"+h);

File fileDir = new File(dirStr + File.separator

+ VALUE_TEMPLATE.replace("{0}", w + "")//

.replace("{1}", h + ""));

fileDir.mkdir();

2,如果当前我的机器为1920*1116,那么我的values-1920x1116里就应该有横向的适配文件和纵向的适配文件,

private final static String WTemplate = "{1}px\n";

private final static String HTemplate = "{1}px\n";

private void generateXmlFile(int w, int h) {

StringBuffer sbForWidth = new StringBuffer();

sbForWidth.append("{C}\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("{C}\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("");

System.out.print("TEST::"+w+"?"+h);

File fileDir = new File(dirStr + File.separator

+ VALUE_TEMPLATE.replace("{0}", w + "")//

.replace("{1}", h + ""));

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

}

}

附件:

package com.wyt.hcy.customviewtestdemo;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintWriter;

/**

* created by hcy

*/

public class WytCreateValueFiles {

private int baseW;

private int baseH;

private String dirStr = "./app/src/main/res";

private final static String WTemplate = "{1}px\n";

private final static String HTemplate = "{1}px\n";

/**

* {0}-HEIGHT

*/

private final static String VALUE_TEMPLATE = "values-{0}x{1}";

private static final String SUPPORT_DIMESION = "1920,1116;";

private String supportStr = SUPPORT_DIMESION;

public WytCreateValueFiles(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("{C}\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("{C}\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("");

System.out.print("TEST::"+w+"?"+h);

File fileDir = new File(dirStr + File.separator

+ VALUE_TEMPLATE.replace("{0}", w + "")//

.replace("{1}", h + ""));

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;

}

/**

*

* @param args

*/

public static void main(String[] args) {

//当前屏幕宽高

int baseW = 1280;

int baseH = 800;

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 WytCreateValueFiles(baseW, baseH, addition).generate();

}

}

使用:如果当前机器为1280x800,那么就在

public static void main(String[] args) {

//当前屏幕宽高

int baseW = 1280;

int baseH = 800;

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 WytCreateValueFiles(baseW, baseH, addition).generate();

}

我想适配1920*1116

private static final String SUPPORT_DIMESION = "1920,1116;1024,600;";

运行就会在app/src/main/res 下生成文件

private String dirStr = "./app/src/main/res";

然后替换布局文件将

横向x纵向y

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值