android沉浸式页面实现

1.依赖

dependencies {
		//沉浸式状态栏
		// After AndroidX
		implementation ('com.github.niorgai:StatusBarCompat:2.3.3', {
    		exclude group: 'androidx.appcompat:appcompat'
    		exclude group: 'com.google.android.material:material'
		})
    	//Google AutoValue
    	compileOnly 'com.google.auto.value:auto-value:1.5.2'
    	annotationProcessor "com.google.auto.value:auto-value:1.5.2"
    }

2.页面布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/srl_index"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_index"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb_index"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#66319bd2"
        app:contentInsetStart="0dp"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_behavior="com.gentek.police.main.index.TranslucentBehavior"
        >


        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center|start"
            android:paddingLeft="15dp"
            android:text="打卡签到"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

    </androidx.appcompat.widget.Toolbar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

3.配置滑动变化的颜色

需要注意的是Toolbar里的属性app:layout_behavior="com.gentek.police.main.index.TranslucentBehavior",其
TranslucentBehavior类是自己定义的

/**
 * Created by kly on 2019/11/30.
 */
@AutoValue
public abstract class RgbValue {

    public abstract int red();

    public abstract int green();

    public abstract int blue();

	// * 注意,在new AutoValue_RgbValue(red,green,blue)之前,需要先Rebuild Project
    public static RgbValue create(int red,int green,int blue){
        return new AutoValue_RgbValue(red,green,blue);
    }
}

/**
 * Created by kly on 2019/11/30.
 */
@SuppressWarnings("unused")
public class TranslucentBehavior extends CoordinatorLayout.Behavior<Toolbar> {

    //顶部距离
    private int mDistanceY=0;
    //颜色变化速度
    private static final int SHOW_SPEED=3;
    //定义变化的颜色
    private final RgbValue RGB_VALUE=RgbValue.create(255,124,2);

    public TranslucentBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) {
        return dependency.getId()== R.id.rv_index;
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull Toolbar child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        return true;
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull Toolbar child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        //增加滑动距离
        mDistanceY+=dy;
        //toolbar的高度
        final int targetHeight=child.getBottom();

        //当滑动时,并距离小于toolbar高度的时候,调整渐变色
        if(mDistanceY > 0 && mDistanceY <= targetHeight){
            final float scale=(float) mDistanceY / targetHeight;
            final float alpha=scale * 255;
            child.setBackgroundColor(Color.argb((int) alpha,RGB_VALUE.red(),RGB_VALUE.green(),RGB_VALUE.blue()));
        }else if(mDistanceY > targetHeight){
            child.setBackgroundColor(Color.rgb(RGB_VALUE.red(),RGB_VALUE.green(),RGB_VALUE.blue()));
        }
    }

}

4.初始化沉浸式状态栏



public class ExampleActivity extends Activity  {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        StatusBarCompat.translucentStatusBar(this,true);
    }
}

5.第三方依赖框架github地址

StatusBarCompat 沉浸式状态栏框架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值