仿qq——沉浸式状态栏效果

这里写图片描述

沉浸式状态栏之前我写过一篇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));
                }

        });

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值