当前位置: 移动技术网 > IT编程>移动开发>Android > Android ScrollView滑动实现仿QQ空间标题栏渐变

Android ScrollView滑动实现仿QQ空间标题栏渐变

2019年07月24日  | 移动技术网IT编程  | 我要评论

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

 

好了我们切入主题。

有可能你不知道的那些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
     *
#scrollby(int, int)} or #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想要的我们都可以实现。
源码地址:https://github.com/hankkin/gradationtitlebar
项目里面我还添加了一个带banner的,原理是一样的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网