Android ScrollView滑动标题栏变色
2016-12-13 18:44
447 查看
就不上效果图了,现在各种app都有这种效果,随着ScrollView的滑动,以某个View为参照,标题栏背景渐渐由 透明 -> 指定颜色
很多实现方法,以下为重写ScrollView。
布局:
重写的ScrollView:
调用:
很多实现方法,以下为重写ScrollView。
布局:
<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"> <AlphaTitleScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-各种内容-> </LinearLayout> </AlphaTitleScrollView> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="标题"/> </RelativeLayout>
重写的ScrollView:
public class AlphaTitleScrollView extends ScrollView { private View mReferenceView; private View mTitleView; private int backgroundColor; private int[] backgroundColorRGB; private boolean isSlowlyChange = true; public AlphaTitleScrollView(Context context) { super(context); } public AlphaTitleScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public AlphaTitleScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public AlphaTitleScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void scrollTo(int x, int y) { if (x == 0 && y == 0 || y <= 0) { super.scrollTo(x, y); } } public void setSlowlyChange(boolean slowlyChange) { this.isSlowlyChange = slowlyChange; } /** * 初始化设置参数 * * @param titleView * @param referenceView * @param backgroundColor * @param backgroundColorRGB */ public void initAlphaTitle(View titleView, View referenceView, int backgroundColor, int[] backgroundColorRGB) { this.mTitleView = titleView; this.mReferenceView = referenceView; this.backgroundColor = backgroundColor; this.backgroundColorRGB = backgroundColorRGB; } @Override protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { super.onOverScrolled(scrollX, scrollY, clampedX, clampedY); if (scrollY >= mReferenceView.getTop() + mReferenceView.getMeasuredHeight()) { mTitleView.setBackgroundColor(backgroundColor); } else if (scrollY >= 0) { if (!isSlowlyChange) { mTitleView.setBackgroundColor(Color.TRANSPARENT); } else { float persent = scrollY * 1f / (mReferenceView.getTop() + mReferenceView.getMeasuredHeight()); int alpha = (int) (255 * persent); int color = Color.argb(alpha, backgroundColorRGB[0], backgroundColorRGB[1], backgroundColorRGB[2]); mTitleView.setBackgroundColor(color); } } }
调用:
mAlphaScrollView.initAlphaTitle(titleView, headView, getResources().getColor(R.color.yellow), new int[]{253, 215, 62});
相关文章推荐
- 浅析Android Handler机制原理。
- Facebook android 第三方登录
- android最容易集成带title的viewpager
- Androidstudio中Log标签的简单使用
- Android 程序是否是在前端运行
- Android中常见的图片加载框架
- Android 数据存储之SQLite数据库存储
- android - px(像素)、dpi(像素密度)、dip(密度无关像素)之间的关系
- Android6.0 WMS(四) WMS中常用变量分析
- Android 热修复原理
- google中国android文档官网地址
- Android动态分析工具Inspeckage
- Android 程序开发——百度地图的使用(二) 基础地图
- DataBinding系列二、绑定事件处理方法
- <学习笔记>Android ListView 通过指定position改变item颜色
- android 控件NullPointerException
- Android Touch事件总结 二 (双指实现RecyclerView的快速滚动)
- 【Fragment】生命周期
- unable to access android sdk add-on list
- 主页面设计-单排list