適配模式分兩種
1,不同屏幕相同高度
2褂痰,不同屏幕相同比例
我們采用第二種模式
等比適配原則
1,將常用屏幕寬高拿來症虑。
2缩歪,針對所有的寬高,將寬度做一個默認(rèn)等分谍憔,一般設(shè)計(jì)圖是ios的匪蝙,采用750分辨率的,所以用750等分习贫。
3逛球,盡量做到所有寬度的xhdpi和xxhdpi兩種密度適配。
如1080寬度的屏幕苫昌,即颤绕,
values-xhdpi-1920*1080
values-xxhdpi-1920*1080
影響系統(tǒng)屏幕匹配的因素有:寬,高,密度
下面開始試驗(yàn):
1奥务,手機(jī)是模擬器物独,1080*1920 8 寸,帶底部導(dǎo)航氯葬,拿到屏幕的參數(shù)是:1080*1824 density 2.0 xhdpi挡篓。
2,自定義單位屏幕750等分帚称,高度100單位官研,即在1080屏上 高度144像素。
實(shí)驗(yàn)步驟:
1闯睹,1080*1824 = 144(僅像素戏羽,寬高都匹配,)
2瞻坝,1080*1800 = 10(僅像素蛛壳,寬匹配,高差24)
3所刀,1080*1812 = 20(僅像素衙荐,寬匹配,高查12)
4浮创,xhdpi-1800*1080=50(dpi匹配忧吟,高度相差24,寬度不差)
5斩披,xhdpi-1812*1080=60(dpi匹配溜族,高度相差12,寬度不差)
6垦沉,xhdpi-1824*1056=30(dpi匹配煌抒,高度不差,寬度相差24)
7厕倍,xhdpi-1824*1068=40(dpi匹配寡壮,高度不差,寬度相差12)
8讹弯,xhdpi=96(僅dpi匹配)
9况既,hdpi-1824*1080=90(像素寬高匹配,dpi低)
10组民,xxhdpi-1824*1080=100(像素寬高匹配棒仍,dpi高)
//7,xhdpi-1824*1080=144
實(shí)際=50
得出屏幕適配原則
1臭胜,密度優(yōu)先級最高
2莫其,密度匹配之后癞尚,如果有像素匹配,則選最接近的寬度/高度
實(shí)驗(yàn)一榜配,僅像素適配
1否纬,1080*1824
0,value 144
1蛋褥,1080*1800(高小24)10
2,1080*1812(高小12)20
3睛驳,1080*1836(高大12)30
4烙心,1080*1848(高大24)40
選中=20,1080*1212。
猜想高大12和小12乏沸,選擇小12淫茵,
如果大10和小12,選擇哪個蹬跃?
將1836-》1035匙瘪,1825,1824蝶缀,發(fā)現(xiàn)結(jié)果都是20丹喻,20,30.也就是說翁都。
在無dpi情況下碍论,像素寬度匹配的時候,選擇最接近的且低于屏幕的高度的值柄慰。
5鳍悠,1056*1824(寬小24)50
6,1068*1824(寬小12)60
7坐搔,1092*1824(寬大12)70
8藏研,1104*1824(寬大24)80
選中=60,1068*1824。
猜想寬大12和小12概行,選擇小12蠢挡,
如果打10和小12,選擇哪個占锯?
將1092-》1091袒哥,1081,1080消略,發(fā)現(xiàn)結(jié)果都是60堡称,60,70艺演,得出結(jié)論:
在無dpi情況下却紧,像素高度匹配的時候桐臊,選擇最接近的切低于屏幕的寬度的值。
9晓殊,1080*1812(高小12)40
10断凶,1068*1824(寬小12)60
選中=60,1068*1824,得出結(jié)論:
在無dpi情況下巫俺,像素某一個匹配的時候认烁,如果另一個低于屏幕的值相等,選擇寬介汹。
猜想如果另一個低于屏幕的值最接近屏幕却嗡,選擇最接近的,如果一樣選擇寬嘹承。
將1812-》1822窗价,發(fā)現(xiàn)結(jié)果為40,得出結(jié)論:
在無dpi情況下叹卷,像素某一個匹配的時候撼港,另一個低于屏幕的值最接近屏幕,選擇最接近的骤竹;如果另一個低于屏幕的值相等帝牡,選擇寬。
9瘤载,1068*1812(寬高都小12)90
10否灾,1068*1836(寬小12高大12)100
11,1092*1836(寬大12高小12)110
12鸣奔,1092*1812(寬大12高大12)120
選中90墨技,得出結(jié)論:
在無dpi情況下,寬高像素都不匹配的情況挎狸,選擇小于屏幕像素的值扣汪。
猜想,選擇小于屏幕的锨匆,任意一個最接近的值崭别。
將1068*1836-1068*1822(寬小12,高小2) 結(jié)果為100恐锣,表示選中1068*1822茅主。得出結(jié)論:
在無dpi情況下,寬高像素都不匹配的情況土榴,選擇小于屏幕像素的诀姚,且寬和高最解決屏幕的。
將1092*1836-》1067*1813(寬小13玷禽,高小11)赫段,1068*1836-》1069*1811(寬小11呀打,高小13)
結(jié)果110,即選中1067*1813糯笙,得出結(jié)論:
在無dpi情況下贬丛,像素都不匹配的情況,一個寬接近给涕,一個高接近豺憔,高更接近的和寬更接近的值相等時,選擇高更接近的稠炬。
實(shí)驗(yàn)二焕阿,dip+屏幕像素
創(chuàng)建文件代碼工具類
```
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
* Created by zhy on 15/5/3.
*/
public class GenerateValueFiles {
? ? private static final int baseW = 750;
? ? private static final int baseH = 1280;
? ? private static final String dirStr = "D:/Demo/Test/app/src/main/res";
? ? 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";
? ? /**
? ? * {0}-HEIGHT
? ? */
? ? private final static String VALUE_TEMPLATE = "values-{0}x{1}";
? ? private static final String[] SUPPORT_DIMESION = {
? ? ? ? ? ? "240,320",//l
? ? ? ? ? ? "320,480",//m
? ? ? ? ? ? "480,800",//h
? ? ? ? ? ? "480,854",
? ? ? ? ? ? "540,960",
? ? ? ? ? ? "600,1024",
? ? ? ? ? ? "640,960",
? ? ? ? ? ? "720,1184",
? ? ? ? ? ? "720,1196",
? ? ? ? ? ? "720,1280",//xh
? ? ? ? ? ? "720,1480",//sansumg s9
? ? ? ? ? ? "752,1280",
? ? ? ? ? ? "768,1024",
? ? ? ? ? ? "768,1280",
? ? ? ? ? ? "800,1280",
? ? ? ? ? ? "1024,1920",
? ? ? ? ? ? "1080,1812",
? ? ? ? ? ? "1080,1920",//xxh
? ? ? ? ? ? "1080,2160",//vivo z5x
? ? ? ? ? ? "1080,2220",//sansumg s9
? ? ? ? ? ? "1080,2248",//華為p20 xiaomi 8
? ? ? ? ? ? "1080,2340",//vivo z5x
? ? ? ? ? ? "1200,1920",
? ? ? ? ? ? "1440,2560",//xxxh
? ? ? ? ? ? "1440,2960",//sansumg s9
? ? ? ? ? ? "1536,2048",
? ? ? ? ? ? "1600,2560",
? ? ? ? ? ? "2160,3840"http://xxxh
? ? };
? ? public GenerateValueFiles() {
? ? ? ? File dir = new File(dirStr);
? ? ? ? if (!dir.exists()) {
? ? ? ? ? ? dir.mkdir();
? ? ? ? }
? ? ? ? System.out.println(dir.getAbsoluteFile());
? ? }
? ? public void generate() {
? ? ? ? for (String val : SUPPORT_DIMESION) {
? ? ? ? ? ? String[] wh = val.split(",");
? ? ? ? ? ? generateXmlFile(Integer.parseInt(wh[0]), Integer.parseInt(wh[0]), Integer.parseInt(wh[1]), baseW, WTemplate, "dimens.xml");
//? ? ? ? ? ? generateXmlFile(Integer.parseInt(wh[1]), Integer.parseInt(wh[0]),Integer.parseInt(wh[1]), baseH, HTemplate, "dimens_y.xml");
? ? ? ? }
? ? }
? ? /**
? ? * 尺寸px
? ? *
? ? * @param size
? ? * @param dimens_x
? ? * @param dimens_y
? ? * @param baseW
? ? * @param WTemplate
? ? * @param fileName
? ? */
? ? private void generateXmlFile(int size, int dimens_x, int dimens_y, int baseW, String WTemplate, String fileName) {
? ? ? ? StringBuffer sbForWidth = new StringBuffer();
? ? ? ? sbForWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
? ? ? ? sbForWidth.append("<resources>\n");
? ? ? ? float cellw = size * 1.0f / baseW;
? ? ? ? System.out.println("width : " + size + "," + baseW + "," + cellw);
? ? ? ? for (int i = 1; -i >= -100; i++) {
? ? ? ? ? ? sbForWidth.append("? ? ")
? ? ? ? ? ? ? ? ? ? .append(WTemplate.replace("{0}", "_" + (i)).replace("{1}",
? ? ? ? ? ? ? ? ? ? ? ? ? ? change(-cellw * i) + ""));
? ? ? ? }
//? ? ? ? sbForWidth.append("? ? ")
//? ? ? ? ? ? ? ? .append(WTemplate.replace("{0}", "_" + baseW + "").replace("{1}",
//? ? ? ? ? ? ? ? ? ? ? ? -size + ""));
? ? ? ? for (int i = 1; i <= 800; i++) {
? ? ? ? ? ? sbForWidth.append("? ? ")
? ? ? ? ? ? ? ? ? ? .append(WTemplate.replace("{0}", i + "").replace("{1}",
? ? ? ? ? ? ? ? ? ? ? ? ? ? change(cellw * i) + ""));
? ? ? ? }
//? ? ? ? sbForWidth.append("? ? ")
//? ? ? ? ? ? ? ? .append(WTemplate.replace("{0}", baseW + "").replace("{1}",
//? ? ? ? ? ? ? ? ? ? ? ? size + ""));
? ? ? ? sbForWidth.append("</resources>");
? ? ? ? File fileDir = new File(dirStr + File.separator
? ? ? ? ? ? ? ? + VALUE_TEMPLATE.replace("{0}", dimens_y + "")//
? ? ? ? ? ? ? ? .replace("{1}", dimens_x + ""));
? ? ? ? if (!fileDir.exists()) {
? ? ? ? ? ? fileDir.mkdir();
? ? ? ? }
? ? ? ? File layxFile = new File(fileDir.getAbsolutePath(), fileName);
? ? ? ? try {
? ? ? ? ? ? PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
? ? ? ? ? ? pw.print(sbForWidth.toString());
? ? ? ? ? ? pw.close();
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public static float change(float a) {
? ? ? ? int temp = (int) (a * 10);
? ? ? ? return temp / 10f;
? ? }
? ? public static void main(String[] args) {
? ? ? ? new GenerateValueFiles().generate();
? ? }
}
```
這是我之前寫過的關(guān)于屏幕適配原則邏輯的文章