当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发实现标题随scrollview滑动变色的方法详解

Android开发实现标题随scrollview滑动变色的方法详解

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

北京肾病医院有哪些,乔木不可休,nuckily

本文实例讲述了android开发实现标题随scrollview滑动变色的方法。分享给大家供大家参考,具体如下:

要实现某个view的背景透明度跟随scrollview滑动而改变需要重新scrollview的onoverscrolled方法,该方法随着滑动变化(包括手指滑动、手指移开惯性滑动)而响应,所以最适合做变色处理。

step1:设定布局

由于我们要实现的是滑动时标题的背景透明度改变,固定顶部的标题view不能在srcollview里面跟随滑动,所以需要这样布局:

<framelayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.****.scrollchangescrollview
      android:id="@+id/scrollview"
      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">
          <textview
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablepadding="5dp"
            android:drawabletop="@drawable/dicovery_vintner_icon_wine"
            android:gravity="center"
            android:text="葡萄酒"
            android:textcolor="@color/hometitlebg" />
      </linearlayout>
    </com.***.scrollchangescrollview>
    <button
      android:id="@+id/btn_back"
      android:layout_width="match_parent"
      android:layout_height="35dp"
      android:layout_centervertical="true"
      android:background="@null"
      android:drawableleft="@drawable/icon_back"
      android:padding="10dp" />
</framelayout>

step2:添加需要用到的方法

滑动时,某个view要变色,重新scrollview后,添加方法让其知道该view需要变色

private view mtitleview;
/**
* 变色标题view
* @param view
*/
public void setuptitleview (view view) {
    this.mtitleview = view;
}

滑动时变色需要参考scrollview里面的某个子view的滑动高度,如果该子view上划完全划出屏幕,则标题view背景透明为0:

private view mbywhichview;
/**
* 跟随的view
* @param view
*/
public void setupbywhichview(view view) {
    mbywhichview = view;
}

再添加一个设置,如果不要背景透明度渐变:

private boolean shouldslowlychange;
public void setshouldslowlychange(boolean slowlychange) {
    this.shouldslowlychange = slowlychange;
}

step3:代码实现

**
 * 滑动时标题变色view
 * created by george.yang on 16/2/21.
 */
public class scrollchangescrollview extends scrollview {
  private view mbywhichview;
  private view mtitleview;
  private boolean shouldslowlychange = true;
  public scrollchangescrollview(context context) {
    super(context);
  }
  public scrollchangescrollview(context context, attributeset attrs) {
    super(context, attrs);
  }
  public scrollchangescrollview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
  }
  @targetapi(build.version_codes.lollipop)
  public scrollchangescrollview(context context, attributeset attrs, int defstyleattr, int defstyleres) {
    super(context, attrs, defstyleattr, defstyleres);
  }
  @override
  public void scrollto(int x, int y) {
    //这是为了修复noscrlllistview嵌套在srcollview时就自动滑动到noscrolllistview的顶部的bug,不影响使用
    if (x == 0 && y == 0 || y <= 0) {
      super.scrollto(x, y);
    }
  }
  public void setlistener(onscrolllistener listener){
    this.mlistener = listener;
  }
  public void setshouldslowlychange(boolean slowlychange) {
    this.shouldslowlychange = slowlychange;
  }
  /**
   * 设置透明度渐变的标题view
   * @param view
   */
  public void setuptitleview (view view) {
    this.mtitleview = view;
  }
  /**
   * 跟随的view
   * @param view
   */
  public void setupbywhichview(view view) {
    mbywhichview = view;
  }
  @override
  protected void onoverscrolled(int scrollx, int scrolly, boolean clampedx,
                 boolean clampedy) {
    super.onoverscrolled(scrollx, scrolly, clampedx, clampedy);
    if (scrolly >= mbywhichview.gettop() + mbywhichview.getmeasuredheight()) {
      mtitleview.setbackgroundcolor(color.black);
    } else if (scrolly>=0) {
      if (!shouldslowlychange) {
        mtitleview.setbackgroundcolor(color.transparent);
      } else {
        float persent = scrolly * 1f / (mbywhichview.gettop() + mbywhichview.getmeasuredheight());
        int alpha = (int) (255 * persent);
        int color = color.argb(alpha,0,0,0);
        mtitleview.setbackgroundcolor(color);
      }
    }
    if (mlistener!=null) {
      mlistener.onscroll(scrollx, scrolly);
    }
  }
}

效果如下:

滑动前

滑动变色中

参考的view超出屏幕后

更多关于android相关内容感兴趣的读者可查看本站专题:《android基本组件用法总结》、《android开发入门与进阶教程》、《android布局layout技巧总结》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网