大家都见过QQ好友动态,不知道你注意到没有,当我们滑动时状态栏会变颜色。今天就自己实现以下这种效果。
1.设置全屏
public static void setStatusBarTranslucent(Activity activity){
//5.0以上
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
//设置全屏
View decorView= activity.getWindow().getDecorView();
int option= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView.setSystemUiVisibility(option);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
//设置全屏
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
2.布局文件(随便写了一个),相信大家都会
3.自定义ScrollView,主要设置监听,不停返回坐标位置
只需要重写onScrollChanged(int l, int t, int oldl, int oldt) ;方法
设置监听回调
public interface ScrollChangedListener{
public void onScroll(int l, int t, int oldl, int oldt);
}
private ScrollChangedListener mListener;
public void setOnScrollChangeListener(ScrollChangedListener listener){
mListener=listener;
}
onScrollChanged(int l, int t, int oldl, int oldt) 中实现回调
@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);
}
}
其实就是如此简单
4.主要实现
.刚进来背景完全透明
llTitle = (LinearLayout) findViewById(R.id.ll_title);
llTitle.getBackground().setAlpha(0);
获取ImageView和llTitle高度
image_view.post(new Runnable() {
@Override
public void run() {
mImageHeight=image_view.getMeasuredHeight();
}
});
llTitle.post(new Runnable() {
@Override
public void run() {
mLLtitleHeight= llTitle.getMeasuredHeight();
}
});
不断监听滚动 判断当前滚动的位置计算背景透明度
scroll_view.setOnScrollChangeListener(new MyScrollView.ScrollChangedListener() {
@Override
public void onScroll(int l, int t, int oldl, int oldt) {
//获取图片的高度,根据当前滚动的位置 计算alpha
if (mImageHeight==0)return;
if (mLLtitleHeight==0)return;
float alpha=(float)t/(mImageHeight-mLLtitleHeight);
if (alpha<=0){
alpha=0;
}
if (alpha>1){
alpha=1;
}
llTitle.getBackground().setAlpha((int) (alpha*255));
}
});