First POST build by Jekyll.github:https://github.com/nishibaiyang
Android全面屏适配
参考:https://www.jianshu.com/p/defcf4f29a12
Galaxy S8及S8+分别搭载“5.8”与“6.2”大屏,高达84%的屏幕占比为Galaxy S8及S8+在游戏娱乐、观看视频时带来深度沉浸式视觉体验。但是与此同时S8却有着一个奇葩的屏幕比例:18.5比9,屏幕分辨率:2960×1440。通常我们在开发过程中android的标准设计图为1920×1080,ios为1334×750,默认采用16比9的比例来设计效果图。android机型众多,分辨率千奇百怪,这回三星又来添乱了,实际开发过程中就遇到了三星S8的适配问题。上下的大黑边,而且视频播放来回切换还会有黑边加大的情况。
通过修改界面布局等方法都不能达到满意的效果,冥冥之中知道是三星s8适配的问题。本来想着应该蛮复杂的,后来通过查阅资料竟然比iphonex简单多了。哈哈。
直接来揭晓答案吧!
开发者只需在App的AndroidManifest.xml文件
<meta-data android:name="android.max_aspect" android:value="2.1" />
Android 标准接口中,支持应用声明其支持的最大屏幕高宽比(maximum aspect ratio)。具体声明如下,其中的 ratio_float 被定义为是高除以宽,以16:9为例,ratio_float = 16/9 = 1.778 (18.5:9则为2.056)。
若开发者没有声明该属性,ratio_float 的默认值为1.86,小于2.056,因此这类应用在三星S8上,默认不会全屏显示,屏幕两边会留黑。
那,怎么动态修改meta-data的值呢?
ApplicationInfo applicationInfo = null;
try {
applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if(applicationInfo == null){
throw new IllegalArgumentException(" get application info = null, has no meta data! ");
}
System.out.println("============="+applicationInfo.metaData.getString("android.max_aspect"));
applicationInfo.metaData.putString("android.max_aspect", "2.1");
System.out.println("============="+applicationInfo.metaData.getString("android.max_aspect"))
上面就可以在代码中动态的修改