当前位置: 移动技术网 > IT编程>移动开发>Android > Android中Toolbar随着ScrollView滑动透明度渐变效果实现

Android中Toolbar随着ScrollView滑动透明度渐变效果实现

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

桂林山水甲天下,堆糖画报,景德镇同城游戏大厅官方下载

android中toolbar随着scrollview滑动透明度渐变效果实现

一.思路:监听scrollview的滑动事件 不断的修改toolbar的透明度

二.注意

1.scrollview 6.0以前没有scrollview.setonscrollchangelistener(l)方法  所以要自定义scrollview 在onscrollchanged()中监听

2.scrollview 6.0(23)以前没有scrollview.setonscrollchangelistener()方法  所以要自定义scrollview 实现.为了toolbar不遮盖scrollview我们给scrollview设置paddingtop

   但是scrollview 设置paddinttop以后 toolbar透明度变为0以后还占据空间 会出现空白,解决方法:

 为scrollview设置两个属性:

 1〉.

android:cliptopadding="false" 

表示控件的绘制范围是否不在padding里面  false就是表示空间的绘制可以绘制到padding中

 2〉

android:clipchildren="false" 

表示子控件是否不能超出padding区域(比如: false :scrollview上滑的时候 child 可以滑出padding区域 ;true:scrollview上滑的时候 child 不能可以滑出padding区域 )

布局文件如下:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 <com.dice.md.toolbar.transperent.translucentscrollview 
  android:id="@+id/scrollview" 
  android:cliptopadding="false" 
  android:clipchildren="true" 
  android:paddingtop="?attr/actionbarsize" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
  <linearlayout 
   android:orientation="vertical" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <textview 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
  </linearlayout> 
 </com.dice.md.toolbar.transperent.translucentscrollview> 
 <android.support.v7.widget.toolbar 
  android:id="@+id/toolbar" 
  android:layout_width="match_parent" 
  android:background="?attr/colorprimary" 
  android:layout_height="?attr/actionbarsize" > 
 </android.support.v7.widget.toolbar> 
</relativelayout> 

三.步骤

1. 创建回调接口:

public interface translucentlistener { 
/** 
 * 透明度的回调 
 * @param alpha 
 */ 
public void ontranslucent(float alpha); 
} 

2.自定义scrollview 在onscrollchange方法中回调translucentlistener接口的方法 并且回传alpha的值:

@override 
protected void onscrollchanged(int l, int t, int oldl, int oldt) { 
 super.onscrollchanged(l, t, oldl, oldt); 
 if (translucentlistener!=null) { 
  //translucentlistener.ontranslucent(alpha); 
 } 
} 

3.alpha的值得计算:

// alpha = 滑出去的高度/(screenheight/3); 
float heightpixels = getcontext().getresources().getdisplaymetrics().heightpixels; 
float scrolly = getscrolly();//该值 大于0 
float alpha = 1-scrolly/(heightpixels/3);// 0~1 透明度是1~0 
//这里选择的screenheight的1/3 是alpha改变的速率 (根据你的需要你可以自己定义)

最后mainactivity中

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

以上所述是小编给大家介绍的android中toolbar随着scrollview滑动透明度渐变效果实现,小编会及时回复大家的。在此也非常感谢大家对移动技术网网站的支持!

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

相关文章:

验证码:
移动技术网