介绍

Palette, 英文翻译,调色板,意思比较接近,Google给它的定位应该是颜色萃取器。

看下Source Code

Palette , A helper class to extract prominent colors from an image.

A number of colors with different profiles are extracted from the image:

支持的颜色类型

颜色 类型
Vibrant 有活力
Vibrant Dark 有活力 暗色
Vibrant Light 有活力 亮色
Muted 柔和
Muted Dark 柔和 暗色
Muted Light 柔和 亮色

使用方法:

// Synchronous 同步

Palette p = Palette.from(bitmap).generate();

// Asynchronous 异步

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

public void onGenerated(Palette p) {

// Use generated instance

}

});


Demo示例

布局设置

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="mraz.com.palettedemo.MainActivity"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"></android.support.v7.widget.Toolbar> <android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>

RecyclerView中使用Item的布局设置:card_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="6dp"> <com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/riv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:riv_border_color="@color/colorAccent"
app:riv_border_width="1dp"
app:riv_corner_radius="20dp" /> </LinearLayout>

ActionBar上使用的menu资源:main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <item
android:id="@+id/action_add"
android:icon="@drawable/ic_add_black_24dp"
android:title="@string/add"
app:showAsAction="ifRoom" /> <item
android:id="@+id/action_del"
android:icon="@drawable/ic_remove_black_24dp"
android:title="@string/del"
app:showAsAction="ifRoom" />
</menu>

代码使用

package mraz.com.palettedemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem; import java.util.ArrayList; public class MainActivity extends AppCompatActivity {
private final int[] resIds = {R.drawable.p_1, R.drawable.p_2, R.drawable.p_3, R.drawable.p_4, R.drawable.p_5, R.drawable.p_6, R.drawable.p_7};//资源图片Id
private final String[] titles = {"Vibrant", "DarkVibrant", "LightVibrant", "Muted", "DarkMuted", "LightMuted"};//6种萃取出来的颜色对应的英文翻译
private Toolbar toolbar;//toolbar
private ArrayList<Integer> colorList;//存储从Palette中萃取出来的6中颜色
private int clickCount = 0;//点击数 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); colorList = new ArrayList<>();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);//设置ActionBar RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_content);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter();//初始化RecyclerView recyclerView.setAdapter(myRecyclerAdapter);
recyclerView.setLayoutManager(layoutManager); recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int offset = recyclerView.computeHorizontalScrollOffset();
int width = recyclerView.getChildAt(0).getWidth();
int current = offset / width;
int secondoffset = offset % width;
if (secondoffset >= width / 2) {
current = current + 1;
}
setActionBarColor(current);
clickCount = 0;
super.onScrolled(recyclerView, dx, dy);
}
});//设置recyclerView的滚动事件监听,以此来改变actionbar支持的颜色
setActionBarColor(0);//首次使用初始化
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);//初始化菜单资源
return super.onCreateOptionsMenu(menu);
} private void setActionBarColor(int position) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resIds[position]);
Palette.PaletteAsyncListener paletteAsyncListener = new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
colorList.clear();
colorList.add(palette.getVibrantColor(Color.WHITE));
colorList.add(palette.getDarkVibrantColor(Color.WHITE));
colorList.add(palette.getLightVibrantColor(Color.WHITE));
colorList.add(palette.getMutedColor(Color.WHITE));
colorList.add(palette.getDarkMutedColor(Color.WHITE));
colorList.add(palette.getLightMutedColor(Color.WHITE));//萃取出六种颜色
toolbar.setBackgroundColor(colorList.get(0));
toolbar.setTitle(titles[0]);
}
};
Palette.from(bitmap).generate(paletteAsyncListener);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add: {
clickCount++;
int index = clickCount % (colorList.size());
toolbar.setBackgroundColor(colorList.get(index)); //通过点击事件切换ActionBar的背景色和标题
toolbar.setTitle(titles[index]);
break;
}
case R.id.action_del: {
if (clickCount > 0) clickCount--;
int index = clickCount % (colorList.size());
toolbar.setBackgroundColor(colorList.get(index));
toolbar.setTitle(titles[index]);
break;
}
}
return super.onOptionsItemSelected(item);
}
}

关于Palette的使用分为两种:

1.同步使用

Palette p = Palette.from(bitmap).generate();

2.异步使用

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

public void onGenerated(Palette p) {

// Use generated instance

}

});

Demo效果

操作 效果图
左右滑动ActionBar背景色改变
切换ActionBar背景色

API简介

  • Palette相关API

  • 直接获取颜色的方法



    通过创建的Palette直接获取其中的颜色,用来设置UI界面中的一些元素

  • 获取Swatch的方法

  • 这里获取的Swatch中的内容更丰富

    通过Swatch可以获取更丰富的颜色内容,如



    getTitleTextColor()

    getBodyTextColor()

    getRgb()

    getHsl()



    示例中只是使用了一部分的方法,具体的其他方法使用情况类似,根据Bitmap的颜色风格,设置对应的字体颜色,UI资源颜色,可以使界面看上去更加的和谐和美好。

<Android 基础(八)> Palette的更多相关文章

  1. Android基础总结(8)——服务

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  2. android基础---->DiskLruCache的使用及原理

    DiskLruCache是谷歌推荐的用来实现硬盘缓存的类,今天我们开始对于DiskLruCache的学习.DiskLruCache的测试代码:DiskLruCache的测试代码下载.关于FidkLru ...

  3. Android基础夯实--重温动画(一)之Tween Animation

    心灵鸡汤:真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自己的声音,走出属于自己的道路. 摘要 不积跬步,无以至千里:不积小流,无以成江海.学习任何东西我们都离不开扎实的基础知识 ...

  4. Android基础测试题(四)

    看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...

  5. Android基础测试题(二)

    今天给大家带来的是Android基础测试题(二) 题目要求: 定义一个5位长度的整型数组并初始化,然后构建方法根据用户传入的数字判断是否存在数组中,如果存在,返回所在位置,如果不存在,返回-1 首先第 ...

  6. Bootstrap <基础八>图片

    Bootstrap 提供了三个可对图片应用简单样式的 class: .img-rounded:添加 border-radius:6px 来获得图片圆角. .img-circle:添加 border-r ...

  7. Mono.Android 基础

    Mono.Android 基础 (地址) Mono.Android项目结构是 — Project + Assets + Resources + drawable + layout + values R ...

  8. 深入理解gradle编译-Android基础篇

    深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...

  9. android基础---->JSON数据的解析

    上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...

  10. 基础4 Android基础

    基础4 Android基础 1. Activity与Fragment的生命周期. Activity生命周期 打开应用 onCreate()->onStart()->onResume 按BA ...

随机推荐

  1. C语言数据结构-单链表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-单链表的实现-C语言 typedef struct LNode { int data; struct LNode* next; } LNode,*LinkList; //这两者等价.Li ...

  2. [比赛|考试]nowcoder NOIPpj组第二场

    nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...

  3. c#静态变量赋值问题

    class Program { static int i = getNum(); int j = getNum(); ; static int getNum() { return num; } sta ...

  4. O - 听说下面都是裸题 (最小生成树模板题)

    Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...

  5. stark组件之创建

    stark组件之需求 仿照Django中的admin , 开发了自己的stark组件,实现类似数据库客户端的功能,对数据进行增删改查 . stark之创建 1.在项目中 创建stark应用,app01 ...

  6. centos7初始优化

    第1章 优化 1.1 修改yum源 epel源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Cen ...

  7. Java 类型转换(int->String)

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  8. WIN10安装SW2012完整破解文件和问题解决Crack

    Win10在安装solidword后,激活时有可能报错,本文介绍怎么解决问题 安装后激活报错信息: 一.按下面步骤进行安装 1.相关序列号,记住要断网进行安装 Install SolidWorks 2 ...

  9. 工作ui(2)

    做完整个小Demo整理的一些方法和踩过的miniUI的坑,分享出来希望大家批评指正,共同进步. 1.动态创建列:尽量不要直接在html文件里创建列,动态设置在js文件里方面添加.修改等. 首先把列定义 ...

  10. JQuery 判断滚动条是否到底部

    BottomJumpPage: function () { var scrollTop = $(this).scrollTop(); var scrollHeight = $(document).he ...