Toolbar随着ScrollView滑动透明度渐变效果实现
2017-01-22 10:40
483 查看
一.思路:监听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〉.
[java] view
plain copy
android:clipToPadding="false"
表示控件的绘制范围是否不在padding里面 false就是表示空间的绘制可以绘制到padding中
2〉
[java] view
plain copy
android:clipChildren="false"
表示子控件是否不能超出padding区域(比如: false :ScrollView上滑的时候 child 可以滑出padding区域 ;true:ScrollView上滑的时候 child 不能可以滑出padding区域 )
布局文件如下:
[java] view
plain copy
<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. 创建回调接口:
[java] view
plain copy
public interface TranslucentListener {
/**
* 透明度的回调
* @param alpha
*/
public void onTranslucent(float alpha);
}
2.自定义ScrollView 在onScrollChange方法中回调TranslucentListener接口的方法 并且回传alpha的值:
[java] view
plain copy
@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的值得计算:
[java] view
plain copy
// 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中
[java] view
plain copy
@Override
public void onTranslucent(float alpha) {
toolbar.setAlpha(alpha);
}
二.注意
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〉.
[java] view
plain copy
android:clipToPadding="false"
表示控件的绘制范围是否不在padding里面 false就是表示空间的绘制可以绘制到padding中
2〉
[java] view
plain copy
android:clipChildren="false"
表示子控件是否不能超出padding区域(比如: false :ScrollView上滑的时候 child 可以滑出padding区域 ;true:ScrollView上滑的时候 child 不能可以滑出padding区域 )
布局文件如下:
[java] view
plain copy
<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. 创建回调接口:
[java] view
plain copy
public interface TranslucentListener {
/**
* 透明度的回调
* @param alpha
*/
public void onTranslucent(float alpha);
}
2.自定义ScrollView 在onScrollChange方法中回调TranslucentListener接口的方法 并且回传alpha的值:
[java] view
plain copy
@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的值得计算:
[java] view
plain copy
// 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中
[java] view
plain copy
@Override
public void onTranslucent(float alpha) {
toolbar.setAlpha(alpha);
}
相关文章推荐
- Toolbar随着ScrollView滑动透明度渐变效果实现
- Android中Toolbar随着ScrollView滑动透明度渐变效果实现
- 自定义Scrollview--实现仿淘宝Toolbar透明度渐变效果
- android标题栏颜色渐变效果的实现(标题栏随着scrollview的滚动而变化)
- android标题栏颜色渐变效果的实现(标题栏随着scrollview的滚动而变化)
- ScrollView滑动悬停ToolBar效果实现
- 顶部布局随ScrollView滑动透明度渐变(QQ空间效果)
- 基于JS实现仿京东搜索栏随滑动透明度渐变效果
- 随着RecycleView的滑动实现标题栏透明度渐变:
- Android开发之滑动ScrollView实现ToolBar半透明效果
- 重写ScrollView 实现滑动监听(top栏透明度渐变)
- Android随着scrollView或者recyclerView滑动状态栏颜色渐变效果
- NavgationBar随着滑动颜色渐变效果的实现
- ScrollView滑动实现标题栏透明度随之渐变
- Android 监听scrollview滚动实现ToolBar显示和隐藏的效果
- Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现
- HorizontalScrollView实现Gallery效果,可滑到最后项且点击可滑动到相应位置
- Android UI效果实现 滑动模糊渐变效果实现
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里
- ViewPager和Fragment结合,利用(HorizontalScrollView)实现指示器与ViewPager同时滑动的动态效果