Android 标题栏滑动渐变背景色
来源:互联网 发布:简单装修 知乎 编辑:程序博客网 时间:2024/04/27 14:25
刚好今天做项目的时候遇到这种效果需求,如下,就是随着标题栏的滑动而改变背景色,一旦回到原点,背景色消失。
这里就不需要见解了吧,注释都写好了!
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private LinearLayout llSearch; private HomeAdapter adapter; private int height = 640;// 滑动开始变色的高,真实项目中此高度是由广告轮播或其他首页view高度决定 private int overallXScroll = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); llSearch = (LinearLayout) findViewById(R.id.ll_search); LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.addItemDecoration(new RecycleViewDivider( MainActivity.this, LinearLayoutManager.HORIZONTAL, Utils.dip2px(MainActivity.this, 0.5f), ContextCompat.getColor(MainActivity.this, R.color.line_color))); recyclerView.setLayoutManager(manager); adapter = new HomeAdapter(this); recyclerView.setAdapter(adapter);/** Color.argb((int) 255, 41, 193, 246) * A表示Alpha值,表示颜色的透明度,FF为不透明,00表示完全透明; R表示红颜色值; G表示绿颜色值; B表示蓝颜色值; */ recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); overallXScroll = overallXScroll + dy;// 累加y值 解决滑动一半y值为0 if (overallXScroll <= 0) { //未滑动时,设置透明度为0 rl_top_search.setBackgroundColor(Color.argb((int) 0, 255, 72, 155)); } else if (overallXScroll > 0 && overallXScroll <= height) { //确定一个渐变区域,背景颜色透明度渐变 //设置渐变比例 float scale = (float) overallXScroll / height; float alpha = (255 * scale); rl_top_search.setBackgroundColor(Color.argb((int) alpha, 255, 72, 155)); } else {//超过渐变区域,透明度都是满的 rl_top_search.setBackgroundColor(Color.argb((int) 255, 255, 72, 155)); } } }); }}
阅读全文
0 0
- Android 标题栏滑动渐变背景色
- Android 特效库 - 实现滑动ViewPager渐变背景色
- Android之scrollview滑动使导航栏渐变背景色
- android控件背景色渐变
- Android UI 背景色渐变
- android 设置渐变背景色
- android标题栏滑动渐变效果
- android中改变标题栏的背景色
- Android背景色渐变(左上->右下)
- ViewPager引导页根据滑动渐变背景色
- css 背景色渐变
- 绘制渐变背景色
- 背景色渐变
- 设置渐变背景色
- 设置渐变背景色
- 页面背景色渐变
- css渐变背景色
- 背景色渐变总结
- 从请求中获取int类型参数
- git 合并其他分支的提交
- LA 5092 Permutation Counting
- Android动画之简要(一)
- javascript 面向对象编程(封装、继承)
- Android 标题栏滑动渐变背景色
- ubuntu 错误修正
- Java动态代理+注解体现Spring AOP思想
- C/C++ 指向常量的指针和常量指针
- java内存管理原理
- CEDRAT.FLUX.V9.3.1_Circuit模块好用_(电、磁、热场有限元分析)\
- 检测和识别身份证的关键字段内容。(face++身份证信息识别)
- splay详解
- NSURL 的几个属性