全面屏手机_适配全面屏广告

适配全面屏手机的凹凸状态栏

开屏广告一般都是全屏的,在非全面屏的手机问世之前 我们只要设置activity的style为<item name="android:windowFullscreen">true</item>,即可解决问题,但是全面屏手机出来后,如果仅仅是设置该样式,我们会发现凹凸部分是全黑并没有实现全面屏,因此对于全面屏的我们需要特殊的处理

理论基础:

  1. Android全面屏手机一般是指宽高比大于1.86
  2. 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

<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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章