Android Support Library控件详细介绍之RecyclerView

RecyclerView控件

依赖  compile 'com.android.support:recyclerview-v7:24.1.1'
RecyclerView也是容器控件,大多数的效果显示可通过代码来控制,这样更加自由。

RecyclerView使用起来很方便因为它提供:
1.它为item的定位提供一个layoutmanager
2.为item的操作提供一个缺省的animations
3.您还可以灵活地定义这个小部件的自定义布局管理器和动画

setLayoutManager()方法接受一个 LayoutManager 布局管理参数。参数类型可以有以下几种:
1.LinearLayoutManager:线性布局
2.GridLayoutManager:网格布局
3.StaggeredGridLayoutManager:流式布局

这样设置recylerView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, true));

第一个参数是Context,第二个参数:布局方向LinearLayout.VERTIAL和LinearLayout.HORIZONTAL。
第三个参数:表示是否从最后的item数据开始显示,true表示是,false表示正常显示——从开头显示。
setItemAnimator()方法的作用是设置当前RecyclerView容器有子Item改变时(添加item或者删除item)导致
整个布局的动画效果。一般我们new 一个系统默认的动画出来就好了
recylerView.setItemAnimator(new DefaultItemAnimator());

Palette

依赖:compile 'com.android.support:palette-v7:22.2.0'

Palette根据图片来决定标题的颜色和标题栏的背景色。

它能让你从图像中提取突出的颜色。这个类能提取以下突出的颜色:
Vibrant(充满活力的)
Vibrant dark(充满活力的黑)
Vibrant light(充满活力的亮)
Muted(柔和的)
Muted dark(柔和的黑)
Muted lighr(柔和的亮)
使用:
1.先获取Bitmap
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img1);
2.创建Palette
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
      3.采集样本(swatch),有6种样本
           Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力
           Palette.Swatch DVibrant= palette.getDarkVibrantSwatch();//有活力 暗色
           Palette.Swatch LVibrant = palette.getLightVibrantSwatch();//有活力 亮色
           Palette.Swatch Muted= palette.getMutedSwatch();//柔和
           Palette.Swatch DMuted= palette.getDarkMutedSwatch();//柔和 暗色
           Palette.Swatch LMuted = palette.getLightMutedSwatch();//柔和 亮色
      4,使用样本
           //需要注意的是getVibrantSwatch()可能会返回一个null值,所以检查一下是必须
             TextView titleView = ...;
             if (LVibrant != null) {
                 titleView.setBackgroundColor(swatch.getRgb());
                 titleView.setTextColor(swatch.getTitleTextColor());
              }   
            }
        });

使用样本Swatch有以下几种方法:
getPopulation(): the amount of pixels which this swatch represents.(该样本含有的像素数量)
getRgb(): the RGB value of this color.(RGB值)
getHsl(): the HSL value of this color.(HSL值)
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.(标题颜色)

介绍一点Bitmap

Bitmap代表一张位图,扩展名可以是.bmp或者.dib。位图是Windows标准格式图形文件,他将图像定义为由点(像素)组成,每个点可以有多种色彩表示,包括2,4,8,16和32位色彩。例如,一幅1024*768分辨率的32位真彩图片,其所占存储字节数为:1024*768*32/8=3072KB。
位图文件图像效果好,但是非压缩格式的,需要占用较大存储空间,不利于在网络上传送。jpg格式则恰好弥补了位图 的这个缺点。

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切,旋转,缩放等操作,并可以指定格式保存图像
文件。
Bitmap实现子啊android.graphics包中。

1.从资源文件中获取

 Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img1); 

2.从SD卡中得到图片
(方法1)
String SDCarePath=Environment.getExternalStorageDirectory().toString();
String filePath=SDCarePath+"/"+"haha.jpg";
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);
(方法2)
InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg");
Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream);

原文地址:https://www.cnblogs.com/feng-ye/p/5777915.html