Android L 之 RecyclerView 、CardView 、Palette
转:
http://blog.csdn.net/xyz_lmn/article/details/38735117
《Material Design》提到,Android L版本中新增了RecyclerView、CardView 、Palette。RecyclerView、CardView为用于显示复杂视图的新增Widget。Palette作为调色板类,可以让你从图像中提取突出的颜色。
RecyclerView
RecyclerView作为替代ListView使用,RecyclerView标准化了ViewHolder,ListView中convertView是复用的,在RecyclerView中,是把ViewHolder作为缓存的单位了,然后convertView作为ViewHolder的成员变量保持在ViewHolder中,也就是说,假设没有屏幕显示10个条目,则会创建10个ViewHolder缓存起来,每次复用的是ViewHolder,所以他把getView这个方法变为了onCreateViewHolder。 ViewHolder更适合多种子布局的列表,尤其IM的对话列表。RecyclerView不提供setOnItemClickListener方法,你可以在ViewHolder中添加事件。RecyclerView的使用可以参考《Material Design UI Widgets》。
RecyclerView可以实现横向、纵向滑动视图:
RecyclerView 1 RecyclerView 2
设置横向:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_recycler_view_horizontal);
- // specify an adapter (see also next example)
- List<MyAdapter.Item> itemList = new ArrayList<MyAdapter.Item>();
- for (int i = 0; i < 100; i++)
- itemList.add(new MyAdapter.Item("Item " + i, "world"));
- mAdapter = new MyAdapter(itemList);
- mRecyclerViewHorizontal = (RecyclerView) findViewById(R.id.my_recycler_view_horizontal);
- mRecyclerViewHorizontal.setHasFixedSize(true);
- // use a linear layout manager
- LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
- mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
- mRecyclerViewHorizontal.setLayoutManager(mLayoutManager);
- mRecyclerViewHorizontal.setAdapter(mAdapter);
- }
CardView
CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影。CardView是一个Layout,可以布局其他View。CardView 的使用可以参考《Material Design UI Widgets》。文章最后会给出这篇文章示例代码。
CardView Palette
Palette
如果你试过android Lollipop的sdk,你可能注意到了Palette。Palette从图像中提取突出的颜色,这样可以把色值赋给ActionBar、或者其他,可以让界面整个色调统一。

创建Palette实例
有四种创建实例的方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// Synchronous methods.// --------------------------------// These should be used when you have access to the underlying image loading thread.// Picasso allows this through a Transformation. For other libraries, YMMV.// Uses the default palette size (16).Palette p = Palette.generate(bitmap);// Allows you to specify the maximum palette size, in this case 24.Palette p = Palette.generate(bitmap, 24);// Asynchronous methods// --------------------------------// This is the quick and easy integration path. Internally uses an AsyncTask so// this may not be optimal (since you're dipping in and out of threads)// Uses the default palette size (16).Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { // Here's your generated palette }});// Allows you to specify the maximum palette size, in this case 24.Palette.generateAsync(bitmap, 24, new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { // Here's your generated palette }}); |
创建完一个实例之后,我们还需要得到一种采集的样本(swatch),有6中样本(swatch):
|
1
2
3
4
5
6
|
Vibrant. Palette.getVibrantSwatch()Vibrant dark. Palette.getDarkVibrantSwatch()Vibrant light. Palette.getLightVibrantSwatch()Muted. Palette.getMutedSwatch()Muted dark. Palette.getDarkMutedSwatch()Muted light. Palette.getLightMutedSwatch() |
具体选择哪一种取决于你自己,大多数情况下我们都使用Vibrant and Dark Vibrant。
使用样本(swatch)
swatch有以下方法:
|
1
2
3
4
5
|
getPopulation(): the amount of pixels which this swatch represents.getRgb(): the RGB value of this color.getHsl(): the HSL value of this color.getBodyTextColor(): the RGB value of a text color which can be displayed on top of this color.getTitleTextColor(): the RGB value of a text color which can be displayed on top of this color. |
比如如果你的TextView 有个背景图片,要想让字体颜色能够和背景图片匹配,则使用getBodyTextColor()比较合适,getTitleTextColor()其实应该和getBodyTextColor()差不多。
下面的代码则是展示了如何从一张图片中提取颜色将textView的背景色设置成图片的主色调,然后再使用getTitleTextColor()来设置一个匹配的文字颜色。
|
1
2
3
4
5
6
|
Palette.Swatch swatch = palette.getVibrantSwatch();TextView titleView = ...;if (swatch != null) { titleView.setBackgroundColor(swatch.getRgb()); titleView.setTextColor(swatch.getTitleTextColor());} |
需要注意的是getVibrantSwatch()可能会返回一个null值,所以检查一下是必须的。
size的问题
你还可以使用如下方法一次性获得所有的swatch:
|
1
|
List<Palette.Swatch> swatches = palette.getSwatches(); |
在上面的代码中,你可能注意到了可以设置palette的size。size越大,花费的时间越长,而越小,可以选择的色彩也越小。最佳的选择是根据image的用途:
头像之类的,size最好在24-32之间;
风景大图之类的 size差不多在8-16;
默认是16.
===================================================================================================
Palette从图像中提取突出的颜色,这样可以把色值赋给ActionBar、或者其他,可以让界面整个色调统一。
Palette这个类中提取以下突出的颜色:
Vibrant (有活力)
Vibrant dark(有活力 暗色)
Vibrant light(有活力 亮色)
Muted (柔和)
Muted dark(柔和 暗色)
Muted light(柔和 亮色)
提取色值代码如下:
- Bitmap bm = BitmapFactory.decodeResource(getResources(), item.image);
- Palette palette = Palette.generate(bm);
- if (palette.getLightVibrantColor() != null) {
- name.setBackgroundColor(palette.getLightVibrantColor().getRgb());
- getSupportActionBar().setBackgroundDrawable(new ColorDrawable(palette.getLightVibrantColor().getRgb()));
- // getSupportActionBar().
- }
Android L 之 RecyclerView 、CardView 、Palette的更多相关文章
- Android L中间RecyclerView 、CardView 、Palette使用
RecyclerView CardView Palette <Material Design>提到,Android L版本号中新增了RecyclerView.CardView .Palet ...
- Android L中的RecyclerView 、CardView 、Palette的使用
<Material Design>提到,Android L版本中新增了RecyclerView.CardView .Palette.RecyclerView.CardView为用于显示复杂 ...
- ANDROID L——RecyclerView,CardView进口和使用(Demo)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控 ...
- Android L新控件RecyclerView简介
Android L是android进化史上的里程碑,尽管还没有正式发布4.5或者5.0,但预览版也同样精彩. 这篇文章只是另外一篇博客的总结性翻译,能够读懂原文的,可以点开这个链接去阅读精彩的原文:h ...
- 【android】使用RecyclerView和CardView,实现知乎日报精致布局
完整代码,请参考我的博客园客户端,git地址:http://git.oschina.net/yso/CNBlogs 在写博客园客户端的时候,突然想到,弄个知乎日报风格的简单清爽多好!不需要那么多繁杂的 ...
- ANDROID L——Material Design详解(UI控件)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...
- RecyclerView,CardView导入和使用(Demo)
简介: 这篇文章是ANDROID L——Material Design详解(UI控件)的一个补充或者说是应用实例,如果有时间建议大家稍微浏览一下上篇文章. 本文主要介绍Android L新增加的两个U ...
- Android应用开发:CardView的使用及兼容
引言 在Google I/O 2014上,Google公布了Android L Preview版本,此版本的UI有了非常大的改变,很炫很给力!同时,Google也给出了两个可以向下兼容的控件放到了V7 ...
- ANDROID L——Material Design综合应用(Demo)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Material Design: Material Design是Google推出的一个全 ...
随机推荐
- Servlet 学习总结-2
#重定向与转发的区别 开发Web应用中会遇到从一个页面跳转到另一个页面的问题,在JSP中有两种跳转方式: 1.重定向 2.转发(转向) 重定向:首先服务器受到浏览器客户端请求之后,服务器发送新的链接到 ...
- mysql 互为主从复制常见问题
报错:1)change master导致的: Last_IO_Error: error connecting to master - retry-time: 60 retr ...
- MYSQL SET类型字段的SQL查询某个字段保函某个值的查询
1.column set('hot','crazy','smart') //column字段(set属性)三个值 2.select * from table where FIND_IN_SET('h ...
- Memcache 在win7x64中安装配置
Memcached从0.2.0开始,要求PHP版本大于等于5.2.0. 环境:phpstudy集成环境 目标:实现php用memcache 下载:memcache for win 64 http:// ...
- js禁止中文输入 最简洁的【禁止输入中文】
方法一:禁止中文输入法 <input type="text" > 方法二:禁止黏贴,禁止拖拽,禁止中文输入法! 这种方法是最强的禁止 中文输入 <input t ...
- Moodle的qq登录版块的使用
在这篇Moodle的qq登录(QQ登陆)版块的使用教程中,我们假定你已经有了一个有域名,外网能访问的Moodle2.4+网站,并且数据库使用的是mysql. 我们将提供Moodle的QQ登录版块的下载 ...
- python操作csv-xls完善后的代码
#coding:utf-8 #导入相应模块 import csv,xlwt,sys,os,fnmatch,xlrd from xlutils.copy import copy #对xls文件中的绝对值 ...
- Amzon MWS API开发之 上传数据
亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...
- Java常见序列化与反序列方法总结
很多商业项目用到数据库.内存映射文件和普通文件来完成项目中的序列化处理的需求,但是这些方法很少会依靠于Java序列化.本文也不是用来解释序列化的,而是一起来看看面试中有关序列化的问题,这些问题你很有可 ...
- matlab---边缘之sobel简单实例
最近在项目中需要做一些图像边缘检测的工作,但是由于之前没接触过图像处理的相关知识,所以只得 在matlab里面对一些图像处理函数挨个挨个的试着用.在用的过程中在慢慢的明白了一些简单的图像处 理方法. ...