android屏幕适配计算方式及适配values文件生成

来源:互联网 发布:淘宝账户为什么会冻结 编辑:程序博客网 时间:2024/06/17 12:10

**

android屏幕适配计算方式及适配values文件生成

**
随着时代的发展,我们的手机的分辨率越来越高,并且伴随着的尺寸也越来越多,对于android程序开发的我们,大多数android手机屏幕适配也成为了一个问题,因为不像以前只有几种规定尺寸及分辨率的手机了

所以这个问题我们要如何进行解决呢?
大家都知道android会根据屏幕密度的不同,去不同的文件夹下面找对应的图片,所以控件的宽高我们也可以使用同一种方法,在android资源目录下面设置手机屏幕对应下的values文件夹,然后把各像素数列表放到对应的资源文件

这里写图片描述

问题又来了,那对应的文件夹要放什么值呢?

首先我们来看一下dp与px的转换
因为ui给你的设计图是以px为单位的,Android开发则是使用dp作为单位的,那么该如何转换呢?

密度类型    代表的分辨率(px)  屏幕密度(dpi)   换算(px/dp)   比例低密度(ldpi)   240x320     120     1dp=0.75px  3中密度(mdpi)   320x480     160     1dp=1px     4高密度(hdpi)   480x800     240     1dp=1.5px   6超高密度(xhdpi)     720x1280    320     1dp=2px     8超超高密度(xxhdpi)   1080x1920   480     1dp=3px     12在Android中,规定以160dpi(即屏幕分辨率为320x480)为基准:1dp=1pxppi的运算方式是:PPI = √(长度像素数² + 宽度像素数²) / 屏幕对角线英寸数dp:Density-independent pixels,以160PPI屏幕为标准,则1dp=1px,dp和px的换算公式 :dp*ppi/160 = px。比如1dp x 320ppi/160 = 2px。sp:Scale-independent pixels,它是安卓的字体单位,以160PPI屏幕为标准,当字体大小为 100%时, 1sp=1px。sp 与 px 的换算公式:sp*ppi/160 = px总结得出:px = dp*ppi/160dp = px / (ppi / 160)px = sp*ppi/160sp = px / (ppi / 160)dp = sp? 

这样我们就可以得出对应的目录下面控件的像素值了,当然,要我吗一个一个这样去算还是有点不合理,不仅有这么多个尺寸的手机,有这么多个要转变的像素,所以,最后整理了一份代码,让它给我们自动生成出对应的结果集

package com.thinkgem.jeesite.common.utils;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;public class AndroidValuesXml {    private final static String rootPath = "C:\\layoutvalues\\values-{0}x{1}\\";    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";    private final static float dw = 320f;    private final static float dh = 480f;    public static void main(String[] args) {        screenString(320, 480);        screenString(480, 800);        screenString(480, 854);        screenString(540, 960);        screenString(600, 1024);        screenString(720, 1184);        screenString(720, 1196);        screenString(720, 1280);        screenString(768, 1024);        screenString(800, 1280);        screenString(1080, 1812);        screenString(1080, 1920);        screenString(1440, 2560);    }    public static void screenString(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;        for (int i = 1; i < 320; i++) {            sb.append(WTemplate.replace("{0}", i + "").replace("{1}",                    change(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;    }}

生成之后的文件格式(单位px)

lay_x.xml

<?xml version="1.0" encoding="utf-8"?><resources><dimen name="x1">3.375px</dimen><dimen name="x2">6.65px</dimen><dimen name="x3">10.125px</dimen>...<dimen name="x320">1080px</dimen></resources>

lay_y.xml

<?xml version="1.0" encoding="utf-8"?><resources><dimen name="y1">4px</dimen><dimen name="y2">8px</dimen><dimen name="y3">12px</dimen><dimen name="y4">16px</dimen>...<dimen name="y480">1920px</dimen></resources>

切记
必须在默认values里面也创建对应默认lay_x.xml和lay_y.xml文件,
如lay_x.xml(单位dp)

<?xml version="1.0" encoding="utf-8"><resources><dimen name="x1">1.0dp</dimen><dimen name="x2">2.0dp</dimen>...</resources>

最后,调用代码实例如下

<TextView    android:id="@+id/tv"    android:text="@string/hello_world"    android:layout_width="@dimen/x100"    android:layout_height="@dimen/y100"/>