全面屏手机_适配全面屏广告
适配全面屏手机的凹凸状态栏
开屏广告一般都是全屏的,在非全面屏的手机问世之前 我们只要设置activity的style为<item name="android:windowFullscreen">true</item>
,即可解决问题,但是全面屏手机出来后,如果仅仅是设置该样式,我们会发现凹凸部分是全黑并没有实现全面屏,因此对于全面屏的我们需要特殊的处理
理论基础:
- Android全面屏手机一般是指宽高比大于1.86
- Android p以及以上谷歌提供了适配凹凸部分的方法 [传送门]
- 沉浸式状态栏,设置状态栏透明
谷歌适配方案说明
但是并不是所有的手机都支持谷歌的全面屏适配方案,例如努比亚 魅族就不支持全面屏的适配方案,这是我们可以采用包含状态栏的全面屏适配方案,
先看效果图
非全面屏 — 三星手机
全面屏不支持适配全面屏 -努比亚手机
全面屏支持适配全面屏 华为P20
首先我们设置activity的样式
默认values 下的style
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@drawable/splash_bg</item>
</style>
values-v28 下的style,因为android:windowLayoutInDisplayCutoutMode是在api28才开始添加的
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@drawable/splash_bg</item>
//设置适配全面屏
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
上面我们说过了有些机型是无法适配全面屏的这时我们要将这些特殊机型给单独适配
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScreenUtil.transparencyBar(this);
TextView textView = findViewById(R.id.tv_count_down);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) textView.getLayoutParams();
//因为是全屏的 所以这里我们需要动态的设置跳过的上边距
layoutParams.topMargin += ScreenUtil.getStatusBarHeight(this);
textView.setLayoutParams(layoutParams);
//全面屏不支持去除状态栏 则退出全屏幕 采用更改状态栏为透明色的方法
if(isCongainer(android.os.Build.BRAND,getResources().getStringArray(R.array.exit_fullscreen))&&ScreenUtil.getScreentRation(this)>1.86){
exitFullScreen();
}
}
/**
* 退出全面屏手机模式
*/
private void exitFullScreen(){
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
window.setAttributes(attributes);
}
}
private boolean isCongainer(String str,String[] datas){
for (String s:datas){
if (TextUtils.equals(str,s)){
return true;
}
}
return false;
}
}
特殊机型建议存放在string文件中 这样以后再添加机型就不用修改代码了
<resources>
<string name="app_name">SplashDemo</string>
<!--广告全面屏视频 无法适配顶部凹凸部分的机型-->
<string-array name="exit_fullscreen">
<item>nubia</item>
<item>Meizu</item>
</string-array>
</resources>
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/ad_bg"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/tv_logo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_logo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#fff"
android:gravity="center"
android:text="我是APP的LOGO"
android:textColor="#000"
android:textSize="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="SpUsage" />
<TextView
android:id="@+id/tv_count_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳过 3"
android:textSize="12dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="15dp"
android:textColor="#000"
/>
</android.support.constraint.ConstraintLayout>
代码地址:代码地址-点我下载