在线直播系统源码,android沉浸式页面实现

在线直播系统源码,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);
    }
}

 

以上就是在线直播系统源码,android沉浸式页面实现, 更多内容欢迎关注之后的文章

 

posted @ 2022-08-25 14:15  云豹科技-苏凌霄  阅读(92)  评论(0)    收藏  举报