大家都见过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.布局文件(随便写了一个),相信大家都会
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<com.projectdemo.zwz.slidingmenu.MyScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="hollod word"/>
<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="hollod word"/>
<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="hollod word"/>
<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="hollod word"/>
<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="hollod word"/>
</LinearLayout>
</com.projectdemo.zwz.slidingmenu.MyScrollView>
<LinearLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center"
android:background="@android:color/holo_blue_light">
<TextView
android:text="头部"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
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));
}
});
网友评论