ScrollView的回弹效果和滚动时toolBar的透明度变化

回弹:http://blog.csdn.net/aaawqqq/article/details/37740463



toolbar随着ScrollView滚动的透明度变化效果


自定义ScrollView如下:

public class TranslucentScrollView extends ScrollView{
    private OnScrollChangedListener mOnScrollChangedListener;

    public TranslucentScrollView(Context context) {
        super(context);
    }

    public TranslucentScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
    }
}
布局文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.example.scrollview.TranslucentScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <TextView
                android:id="@+id/iv_head"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dimen_300"
                android:background="#ba0085"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1200dp"
                android:background="#ff0000"
                />
        </LinearLayout>
    </com.example.scrollview.TranslucentScrollView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="#0019be"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize">

    </android.support.v7.widget.Toolbar>

</RelativeLayout>

activity中

public class ScrollViewActivity extends AppCompatActivity implements TranslucentScrollView.OnScrollChangedListener {
    private TranslucentScrollView scrollView;
    private Toolbar toolbar;

    private float headerHeight;//顶部高度
    private float minHeaderHeight;//顶部最低高度,即Bar的高度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrollview_layout);

        initView();
    }

    private void initView() {
        scrollView = (TranslucentScrollView) findViewById(R.id.scrollview);
        scrollView.setOnScrollChangedListener(this);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setAlpha(0);
        initMeasure();
    }

    private void initMeasure() {
        headerHeight = getResources().getDimension(R.dimen.dimen_300);
        minHeaderHeight = getResources().getDimension(R.dimen.abc_action_bar_default_height_material);

    }

    @Override
    public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
        //Y轴偏移量
        float scrollY = who.getScrollY();

        //变化率
        float headerBarOffsetY = headerHeight - minHeaderHeight;//Toolbar与header高度的差值
        float offset = 1 - Math.max((headerBarOffsetY - scrollY) / headerBarOffsetY, 0f);

<span style="white-space:pre">	</span>toolbar.setAlpha(offset);//toolbar整体透明




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值