ScrollView+渐变头布局

首先自定义ScrollView    代码如下:

public class ListenerScrollView extends NestedScrollView {
    private ScrollViewListener scrollViewListener = null;

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

    public ListenerScrollView(Context context, AttributeSet attrs,
                              int defStyle) {
        super(context, attrs, defStyle);
    }

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

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
        super.onScrollChanged(x, y, oldx, oldy);
    }

    public interface ScrollViewListener {
        void onScrollChanged(ListenerScrollView scrollView, int x, int y, int oldx, int oldy);
    }
}

核心代码为监听滑动状态:

        @Override
            public void onScrollChanged(ListenerScrollView scrollView, int x, int y, int oldx, int oldy) {
                float scale = (float) y / (float) (DensityUtil.dp2px(DetailsActivity.this, 170f) - detailsTitle.getHeight());
                int newAlpha = (int) (scale * 255.00f);
                if (newAlpha <= 0) {
                    newAlpha = 0;
                } else if (newAlpha > 0 && newAlpha <= 127) {
                    titAlpha = (int) (255 - 255 * scale * 2);
                    detailsTitle.setBackgroundColor(Color.argb(newAlpha, 255, 255, 255));
                    if (changeBg2) {
//                        ivBack.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.btn_exhibit_back_selector2, null));
//                        ivTitleRight.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.exhibit_menu_selector, null));
                        changeBg2 = false;
                        changeBg = true;
                    }
                } else if (newAlpha > 128 && newAlpha <= 255) {
                    titAlpha = (int) ((scale - 0.5) * 2 * 255.00f);
                    detailsTitle.setBackgroundColor(Color.argb(newAlpha, 255, 255, 255));
                    if (changeBg) {
//                        ivBack.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.btn_exhibit_back_selector, null));
//                        ivTitleRight.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.exhibit_menu_selector2, null));
                        changeBg = false;
                        changeBg2 = true;
                    }
                } else if (newAlpha > 255) {
                    newAlpha = 255;
                }
                detailsTitle.setBackgroundColor(Color.argb(newAlpha, 255, 255, 255));

            }
        });
        ScaleAnimation animation = new ScaleAnimation(1.4f, 1.0f, 1.4f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(200);

工具类:

/**
 * 将dp类型的尺寸转换成px类型的尺寸
 */
public static int dp2px(Context context, float dipValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我可以提供一些思路和参考代码。 首先,实现 Android 中的自定义吸顶,可以采用两种方式: 1. 使用固定顶部控件 + ScrollView + ViewPager 的方式实现。 这种方式比较常见,具体实现思路如下: (1)在布局文件中定义一个固定在顶部的控件,例如 LinearLayout,将其设置为可见性为 gone,即不可见。 (2)在 ScrollView 中添加 ViewPager,将其填充满整个布局,用于滑动切换不同的子页面。 (3)监听 ScrollView 的滑动事件,在滑动到一定位置时,将顶部控件设置为可见,实现吸顶效果。 具体实现代码可以参考以下链接: - https://www.jianshu.com/p/4f28a4d0c3b1 - https://www.cnblogs.com/xiaohuafice/p/11050662.html 2. 使用 CoordinatorLayout + AppBarLayout 实现。 这种方式相对来说比较简单,具体实现思路如下: (1)在布局文件中使用 CoordinatorLayout 作为根布局,并添加一个 AppBarLayout 作为子布局。 (2)在 AppBarLayout 中添加一个 Toolbar 控件作为顶部的固定控件,将其设置为可见性为 gone。 (3)在子页面中,使用 NestedScrollView 作为滑动的容器,并将其放在 AppBarLayout 的下面。 (4)监听 NestedScrollView 的滑动事件,在滑动到一定位置时,将 Toolbar 设置为可见,实现吸顶效果。 具体实现代码可以参考以下链接: - https://www.jianshu.com/p/5d0f7e7e7c97 - https://www.jianshu.com/p/5d0f7e7e7c97 希望以上内容能够帮助到你,有什么问题可以再和我交流哦!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值