Android ScrollView滑动标题栏变色

ScrollView渐变背景效果

就不上效果图了,现在各种app都有这种效果,随着ScrollView的滑动,以某个View为参照,标题栏背景渐渐由  透明  ->  指定颜色

很多实现方法,以下为重写ScrollView。

布局:

<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">

 <AlphaTitleScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <!-各种内容->
            </LinearLayout>

 </AlphaTitleScrollView>

  <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="标题"/>
</RelativeLayout>

重写的ScrollView:

public class AlphaTitleScrollView extends ScrollView {

    private View mReferenceView;
    private View mTitleView;
    private int backgroundColor;
    private int[] backgroundColorRGB;
    private boolean isSlowlyChange = true;

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

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

    public AlphaTitleScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AlphaTitleScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    public void scrollTo(int x, int y) {
        if (x == 0 && y == 0 || y <= 0) {
            super.scrollTo(x, y);
        }
    }

    public void setSlowlyChange(boolean slowlyChange) {
        this.isSlowlyChange = slowlyChange;
    }

    /**
     * 初始化设置参数
     *
     * @param titleView
     * @param referenceView
     * @param backgroundColor
     * @param backgroundColorRGB
     */
    public void initAlphaTitle(View titleView, View referenceView, int backgroundColor, int[] backgroundColorRGB) {
        this.mTitleView = titleView;
        this.mReferenceView = referenceView;
        this.backgroundColor = backgroundColor;
        this.backgroundColorRGB = backgroundColorRGB;
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);

        if (scrollY >= mReferenceView.getTop() + mReferenceView.getMeasuredHeight()) {
            mTitleView.setBackgroundColor(backgroundColor);
        } else if (scrollY >= 0) {
            if (!isSlowlyChange) {
                mTitleView.setBackgroundColor(Color.TRANSPARENT);
            } else {
                float persent = scrollY * 1f / (mReferenceView.getTop() + mReferenceView.getMeasuredHeight());
                int alpha = (int) (255 * persent);
                int color = Color.argb(alpha, backgroundColorRGB[0], backgroundColorRGB[1], backgroundColorRGB[2]);
                mTitleView.setBackgroundColor(color);
            }
        }

    }

调用:

 mAlphaScrollView.initAlphaTitle(titleView, headView, getResources().getColor(R.color.yellow), new int[]{253, 215, 62});

 

转载于:https://my.oschina.net/yaly/blog/805346

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值