手機異形屏的出現 , 給手遊自適應帶來了新的麻煩 。對於Game前端來講 , 奇葩的是 , 劉海屏 / 打孔屏 / 水滴屏,未來還不知道出現個什麼屏 , 更爲奇葩的是 , 劉海吧 , 大小長度不同 ; 打孔吧 , 位置不同 ; 水滴屏 , 大小不同 。
SO , 本篇Blog , 給出了廣泛的作法 : 將劉海,打孔,水滴所有部分去掉,不顯示任何內容。固然讀者能夠更具不一樣的機型 , 定製的自適應異形屏。
前端
1, 利用wx.getSystemInfo , 能夠獲得客戶端手機的型號
2 , 由於各個奇葩的孔大小不同,因此有一個配置表:微信
{ "screen": [ { "model" : [ "Iphone 11 Pro" , "Iphone 11", "Iphone X" , "Iphone XR" , "Iphone XS MAX" , "HuaWei MATE 30 Pro 5G" , "HuaWei MATE 30 Pro" , "HuaWei MATE 20 Pro", "AXON 9 Pro" , "Galaxy S10" , "MI 8SE" ], "defect" : 55 }, { "model" : [ "HuaWei P30 Pro" , "Mi 8" , "Honor 20 Pro" , "VIVO Z3X" , "OPPO A5", "HuaWei P20 Pro","HuaWei Mate 20 5G", "HuaWei MATE 30" , "Galaxy Note10+ 5G" , "HuaWei P20" , "MI 6 Pro" ], "defect" : 45 }, { "model" : ["Honor 10" , "Honor V10" , "MI 9" , "MI 9 Pro" , "MI 9 Pro 5G" , "VIVO X23" , "VIVO Z25" , "VIVO Z5x" , "VIVO Z3" , "VIVOZ3I", "VIVO Y7S" , "VIVO Y3" , "VIVO Y91" , "VIVO U3" , "VIVO U3X" , "VIVO U1" , "OPPO RENO ACE" , "OPPO RENO Z" , "OPPO R17 PRO" , "OPPO R17", "OPPO R15X" , "OPPO A11" , "OPPO A11X" , "OPPO A9X" , "OPPO A9" , "OPPO A7" , "OPPO K5" , "OPPO K1" , "MI CC9" , "REDMI NOTE 7 PRO", "REDMI NOTE 7" , "REDMI 7" , "MI 9 SE" , "REDMI 8" , "REDMI 8A" , "REDMI NOTE 8 PRO" , "REDMI NOTE 8" , "HuaWei NOVA 5 Pro","HuaWei NOVA 5I Pro", "Honor V20" , "Honor NOVA 5Z" , "Honor 20" , "Honor Play3E" , "Honor Play3" , "Honor 20S" , "HuaWei MATE 20 X 5G" , "HuaWei NOVA 5" , "Honor 20 Pro", "Honor 20I" , "HuaWei MATE 20" , "HuaWei NOVA 4E" , "HuaWei NOVA 4" , "HuaWei P30" , "HuaWei P30 Pro" , "Honor 8X MAX" , "OnePlus 7" , "OnePlus 7T", "AXON 10 Pro", "AXON 10 Pro 5G" , "Blade A7" , "Blade V10" , "Blade V10 VITA" , "Galaxy A90 5G" , "Galaxy A50S" , "Samsung Galaxy M30s", "MI CC9E" ], "defect" : 35 } ] }
其中 , defect的意思是 : 屏幕上端劉海、孔、水滴所佔用的最大的高度 PX
3,在Layer中(eui.Group)重寫 , 注意與手機model匹配(這裏不寫):
measure , updateDisplayList2個函數 , 重點是updateDisplayList函數 , 以下:ide
protected measure(): void { if (!this.$layout) { this.setMeasuredSize(0, 0); return; } this.$layout.measure(); } protected updateDisplayList(unscaledWidth:number, unscaledHeight:number):void{ if (this.$layout) { if( egret.Capabilities.runtimeType == egret.RuntimeType.WXGAME ){ DeviceUtils.MobilePhoneDefect( ( $defect : number ) : void => { if( $defect != null && $defect > 0 ){ this.$layout.updateDisplayList(unscaledWidth, unscaledHeight - $defect); }else{ this.$layout.updateDisplayList(unscaledWidth, unscaledHeight); } } ); }else{ this.$layout.updateDisplayList(unscaledWidth, unscaledHeight); } } this["updateScrollRect"](); }
注意 :
4,調整Layer的Y座標函數
private resetLocation2BaseEuiLayer(layer: egret.DisplayObjectContainer) : void{ if( egret.Capabilities.runtimeType == egret.RuntimeType.WXGAME ){ DeviceUtils.MobilePhoneDefect( ( $defect : number ) : void => { if( $defect != null && $defect > 0 ){ layer.y = $defect; } }); } }