博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android CollapsingToolbarLayout
阅读量:4705 次
发布时间:2019-06-10

本文共 3398 字,大约阅读时间需要 11 分钟。

第一次看到这种用户体验是在Google Play Store App的应用详情的Activity中.

大的Banner图,能第一时间吸引用户的眼球,用不一样的Banner大图更具个性化的展示内容.图总是比文字要吸引人.

当向下滚动时,Banner大图会跟随滚动手势而Collapse.最后收折成一个普通的ActionBar(实际是个Toolbar,Android官方在最新的Support Library都推荐把ActionBar替换成Toolbar).

通过属性Flag的组合,也能实现把ActionBar直接推出屏幕,让其消失.

中提供的CollapseToolbar实现这效果.

这是Layout布局.CoordinatorLayout和AppBarLayout的组合在中有介绍,实现了滚动隐藏Toolbar的效果,这里就不在重复了.

CollapsingToolbarLayout是实现GIF效果的关键.

CollapsingToolbarLayout有两个Children.ImageView用来显示Banner大图,即Gif中曼联队徽的大图.而Toolbar就是折叠后看到的顶栏Toolbar.

app:contentScrim="?attr/colorPrimary",CollapsingToolbarLayout这个属性是设置折叠后Toolbar的颜色.

app:layout_scrollFlags="scroll|exitUntilCollapsed",这是两个Flag控制滚动时候CollapsingToolbarLayout的表现.

     1) Scroll, 表示向下滚动列表时候,CollapsingToolbarLayout会滚出屏幕并且消失(原文解释:this flag should be set for all views that want to scroll off the screen - for views that do not use this flag, they’ll remain pinned to the top of the screen)

     2) exitUntilCollapsed, 表示这个layout会一直滚动离开屏幕范围,直到它收折成它的最小高度.(原文解释:this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting)

app:layout_collapseMode="parallax",这是控制滚出屏幕范围的效果的

     1) parallax,表示滚动过程中,会一直保持可见区域在正中间.

     2) pin,表示不会被滚出屏幕范围.

 

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fourth_activity);        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        ActionBar actionBar = getSupportActionBar();        if (actionBar != null) {            actionBar.setDisplayHomeAsUpEnabled(true);        }        final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(                R.id.collapsing_toolbar);        collapsingToolbar.setTitle(getString(R.string.fourth_activity));        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);        recyclerView.setLayoutManager(linearLayoutManager);        recyclerView.setAdapter(new MyAdapter(this));        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mu);        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {            @Override            public void onGenerated(final Palette palette) {                int defaultColor = getResources().getColor(R.color.medium_blue);                int defaultTitleColor = getResources().getColor(R.color.white);                int bgColor = palette.getDarkVibrantColor(defaultColor);                int titleColor = palette.getLightVibrantColor(defaultTitleColor);                collapsingToolbar.setContentScrimColor(bgColor);                collapsingToolbar.setCollapsedTitleTextColor(titleColor);                collapsingToolbar.setExpandedTitleColor(titleColor);            }        });    }

这是Activity的onCreate方法,有两处地方需要关注的

1. setSupportActionBar()方法,告诉AppCompatActivity哪一个是ActionBar(实际是Toolbar).不然的话,做透明Status Bar(电池,手机信号那一区域)效果时候,ActionBar会位置不正确.

2. Palette,调色板的意思,也是Android Support Library提供的.用来抓取Bitmap的颜色.在此处的用处是,当ActionBar被收折后,背景颜色能保持和Banner大图的色调一致,而Title文字的颜色保证和Banner大图的色调形成强对比.

 

Demo 代码地址:  

转载于:https://www.cnblogs.com/wingyip/p/4609891.html

你可能感兴趣的文章
【Mongodb】---Scheme和Collections对应问题
查看>>
团队作业个人博客02
查看>>
改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects)
查看>>
大型高性能ASP.NET系统架构设计
查看>>
php : 基础(6)
查看>>
在Linux自己Home下安装python
查看>>
SceneGrabber NET 视频批量自动截图软件使用技巧
查看>>
Codeforces985E. Pencils and Boxes (单调队列)
查看>>
python 打印进度条
查看>>
Art Pipeline for glTF
查看>>
Spring Boot配置
查看>>
芒果云 在线代码编辑器
查看>>
《从零开始学Swift》学习笔记(Day 16)——字典集合
查看>>
NOIP2012Day2 T1/T2题解
查看>>
hdu 2689
查看>>
C#和Unity总结Day01
查看>>
SQLAlchemy中解决数据库访问时出现的Incorrect string value: xxx at row 484
查看>>
5238-整数校验器-洛谷3月赛gg祭
查看>>
IOS 给按钮添加图片
查看>>
适合移动应用的日期时间拾取器
查看>>