Palette是一个类似调色板的工具类,根据传入的bitmap,提取出主体颜色,使得图片和颜色更加搭配,界面更协调。Palette 可以从一张图片中提取颜色,我们可以把提取的颜色融入到App UI中,可以使UI风格更加美观融洽。比如,我们可以从图片中提取颜色设置给ActionBar做背景颜色,这样ActionBar的颜色就会随着显示图片的变化而变化。

Palette可以提取的颜色如下:

● Vibrant (有活力的)

● Vibrant dark(有活力的 暗色)

● Vibrant light(有活力的 亮色)

● Muted (柔和的)

● Muted dark(柔和的 暗色)

● Muted light(柔和的 亮色)

第一步

导入v7包下的palette: compile 'com.android.support:palette-v7:23.4.0'

第二步,我们需要通过一个Bitmap对象来生成一个对应的Palette对象。 Palette 提供了四个静态方法用来生成对象

● Palette generate(Bitmap bitmap)

● Palette generate(Bitmap bitmap, int numColors)

● generateAsync(Bitmap bitmap, PaletteAsyncListener listener)

● generateAsync(Bitmap bitmap, int numColors, final PaletteAsyncListener listener)

不难看出,生成方法分为generate(同步)和generateAsync(异步)两种,如果图片过大使用generate方法,可能会阻塞主线程,我们更倾向于使用generateAsync的方法,其实内部就是创建了一个AsyncTask。generateAsync方法需要一个PaletteAsyncListener对象用于监听生成完毕的回调。除了必须的Bitmap参数外,还可以传入一个numColors参数指定颜色数,默认是 16。

第三步,得到Palette对象后,就可以拿到提取到的颜色值

● Palette.getVibrantSwatch()

● Palette.getDarkVibrantSwatch()

● Palette.getLightVibrantSwatch()

● Palette.getMutedSwatch()

● Palette.getDarkMutedSwatch()

● Palette.getLightMutedSwatch()

第四步,使用颜色,上面get方法中返回的是一个 Swatch 样本对象,这个样本对象是Palette的一个内部类,它提供了一些获取最终颜色的方法。

● getPopulation(): 样本中的像素数量

● getRgb(): 颜色的RBG值

● getHsl(): 颜色的HSL值

● getBodyTextColor(): 主体文字的颜色值

● getTitleTextColor(): 标题文字的颜色值

通过 getRgb() 可以得到最终的颜色值并应用到UI中。getBodyTextColor() 和 getTitleTextColor() 可以得到此颜色下文字适合的颜色,这样很方便我们设置文字的颜色,使文字看起来更加舒服。

示例:这边有这样一张图片

f.png

我们使用Palette获取它的颜色

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/t1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="darkMutedColor"

android:minHeight="40dp"

android:gravity="center"

android:layout_marginTop="10dp"

android:layout_alignParentTop="true"

/>

android:id="@+id/t2"

android:layout_below="@+id/t1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="darkVibrantColor"

android:minHeight="40dp"

android:gravity="center"

android:layout_marginTop="10dp"

/>

android:id="@+id/t3"

android:layout_below="@+id/t2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="lightVibrantColor"

android:minHeight="40dp"

android:gravity="center"

android:layout_marginTop="10dp"

/>

android:layout_below="@+id/t3"

android:id="@+id/t4"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="lightMutedColor"

android:minHeight="40dp"

android:gravity="center"

android:layout_marginTop="10dp"

/>

android:layout_below="@+id/t4"

android:id="@+id/t5"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="mutedColor"

android:minHeight="40dp"

android:gravity="center"

android:layout_marginTop="10dp"

/>

android:layout_below="@+id/t5"

android:id="@+id/t6"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="vibrantColor"

android:minHeight="40dp"

android:gravity="center"

android:layout_marginTop="10dp"

/>

public class MainActivity extends AppCompatActivity {

TextView t1;

TextView t2;

TextView t3;

TextView t4;

TextView t5;

TextView t6;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

t1 = (TextView) findViewById(R.id.t1);

t2 = (TextView) findViewById(R.id.t2);

t3 = (TextView) findViewById(R.id.t3);

t4 = (TextView) findViewById(R.id.t4);

t5 = (TextView) findViewById(R.id.t5);

t6 = (TextView) findViewById(R.id.t6);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f);

Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {

//发生主线程 Palette调色板 总共六种颜色

@Override

public void onGenerated(Palette palette) {

//柔和而暗的颜色

int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);

//鲜艳和暗的颜色

int darkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);

//亮和鲜艳的颜色

int lightVibrantColor = palette.getLightVibrantColor(Color.BLUE);

//亮和柔和的颜色

int lightMutedColor = palette.getLightMutedColor(Color.BLUE);

//柔和颜色

int mutedColor = palette.getMutedColor(Color.BLUE);

int vibrantColor = palette.getVibrantColor(Color.BLUE);

t1.setBackgroundColor(darkMutedColor);

t2.setBackgroundColor(darkVibrantColor);

t3.setBackgroundColor(lightVibrantColor);

t4.setBackgroundColor(lightMutedColor);

t5.setBackgroundColor(mutedColor);

t6.setBackgroundColor(vibrantColor);

}

});

}

}

Palette取出的各种颜色.jpg

android palette组件用法,Android--Palette基本使用相关推荐

  1. android palette组件用法,Android Support Palette使用详解

    使用Palette API选择颜色 良好的视觉设计是app成功所必不可少的, 而色彩设计体系是设计的基础构成. Palette包是支持包, 能够从图片中解析出突出的颜色, 从而帮助你创建出视觉迷人的应 ...

  2. android palette组件用法,Android Material Design:使用Palette优化界面色彩搭配。

    我写过一篇博客介绍了常用Material Design控件的使用. http://www.jianshu.com/p/776cc6329fff 本想把大部分的Material Design的知识点写到 ...

  3. android palette组件用法,android Palette使用详解

    Palette是一个可以从图片(Bitmap)中提取颜色的帮助类,可以使UI更加美观,根据图片动态的显示相应的颜色.现在最新的api是在版本22.0.0添加的,本篇文章也是使用的22.0.0的api版 ...

  4. android palette组件用法,Android Palette(调色板)的使用

    一.Palette的简单介绍 android-support-v7-palette 里面的Palette是Android L SDK 中的新特性,可以使用 Palette 从图像中提取出突出的颜色(主 ...

  5. android palette组件用法,android Palette调色板使用详解

    Palette是一个可以从图片(Bitmap)中提取颜色的帮助类,可以使UI更加美观,根据图片动态的显示相应的颜色.现在最新的api是在版本22.0.0添加的,本篇文章也是使用的22.0.0的api版 ...

  6. android图片显示组件,Android可循环显示图像的Android Gallery组件用法实例

    本文实例分析了Android可循环显示图像的Android Gallery组件用法.分享给大家供大家参考,具体如下: Gallery组件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能 ...

  7. android palette组件用法,Palette颜色提取使用详解

    如果你试过android Lollipop的sdk,你可能注意到了Palette.Palette从图像中提取突出的颜色,这样可以把色值赋给ActionBar.或者其他,可以让界面整个色调统一. 创建P ...

  8. android置组件下面,Android Jetpack架构组件(十二)之Hilt

    一. 依赖注入简介 依赖注入(英文Dependency Injection,简写DI)是一种被广泛使用的编程技术,主要的作用代码解耦. 借助依赖注入,我们可以轻松的管理类之间的依赖,并最终建立高可维护 ...

  9. android气泡组件,气泡  |  Android 开发者  |  Android Developers

    气泡让用户可以轻松查看并参与对话. 气泡内置于"通知"系统中.它们浮动在其他应用内容上层,并会跟随用户转到任意位置.气泡可以展开以显示应用功能和信息,并可在不使用时收起. 当设备处 ...

最新文章

  1. Day03——Python函数
  2. string和C语言字符串之间的相互转换以及string常用函数
  3. android removeRule的使用
  4. System Test GIS压力测试利器
  5. PHP7.2 redis
  6. 一次性插入上万条数据的写法
  7. 关于用MATLAB求解定积分方程的问题
  8. 关于EditPlus3 取消备份后再重新打开 备份设置还原到默认状态的问题
  9. kali 克隆网页_Web侦察工具HTTrack (网站克隆)
  10. 12000字解读BabyCare:母婴用品品牌的终局是走向全品类吗?
  11. 球面绘制六边形——二维平面坐标转成三维球面坐标
  12. 字节跳动终于迎来普调,薪资普遍降17%
  13. 最大公约数,最小公倍数的求法
  14. IMPALA(Importance Weighted Actor-Learner Architectures)
  15. 我与Java的那些年和那些事
  16. limbo运行veket linux,Veket——『350M』的操作系统,五脏俱全全到可怕!
  17. 百度推送网站url到搜索引擎方式 : 主动推送sitemap
  18. [Alpha] Scrum Meeting 6 - TEAM LESS ERROR
  19. 快递100发送短信提醒
  20. 论文编辑与投稿——引用文献(References)中作者名字中“姓”、“名”应该如何组织

热门文章

  1. 计算机网络七年级教学设计,初中七年级信息技术教案
  2. 有关计算机的应用的文章,关于计算机常用软件的论文参考文献 计算机常用软件参考文献有哪些...
  3. 【老生谈算法】matlab实现图像拼接算法源码——图像拼接
  4. Go技术日报(2021-07-27)——全网最全的Go书单来喽
  5. 更新补丁引发系统性瘫痪,VMware替换势在必行
  6. 麦当劳重金收购一大数据创业公司,持续加码数字化转型
  7. Pandas初级认识
  8. 互联网中国20年:生活方式是否时尚
  9. 万能的Attention及其代码实现
  10. GoogleDeveloperDay 回顾