iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中Toolbar随着ScrollView滑动透明度渐变效果实现
  • 915
分享到

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

渐变效果明度toolbarscrollviewAndroid 2022-06-06 04:06:19 915人浏览 八月长安
摘要

Android中Toolbar随着ScrollView滑动透明度渐变效果实现 一.思路:监听ScrollView的滑动事件 不断的修改Toolbar的透明度 二.注意 1.Sc

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 { 
 
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自定义进度条渐变色View的实例代码Android ScrollView滑动实现仿QQ空间标题栏渐变Android ListView滑动改变标题栏背景渐变效果Android UI设计系列之自定义ListView仿QQ空间阻尼下拉刷新和渐变菜单栏效果(8)Android自定义渐变式炫酷ListView下拉刷新动画Android Textview实现颜色渐变滚动效果Android中recyclerView底部添加透明渐变效果Android自定义View实现渐变色进度条Android开发之ListView的head消失页面导航栏的渐变出现和隐藏Android自定义view渐变圆形动画


--结束END--

本文标题: Android中Toolbar随着ScrollView滑动透明度渐变效果实现

本文链接: https://www.lsjlt.com/news/22492.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作