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 沉浸式狀態欄框架

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