沉浸式状态栏之前我写过一篇http://blog.csdn.net/qq_24675479/article/details/78557698,我就不说封装了
首先我们自定义一个ScrollView,监听返回onScrollChanged方法
public class MyScrollView extends ScrollView{
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(mListener!=null){
mListener.onScroll(l,t,oldl,oldt);
}
}
public interface ScrollChangeListener{
public void onScroll(int scrollX, int scrollY, int oldScrollX, int oldScrollY);
}
private ScrollChangeListener mListener;
public void setOnScrollChangeListener(ScrollChangeListener listener){
mListener = listener;
}
}
然后是布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.hbwj.a12_.MyScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="400dp" />
<TextView
android:layout_width="match_parent"
android:text="text"
android:layout_height="400dp" />
<TextView
android:layout_width="match_parent"
android:text="text"
android:layout_height="400dp" />
<TextView
android:layout_width="match_parent"
android:text="text"
android:layout_height="400dp" />
<TextView
android:layout_width="match_parent"
android:text="text"
android:layout_height="400dp" />
</LinearLayout>
</com.hbwj.a12_.MyScrollView>
<LinearLayout
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:gravity="center"
android:background="@android:color/holo_blue_light"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:text="头部"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
主要代码
StatusBarUtils.setImage(this);
mScrollView = (MyScrollView) findViewById(R.id.scroll_view);
mTitleBar = (LinearLayout) findViewById(R.id.title_bar);
mImageView = (ImageView) findViewById(R.id.image_view);
mTitleBar.getBackground().setAlpha(0);
mImageView.post(new Runnable() {
@Override
public void run() {
mImageViewHeight = mImageView.getMeasuredHeight();
mTitleBarHeight=mTitleBar.getMeasuredHeight();
}
});
//获取图片的高度
mScrollView.setOnScrollChangeListener(new MyScrollView.ScrollChangeListener() {
@Override
public void onScroll(int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
// 获取图片的高度,根据当前滚动的位置,计算alpha 值
if (mImageViewHeight == 0) return;
float alpha = (float) scrollY / (mImageViewHeight-mTitleBarHeight);//0->1
if (alpha < 0) {
alpha = 0;
}
if (alpha > 1) {
alpha = 1;
}
mTitleBar.getBackground().setAlpha((int) (alpha*255));
}
});
}