欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)

程序员文章站 2022-04-26 14:24:44
scrollview标题栏滑动渐变仿京东样式(上滑显示下滑渐变消失)/** * @classname myscrollview * @author rex * @date 2021/1/27 17:3...

scrollview标题栏滑动渐变

仿京东样式(上滑显示下滑渐变消失)

Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)

Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)

/**
 * @classname myscrollview
 * @author rex
 * @date 2021/1/27 17:38
 */
public class myscrollview extends scrollview {
 private translucentlistener mtranslucentlistener;

 public void settranslucentlistener(translucentlistener translucentlistener) {
  this.mtranslucentlistener = translucentlistener;
 }
 public myscrollview(context context) {
  this(context, null);
 }

 public myscrollview(context context, attributeset attrs) {
  this(context, attrs, 0);
 }

 public myscrollview(context context, attributeset attrs, int defstyleattr) {
  this(context, attrs, defstyleattr, 0);
 }

 public myscrollview(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
 }

 @override
 protected void onscrollchanged(int l, int t, int oldl, int oldt) {
  super.onscrollchanged(l, t, oldl, oldt);
  if (mtranslucentlistener != null) {
   //scrollview滑出高度
   int scrolly = getscrolly();
   //屏幕高度
   int screenheight = getcontext().getresources().getdisplaymetrics().heightpixels;
   //有效滑动距离为屏幕2分之一
   // alpha = 滑动高度/(screenheight/3f)
   if (scrolly <= screenheight / 2f) {
    log.d(">>>>>>>>>", "scrollview划出高度:" + scrolly);
    log.d(">>>>>>>>>", "屏幕高度:" + screenheight);
    log.d(">>>>>>>>>", "渐变值:" + (0 + scrolly / (screenheight / 4f)));
    // 渐变的过程 1~0
    mtranslucentlistener.ontranslucent(0 + scrolly / (screenheight /4f));
   }
  }
 }
}

activity 设置

public class toolbaractivity extends appcompatactivity implements translucentlistener {
 private toolbar mtoolbar;
 private myscrollview mscrollview;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_toobar);
  mtoolbar = findviewbyid(r.id.id_toolbar);
  mscrollview = findviewbyid(r.id.id_scrollview);
  //初始化渐变为0
  mtoolbar.setalpha(0);
  //设置渐变回调
  mscrollview.settranslucentlistener(this);
 }

 @override
 public void ontranslucent(float alpha) {
  mtoolbar.setalpha(alpha);
 }
}

渐变回调接口

/**
 * @classname translucentlistener
 * @author rex
 * @date 2021/1/27 17:38
 */
public interface translucentlistener {
 /**
  * 透明度的回调监听
  *
  * @param alpha 0~1 透明度
  */
 public void ontranslucent(float alpha);
}

布局文件

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

 <com.rex.rxhttpdemo.myscrollview
  android:id="@+id/id_scrollview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clipchildren="false"
  android:cliptopadding="false"
  >

  <linearlayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

   <button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button0" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button1" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button2" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button3" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button4" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button5" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />

   <button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="button" />
  </linearlayout>
 </com.rex.rxhttpdemo.myscrollview>

 <androidx.appcompat.widget.toolbar
  android:id="@+id/id_toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/coloraccent"
  app:title="title"
  />

</relativelayout>

下滑显示上滑渐变消失

/**
 * @classname myscrollview
 * @author rex
 * @date 2021/1/27 17:38
 */
public class myscrollview extends scrollview {
 private translucentlistener mtranslucentlistener;

 public void settranslucentlistener(translucentlistener translucentlistener) {
  this.mtranslucentlistener = translucentlistener;
 }
 public myscrollview(context context) {
  this(context, null);
 }

 public myscrollview(context context, attributeset attrs) {
  this(context, attrs, 0);
 }

 public myscrollview(context context, attributeset attrs, int defstyleattr) {
  this(context, attrs, defstyleattr, 0);
 }

 public myscrollview(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
 }

 @override
 protected void onscrollchanged(int l, int t, int oldl, int oldt) {
  super.onscrollchanged(l, t, oldl, oldt);
  if (mtranslucentlistener != null) {
   //scrollview滑出高度
   int scrolly = getscrolly();
   //屏幕高度
   int screenheight = getcontext().getresources().getdisplaymetrics().heightpixels;
   //有效滑动距离为屏幕2分之一
   // alpha = 滑动高度/(screenheight/3f)
   if (scrolly <= screenheight / 2f) {
    log.d(">>>>>>>>>", "scrollview划出高度:" + scrolly);
    log.d(">>>>>>>>>", "屏幕高度:" + screenheight);
    log.d(">>>>>>>>>", "渐变值:" + (1 - scrolly / (screenheight / 4f)));
    // 渐变的过程 1~0
    mtranslucentlistener.ontranslucent(1 - scrolly / (screenheight /4f));
   }
  }
 }
}

注意: 这里只是更改了 mtranslucentlistener.ontranslucent 里的 渐变值

activty 里 把初始化 mtoolbar.setalpha(0); 去掉

xml

 <com.rex.rxhttpdemo.myscrollview
  android:id="@+id/id_scrollview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clipchildren="false"
  android:cliptopadding="false"
  android:paddingtop="?attr/actionbarsize"
  >
 </com.rex.rxhttpdemo.myscrollview>

xml 加入 paddingtop .

注意:
android:clipchildren=“false”
android:cliptopadding="false"

这俩个属性 如果不加会有留白

到此这篇关于android 滑动scrollview标题栏渐变效果(仿京东toolbar)的文章就介绍到这了,更多相关android 滑动scrollview标题栏渐变内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!