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

来源:互联网 发布:死神结局是什么 知乎 编辑:程序博客网 时间:2024/06/05 00:45

回弹: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 0
原创粉丝点击