分辨率适配的取值范围

前面有篇文章《React native 分辨率适配》中提到了FixWidth,FixHeight两种适配策略,如果我选择了FixWidth模式,素材高度定多少合适呢?或者我选择了FixHeight, 素材的宽度我又选择多少合适呢?

下面是一段lua代码,分别计算了主流分辨率尺寸下,使用FixWidth/FixHeight后,高度/宽度的取值范围。

code

function caculate(design_width,design_height,resolutions)
    local max = function(arr,key) local result = 0; for i,v in ipairs(arr)do result = math.max(result,key and v[key] or v);end return result end
    local min = function(arr,key) local result = math.huge; for i,v in ipairs(arr)do result = math.min(result,key and v[key] or v);end return result end
    if design_width > design_height then design_width,design_height = design_height,design_width;end
    
    local fixed_height_results,fixed_width_results = {},{};
    for i,v in ipairs(resolutions) do
        local pos_b,pos_e = string.find(v,"*");
        width,height = tonumber(string.sub(v,pos_e+1)),tonumber(string.sub(v,1,pos_b-1));
        if width > height then width,height = height,width;end

        local resolution = width.."*"..height;
        table.insert(fixed_height_results,{resolution=resolution,w=math.ceil(design_height*width/height),h=design_height});
        table.insert(fixed_width_results,{resolution=resolution,w= design_width,h = math.ceil(design_width*height/width)});
    end

    local result = {};
    table.insert(result,{"RESOLUSTION","FIXED_WIDTH:"..design_width,"FIXED_HEIGHT:"..design_height})
    table.insert(result,{"---","---","---"});
    for i,v in ipairs(fixed_height_results) do
        local m=fixed_width_results[i];
        table.insert(result,{v.resolution,m.w.."*"..m.h,v.w.."*"..v.h});
    end
    table.insert(result,{"-",
        string.format(design_width.."*[%s - %s]",min(fixed_width_results,"h"),max(fixed_width_results,"h")),
        string.format("[%s - %s]*"..design_height,min(fixed_height_results,"w"),max(fixed_height_results,"w")),
        });

    for i,v in ipairs(result) do result[i] = table.concat(v," | ");end
    result = string.gsub(table.concat(result,"\n"),"%*"," * ");
    print(result);
end

local resolutions = {
--[[android]]"1280*720","1920*1080","854*480","960*540","800*480","1184*720","1776*1080","2560*1440","1812*1080",
--[[  ios  ]]"1136*640","1334*750","2208*1242","960*640","2048*1536","2001*1125","1024*768","1920*1080","1704*960"
}
caculate(640,1136,resolutions)

Result

RESOLUSTION FIXED_WIDTH:640 FIXED_HEIGHT:1136
720 * 1280 640 * 1138 639 * 1136
1080 * 1920 640 * 1138 639 * 1136
480 * 854 640 * 1139 639 * 1136
540 * 960 640 * 1138 639 * 1136
480 * 800 640 * 1067 682 * 1136
720 * 1184 640 * 1053 691 * 1136
1080 * 1776 640 * 1053 691 * 1136
1440 * 2560 640 * 1138 639 * 1136
1080 * 1812 640 * 1074 678 * 1136
640 * 1136 640 * 1136 640 * 1136
750 * 1334 640 * 1139 639 * 1136
1242 * 2208 640 * 1138 639 * 1136
640 * 960 640 * 960 758 * 1136
1536 * 2048 640 * 854 852 * 1136
1125 * 2001 640 * 1139 639 * 1136
768 * 1024 640 * 854 852 * 1136
1080 * 1920 640 * 1138 639 * 1136
960 * 1704 640 * 1136 640 * 1136
- 640 * [854 - 1139] [639 - 852] * 1136

结论

当你选择fixwidth=640后,高度的可选范围[854, 1139],100%可视区域为:(640,<854),但图片要设计为(640,>1139)才能完美适配到所有的分辨率。

当你选择FixHeight=1136后,宽度的可选范围[639, 852],100%可视区域为:(<639,1136),但图片要设计为(>852,1136)才能完美适配到所有的分辨率。

Links

推荐阅读更多精彩内容