Android带你解析ScrollView--仿QQ空间标题栏渐变

简介: 版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/52107851 绪论今天来研究的是ScrollView-滚动视图,滚动视图又分横向滚动视图(HorizontalScrollView)和纵向滚动视图(ScrollView),今天主要研究纵向的。
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/52107851

绪论

今天来研究的是ScrollView-滚动视图,滚动视图又分横向滚动视图(HorizontalScrollView)和纵向滚动视图(ScrollView),今天主要研究纵向的。相信大家在开发中经常用到,ScrollView的功能已经很强大了,但是仍然满足不了我们脑洞大开的UI设计师们,所以我们要自定义…本篇文章主要讲监听ScrollView的滑动实现仿QQ空间标题栏渐变,先看一下效果图:

Gif
好了我们切入主题。

有可能你不知道的那些ScrollView属性

  • android:scrollbars
    设置滚动条显示。none(隐藏),horizontal(水平),vertical(垂直)
  • android:scrollbarStyle
    设置滚动条的风格和位置。设置值:insideOverlay、insideInset、outsideOverlay、outsideInset
  • android:scrollbarThumbHorizontal
    设置水平滚动条的drawable。
  • android:soundEffectsEnabled
    设置点击或触摸时是否有声音效果
  • android:fadingEdge
    设置拉滚动条时,边框渐变的放向。none(边框颜色不变),horizontal(水平方向颜色变淡),vertical(垂直方向颜色变淡)。参照fadingEdgeLength的效果图 android:fadingEdgeLength 设置边框渐变的长度
  • android:scrollX
    以像素为单位设置水平方向滚动的的偏移值,在GridView中可看的这个效果
  • android:scrollY
    以像素为单位设置垂直方向滚动的的偏移值
  • android:scrollbarAlwaysDrawHorizontalTrack
    设置是否始终显示垂直滚动条
  • android:scrollbarDefaultDelayBeforeFade
    设置N毫秒后开始淡化,以毫秒为单位。

以上这些属性有兴趣的可以去研究一下,这里就不详细讲了。很多属性并不常用,下面说说我们经常用的,怎样监听ScrollView的滑动并实现标题栏的渐变?

ScrollView滑动监听:

Google并没有给我们提供ScrollView的滑动距离、是否滑动到布局底部、顶部的方法,但是提供了一个onScrollChanged方法:

@Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        //todo:
        }
    }

通过查看源码注释,

/**
     * This is called in response to an internal scroll in this view (i.e., the
     * view scrolled its own contents). This is typically as a result of
     * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
     * called.
     *
     * @param l Current horizontal scroll origin.
     * @param t Current vertical scroll origin.
     * @param oldl Previous horizontal scroll origin.
     * @param oldt Previous vertical scroll origin.
     */

我们可以知道这个方法的参数分别为:
l:当前横向滑动距离
t:当前纵向滑动距离
oldl:之前横向滑动距离
oldt:之前纵向滑动距离

但是这个方法我们不可以调用,我们可以重写接口或者重写ScrollView暴露该方法:

package com.hankkin.gradationscroll;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
 * 带滚动监听的scrollview
 *
 */
public class GradationScrollView extends ScrollView {

    public interface ScrollViewListener {

        void onScrollChanged(GradationScrollView scrollView, int x, int y,
                             int oldx, int oldy);

    }

    private ScrollViewListener scrollViewListener = null;

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

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

    public GradationScrollView(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) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

}

设置标题渐变

滚动监听暴露出来我们就该去设置标题栏随着ScrollView的滑动来改变标题栏的透明度实现渐变:
我们先看一下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hankkin.gradationtitlebar.QQSpeakActivity">

    <com.hankkin.gradationscroll.GradationScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/iv_banner"
                android:scaleType="fitXY"
                android:src="@drawable/banner3"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
            <com.hankkin.gradationscroll.NoScrollListview
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.hankkin.gradationscroll.NoScrollListview>
        </LinearLayout>
    </com.hankkin.gradationscroll.GradationScrollView>
    <TextView
        android:paddingBottom="10dp"
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:gravity="center|bottom"
        android:text="我是标题"
        android:textSize="18sp"
        android:textColor="@color/transparent"
        android:background="#00000000" />
</RelativeLayout>

最外层是我们自定义的ScrollView,包裹着一张背景图片和一个ListView(ListView重写为不可以滑动),然后布局的上面有一个TextView当做标题栏,你也可以用布局。
这里写图片描述

然后我们需要获取图片的高度,并且设置滚动监听,随着滚动的距离来设置标题栏的颜色透明度和字体颜色的透明度

/**
     * 获取顶部图片高度后,设置滚动监听
     */
    private void initListeners() {

        ViewTreeObserver vto = ivBanner.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                textView.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                height = ivBanner.getHeight();

                scrollView.setScrollViewListener(QQSpeakActivity.this);
            }
        });
    }
 /**
     * 滑动监听
     * @param scrollView
     * @param x
     * @param y
     * @param oldx
     * @param oldy
     */
    @Override
    public void onScrollChanged(GradationScrollView scrollView, int x, int y,
                                int oldx, int oldy) {
        // TODO Auto-generated method stub
        if (y <= 0) {   //设置标题的背景颜色
            textView.setBackgroundColor(Color.argb((int) 0, 144,151,166));
        } else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / height;
            float alpha = (255 * scale);
            textView.setTextColor(Color.argb((int) alpha, 255,255,255));
            textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166));
        } else {    //滑动到banner下面设置普通颜色
            textView.setBackgroundColor(Color.argb((int) 255, 144,151,166));
        }
    }

OK,这就实现了你在最上方看到的效果了。
其实并不难,只是我们没有亲自动手去实现,相信多动手自己亲自去实现一下,UI想要的我们都可以实现。
源码地址:欢迎Star,fork,有问题多多指正。
https://github.com/Hankkin/GradationTitleBar
项目里面我还添加了一个带banner的,原理是一样的。

相关文章
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
3月前
|
算法 C++ 容器
C++ STL:空间配置器源码解析
C++ STL:空间配置器源码解析
|
3月前
|
存储 算法 安全
AVB数据解析:Android verified boot 2.0 vbmeta 数据结构解析
AVB数据解析:Android verified boot 2.0 vbmeta 数据结构解析
143 0
|
1月前
|
编译器 开发工具 Android开发
Android 12 新特性深度解析
【2月更文挑战第15天】 随着移动操作系统的不断进化,Android 12带来了一系列创新功能与性能提升。本文将深入剖析Android 12的核心新特性,包括隐私仪表盘、通知管理、设备控制以及性能优化等方面,为开发者和用户提供全面的更新指南。
|
2月前
|
存储 弹性计算 固态存储
阿里云服务器租用费用1t空间多少钱?全面解析
阿里云服务器租用费用1t空间多少钱?1T空间如果是系统盘SSD云盘价格是3686元一年、ESSD云盘1t空间是5222元一年,ESSD Entry云盘1024G存储空间价格是2580元一年。阿里云百科整理几款不同的云盘1t空间价格
|
3月前
|
JSON Java Android开发
Android网络和数据交互: 请解释Android中的JSON解析库,如Gson。
Android网络和数据交互: 请解释Android中的JSON解析库,如Gson。
24 0
|
4月前
|
Android开发 容器
[Android]View的事件分发机制(源码解析)
[Android]View的事件分发机制(源码解析)
36 0
|
4月前
|
SQL 数据库 数据安全/隐私保护
Android Studio App开发中数据库SQLite的解析及实战使用(包括创建数据库,增删改查,记住密码等 附源码必看)
Android Studio App开发中数据库SQLite的解析及实战使用(包括创建数据库,增删改查,记住密码等 附源码必看)
60 0
|
1天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
9天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
19 0

推荐镜像

更多