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

 更新时间:2017年01月22日 10:53:36   作者:Luyifei666  
这篇文章主要介绍了Android中Toolbar随着ScrollView滑动透明度渐变效果实现,非常不错,具有参考借鉴价值,需要的的朋友参考下

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滑动透明度渐变效果实现,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Android开发解决字符对齐问题方法

    Android开发解决字符对齐问题方法

    这篇文章主要为大家介绍了Android开发解决字符对齐问题方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android超详细讲解弹出多选框的实现

    Android超详细讲解弹出多选框的实现

    这篇文章主要介绍了在Android开发中如何实现弹出多选框的功能,多选框是很常见的操作控件,感兴趣的朋友都来一起看看吧
    2022-03-03
  • 详解Android中使用Notification实现进度通知栏(示例三)

    详解Android中使用Notification实现进度通知栏(示例三)

    这篇文章主要介绍了详解Android中使用Notification实现进度通知栏(示例三),具有一定的参考价值,有兴趣的可以了解一下。
    2016-12-12
  • Android Notification使用教程详解

    Android Notification使用教程详解

    这篇文章主要介绍了Android Notification使用,通知的使用的内容还是比较多的,此篇文章将会尽可能详细的介绍Notification的内容,需要的朋友可以参考下
    2022-07-07
  • android监听安装和卸载示例

    android监听安装和卸载示例

    Android应用程序的安装和卸载事件,是由系统进行监听并全局广播的,支持1.5(android 3)以上,因此,如果想要监听获取应用的安装和卸载事件,只需要自定义一个BroadcastReceiver,来对系统广播进行监听和处理
    2014-02-02
  • Kotlin作用域函数之间的区别和使用场景详解

    Kotlin作用域函数之间的区别和使用场景详解

    这篇文章主要给大家介绍了关于Kotlin作用域函数之间的区别和使用场景的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Android开发实现从相册中选择照片功能详解

    Android开发实现从相册中选择照片功能详解

    这篇文章主要介绍了Android开发实现从相册中选择照片功能,涉及Android权限控制、事件绑定、文件路径与获取等相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • 详解Android开发中ContentObserver类的使用

    详解Android开发中ContentObserver类的使用

    这篇文章主要介绍了详解Android开发中ContentObserver类的使用,ContentObserver内容观察者主要用来监听uri的改变请情况,需要的朋友可以参考下
    2016-04-04
  • Android 中 viewpager 滑动指示器的实例代码

    Android 中 viewpager 滑动指示器的实例代码

    本文通过实例代码给大家介绍了android 中 viewpager 滑动指示器,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-12-12
  • 仿iPhone风格对话框(附件包含例子/jar包/jar包源码)

    仿iPhone风格对话框(附件包含例子/jar包/jar包源码)

    这个对框完全继承、仿照AlertDialog,只是实现了自定义效果;另外,没有实现setIcon,因为iphone中的对话框多数都没有图标;附件包含例子、jar包、jar包源码
    2013-01-01

最新评论