laobie / StatusBarUtil

A util for setting status bar style on Android App.
http://t.cn/Rq746Kb
Apache License 2.0
8.81k stars 1.72k forks source link

StatusBarUtil.setColorForSwipeBack 结合BGASwipeBackLayout-Android 高版本华为手机多出状态栏View高度 #193

Closed LouisMari1991 closed 6 years ago

LouisMari1991 commented 6 years ago

在Demo增加依赖 BGASwipeBackLayout

compile 'cn.bingoogolapple:bga-swipebacklayout:1.1.8@aar'

5.1魅族手机显示正常 8.0 华为手机显示如下图所示.

506383759290259841

package com.jaeger.statusbarutil;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper;
import com.jaeger.library.StatusBarUtil;
import com.jaeger.statusbardemo.R;
import java.util.Random;

/**
 * Created by Jaeger on 2016/10/15.
 *
 * Email: chjie.jaeger@gmail.com
 * GitHub: https://github.com/laobie
 */

public class SwipeBackActivity extends BaseActivity implements BGASwipeBackHelper.Delegate {
  private Button mBtnChangeColor;
  private int mColor = Color.WHITE;
  private Toolbar mToolbar;

  protected BGASwipeBackHelper mSwipeBackHelper;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置右滑动返回
    //Slidr.attach(this);
    initSwipeBackFinish();
    setContentView(R.layout.swipe_back_activity);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mBtnChangeColor = (Button) findViewById(R.id.btn_change_color);

    setSupportActionBar(mToolbar);
    if (getSupportActionBar() != null) {
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    //mToolbar.setBackgroundColor(mColor);
    mBtnChangeColor.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        Random random = new Random();
        mColor = 0xff000000 | random.nextInt(0xffffff);
        //mToolbar.setBackgroundColor(mColor);
        StatusBarUtil.setColorForSwipeBack(SwipeBackActivity.this, mColor, 38);
      }
    });
  }

  @Override protected void setStatusBar() {
    StatusBarUtil.setColorForSwipeBack(this, mColor, 0);
    StatusBarUtil.setLightMode(this);
  }

  /**
   * 初始化滑动返回。在 super.onCreate(savedInstanceState) 之前调用该方法
   */
  private void initSwipeBackFinish() {
    mSwipeBackHelper = new BGASwipeBackHelper(this, this);

    // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
    // 下面几项可以不配置,这里只是为了讲述接口用法。

    // 设置滑动返回是否可用。默认值为 true
    mSwipeBackHelper.setSwipeBackEnable(true);
    // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
    mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true);
    // 设置是否是微信滑动返回样式。默认值为 true
    //mSwipeBackHelper.setIsWeChatStyle(true);
    // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
    mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow);
    // 设置是否显示滑动返回的阴影效果。默认值为 true
    mSwipeBackHelper.setIsNeedShowShadow(true);
    // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
    mSwipeBackHelper.setIsShadowAlphaGradient(true);
    // 设置触发释放后自动滑动返回的阈值,默认值为 0.3f
    mSwipeBackHelper.setSwipeBackThreshold(0.3f);
    // 设置底部导航条是否悬浮在内容上,默认值为 false
    mSwipeBackHelper.setIsNavigationBarOverlap(false);
  }

  /**
   * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回,如果某个界面不想支持滑动返回则重写该方法返回 false 即可
   */
  @Override public boolean isSupportSwipeBack() {
    return true;
  }

  /**
   * 正在滑动返回
   *
   * @param slideOffset 从 0 到 1
   */
  @Override public void onSwipeBackLayoutSlide(float slideOffset) {
  }

  /**
   * 没达到滑动返回的阈值,取消滑动返回动作,回到默认状态
   */
  @Override public void onSwipeBackLayoutCancel() {
  }

  /**
   * 滑动返回执行完毕,销毁当前 Activity
   */
  @Override public void onSwipeBackLayoutExecuted() {
    mSwipeBackHelper.swipeBackward();
  }

  @Override public void onBackPressed() {
    // 正在滑动返回的时候取消返回按钮事件
    if (mSwipeBackHelper.isSliding()) {
      return;
    }
    mSwipeBackHelper.backward();
  }
}
LouisMari1991 commented 6 years ago

已经在1.5.1 版本修复