Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件

来源:互联网 发布:淘宝低价引流软件 编辑:程序博客网 时间:2024/05/23 20:52

在利用dimens.xml进行android设备适配的时候,在xml中我们不能直接定义写死的各个dp,px,sp等。应该使用@dimens/*******的方法,根据android设备的dpi和分辨率来获取不同文件夹中的dimens.xml文件。

而各个文件夹的dimes.xml存在比例关系,例如xhdpi中的dimens.xml比hdpi中的dimens.xml的数值大1.5倍。dimens.xml中的数值按照 ldpi,mdpi,hdpi,xhdpi,xxhdpi,依次升级1.5倍。

在dimens.xml中全部使用px来进行适配,这是因为对于相同分辨率有不同尺寸的屏幕,我们根据px来适配,可以直接按照px来缩放UI,而不会出现根据dp来适配出现的问题(不同尺寸的屏幕手机,相同的控件大小)

比如1920*1080分辨率的手机,当手机尺寸为6寸多,那么这手机的ppi为320,程序调用的是xhdpi-values中的dimens.xml,如果直接这样调用,那么会出现问题,我们实际所需的是xxhdpi中的dimens.xml,我们可以这样做:

新建一个文件夹xhdpi-values-1920x1080,将xxhdpi中的dimens.xml复制到这个文件夹,那么适配就完成了,很轻松。但是写不同的dimens.xml会比较累,然后dimens.xml中存在倍数关系,1.5倍,这样我们可以通过程序来自动帮我们完成,

下面就是我写的,根据xhdpi中的dimens.xml来生成其余文件夹中的dimens.xml。

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.InputStream;import java.io.InputStreamReader;/*** @date       2014年5月29日* @author     谭帅* @说明         以values-xhdpi中的dimens.xml为标准,*         更新其余dimens.xml的数据以适配不同分辨率和密度的屏幕手机*/public class DimensUtils {// xhdpi中dimens.xml所在目录static String srcFilePath = "E:\\kw_WorkSpace\\KuwoPlayerV3_UI_aspect\\res\\values-xhdpi\\dimens.xml";// 新生成的xxhdpi中dimens.xml所在目录static String xxhdpiFilePath = "C:\\Users\\Administrator\\Desktop\\new_dimens\\values_xxhdpi_dimens.xml";// 新生成的hdpi中dimens.xml所在目录static String hdpiFilePath = "C:\\Users\\Administrator\\Desktop\\new_dimens\\values_hdpi_dimens.xml";// 新生成的mdpi中dimens.xml所在目录static String mhdpiFilePath = "C:\\Users\\Administrator\\Desktop\\new_dimens\\values_mdpi_dimens.xml";// 新生成的ldpi中dimens.xml所在目录static String lhdpiFilePath = "C:\\Users\\Administrator\\Desktop\\new_dimens\\values_ldpi_dimens.xml";// 缩放比例(xxhdpi中为乘,其余为除)static float factor = 1.5f;public static void main(String[] args) {File file = new File(srcFilePath);File xxhFile = new File(xxhdpiFilePath);File hFile = new File(hdpiFilePath);File mFile = new File(mhdpiFilePath);File lFile = new File(lhdpiFilePath);InputStream inStream;try {inStream = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(inStream));String brLine = br.readLine();FileWriter xxhWriter = new FileWriter(xxhFile);BufferedWriter xxhBw = new BufferedWriter(xxhWriter);FileWriter hWriter = new FileWriter(hFile);BufferedWriter hBw = new BufferedWriter(hWriter);FileWriter mWriter = new FileWriter(mFile);BufferedWriter mBw = new BufferedWriter(mWriter);FileWriter lWriter = new FileWriter(lFile);BufferedWriter lBw = new BufferedWriter(lWriter);while (brLine != null) {if (brLine.endsWith("px</dimen>")) {int start = brLine.indexOf("\">") + 2;int end = brLine.indexOf("px<");String temp = brLine.substring(start, end);int px = Integer.parseInt(temp);// 写入xxhdpi中-乘以(1.5)xxhBw.append(brLine.subSequence(0, start)+ getNewPxString("xxhdpi", px)+ brLine.substring(end) + "\n");// 写入hdpi中-除以(1.5)hBw.append(brLine.subSequence(0, start)+ getNewPxString("hdpi", px)+ brLine.substring(end) + "\n");// 写入mdpi中-除以(1.5*1.5)mBw.append(brLine.subSequence(0, start)+ getNewPxString("mdpi", px)+ brLine.substring(end) + "\n");// 写入ldpi中-除以(1.5*1.5)lBw.append(brLine.subSequence(0, start)+ getNewPxString("ldpi", px)+ brLine.substring(end) + "\n");} else {xxhBw.append(brLine + "\n");hBw.append(brLine + "\n");mBw.append(brLine + "\n");lBw.append(brLine + "\n");}brLine = br.readLine();}br.close();xxhBw.close();hBw.close();mBw.close();lBw.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("run over");}private static String getNewPxString(String fileSrc, int px) {float tempFloat = getFactor(fileSrc, px) - (int) getFactor(fileSrc, px);int newPx;if (tempFloat >= 0.5) {newPx = (int) getFactor(fileSrc, px) + 1;} else {newPx = (int) getFactor(fileSrc, px);}String newPxString = newPx + "";return newPxString;}private static float getFactor(String fileSrc, int px) {if (fileSrc.equals("xxhdpi")) {return px * factor;} else if (fileSrc.equals("hdpi")) {return px / factor;} else if (fileSrc.equals("mdpi")) {return px / (factor * factor);} else if (fileSrc.equals("ldpi")) {return px / (factor * factor * factor);} else {return 0;}}}


0 0
原创粉丝点击