01、概述:
在开发过程中,这样的需求也是常见的,随着ScrollView 的滑动,标题栏和状态栏背景透明度发生变化。有多中实现方式,在这里记录一下自己用到的一种。
02、效果:
03、代码实现:
public class ZpScrollViewActivity extends Activity{
private View titleLine;
private View titleLine1;
private LinearLayout title;
private LinearLayout top;
private MyScrollView scrollView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview);
initView();
}
private void initView() {
setTranslucentStatus(this, true);
title = (LinearLayout) findViewById(R.id.ll_title);
title.getBackground().mutate().setAlpha(0);
top = (LinearLayout) findViewById(R.id.ll_title_top);
titleLine1 = findViewById(R.id.v_title_line_1);
titleLine = findViewById(R.id.v_title_line);
scrollView = (MyScrollView) findViewById(R.id.sv_content);
// 设置状态栏高度
int statusBarHeight = this.getResources().getDimensionPixelSize(this.getResources().getIdentifier("status_bar_height", "dimen", "android"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
titleLine.setLayoutParams(params);
titleLine1.setLayoutParams(params);
// 设置滑动
scrollView.setOnScrollistener(new MyScrollView.OnScrollistener() {
@Override
public void onScroll(int startY, int endY) {
//根据scrollview滑动更改标题栏透明度
changeAphla(startY, endY);
}
});
}
/**
* 根据内容窗体的移动改变标题栏背景透明度
*
* @param startY scrollview开始滑动的y坐标(相对值