android palette组件用法,android Palette调色板使用详解
Palette是一个可以从图片(Bitmap)中提取颜色的帮助类,可以使UI更加美观,根据图片动态的显示相应的颜色。现在最新的api是在版本22.0.0添加的,本篇文章也是使用的22.0.0的api版本(注意版本之间api的不同)。
应用中的效果 Demo 效果
Palette可以提取的颜色:
Vibrant (有活力的)
Vibrant dark(有活力的 暗色)
Vibrant light(有活力的 亮色)
Muted (柔和的)
Muted dark(柔和的 暗色)
Muted light(柔和的 亮色)
使用方法: module的build.gradle中引用
compile 'com.android.support:palette-v7:25.3.1'
使用步骤:
1.获取Palette对象,也就是图像调色板
2.获取从图像调色板生成的色样
3.从色样中提取相应颜色
1.获取Palette对象,也就是图像调色板
获取Palette对象有同步和异步两种方式,建议使用异步获取Palette对象
// Synchronous
Palette p = Palette.from(bitmap).generate();
// Asynchronous
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance
}
});
2.获取从图像调色板生成的色样
可以获取到六种色样,但是有的时候获取不到对应的色样对象,必须注意非空判断。
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的
Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色
Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色
Palette.Swatch muted = palette.getMutedSwatch();//柔和的
Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色
Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色
3.从色样中提取相应颜色
通过 getRgb() 可以得到最终的颜色值并应用到UI中。getBodyTextColor() 和 getTitleTextColor() 可以得到此颜色下文字适合的颜色,这样很方便我们设置文字的颜色,使文字看起来更加舒服。
swatch.getPopulation(): 样本中的像素数量
swatch.getRgb(): 颜色的RBG值
swatch.getHsl(): 颜色的HSL值
swatch.getBodyTextColor(): 主体文字的颜色值
swatch.getTitleTextColor(): 标题文字的颜色值
Demo的代码中没有对获取到的色样对象进行非空判断,注意一定要加上非空判断
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
private LinearLayout line1,line2,line3,line4,line5,line6;
private TextView tv1_1,tv1_2,tv2_1,tv2_2,tv3_1,tv3_2,tv4_1,tv4_2,tv5_1,tv5_2,tv6_1,tv6_2;
private List bgs = new ArrayList<>();
private List bodyTexts = new ArrayList<>();
private List titleTexts = new ArrayList<>();
private List swatchs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.img);
initView();
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
if (bitmap == null){
return;
}
Palette.from(bitmap).generate(listener);
}
private Palette.PaletteAsyncListener listener = new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
if (palette != null){
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的
Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色
Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色
Palette.Swatch muted = palette.getMutedSwatch();//柔和的
Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色
Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色
swatchs.clear();
swatchs.add(vibrant);swatchs.add(vibrantDark);swatchs.add(vibrantLight);
swatchs.add(muted);swatchs.add(mutedDark);swatchs.add(mutedLight);
show();
}
}
};
private void show() {
for (int i = 0; i < 6; i++) {
bgs.get(i).setBackgroundColor(swatchs.get(i).getRgb());
bodyTexts.get(i).setTextColor(swatchs.get(i).getBodyTextColor());
titleTexts.get(i).setTextColor(swatchs.get(i).getTitleTextColor());
}
}
private void initView() {
line1 = (LinearLayout) findViewById(R.id.line1);
line2 = (LinearLayout) findViewById(R.id.line2);
line3 = (LinearLayout) findViewById(R.id.line3);
line4 = (LinearLayout) findViewById(R.id.line4);
line5 = (LinearLayout) findViewById(R.id.line5);
line6 = (LinearLayout) findViewById(R.id.line6);
bgs.clear();
bgs.add(line1);bgs.add(line2);bgs.add(line3);bgs.add(line4);bgs.add(line5);bgs.add(line6);
tv1_1 = (TextView) findViewById(R.id.tv1_1);
tv2_1 = (TextView) findViewById(R.id.tv2_1);
tv3_1 = (TextView) findViewById(R.id.tv3_1);
tv4_1 = (TextView) findViewById(R.id.tv4_1);
tv5_1 = (TextView) findViewById(R.id.tv5_1);
tv6_1 = (TextView) findViewById(R.id.tv6_1);
tv1_2 = (TextView) findViewById(R.id.tv1_2);
tv2_2 = (TextView) findViewById(R.id.tv2_2);
tv3_2 = (TextView) findViewById(R.id.tv3_2);
tv4_2 = (TextView) findViewById(R.id.tv4_2);
tv5_2 = (TextView) findViewById(R.id.tv5_2);
tv6_2 = (TextView) findViewById(R.id.tv6_2);
bodyTexts.clear();titleTexts.clear();
bodyTexts.add(tv1_1);bodyTexts.add(tv2_1);bodyTexts.add(tv3_1);bodyTexts.add(tv4_1);bodyTexts.add(tv5_1);bodyTexts.add(tv6_1);
titleTexts.add(tv1_2);titleTexts.add(tv2_2);titleTexts.add(tv3_2);titleTexts.add(tv4_2);titleTexts.add(tv5_2);titleTexts.add(tv6_2);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
android palette组件用法,android Palette调色板使用详解相关推荐
- Android四大组件之bindService源码实现详解
Android四大组件之bindService源码实现详解 Android四大组件源码实现详解系列博客目录: Android应用进程创建流程大揭秘 Android四大组件之bindServic ...
- android palette组件用法,Android Palette(调色板)的使用
一.Palette的简单介绍 android-support-v7-palette 里面的Palette是Android L SDK 中的新特性,可以使用 Palette 从图像中提取出突出的颜色(主 ...
- android palette组件用法,Android Material Design:使用Palette优化界面色彩搭配。
我写过一篇博客介绍了常用Material Design控件的使用. http://www.jianshu.com/p/776cc6329fff 本想把大部分的Material Design的知识点写到 ...
- android palette组件用法,android Palette使用详解
Palette是一个可以从图片(Bitmap)中提取颜色的帮助类,可以使UI更加美观,根据图片动态的显示相应的颜色.现在最新的api是在版本22.0.0添加的,本篇文章也是使用的22.0.0的api版 ...
- android palette组件用法,Android Support Palette使用详解
使用Palette API选择颜色 良好的视觉设计是app成功所必不可少的, 而色彩设计体系是设计的基础构成. Palette包是支持包, 能够从图片中解析出突出的颜色, 从而帮助你创建出视觉迷人的应 ...
- android图片显示组件,Android可循环显示图像的Android Gallery组件用法实例
本文实例分析了Android可循环显示图像的Android Gallery组件用法.分享给大家供大家参考,具体如下: Gallery组件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能 ...
- android 实现毫秒定时器,Android实现定时器的五种方法实例详解
一.Timer Timer是Android直接启动定时器的类,TimerTask是一个子线程,方便处理一些比较复杂耗时的功能逻辑,经常与handler结合使用. 跟handler自身实现的定时器相比, ...
- [免费专栏] Android安全之数据存储与数据安全「详解」
也许每个人出生的时候都以为这世界都是为他一个人而存在的,当他发现自己错的时候,他便开始长大 少走了弯路,也就错过了风景,无论如何,感谢经历 Android安全付费专栏长期更新,本篇最新内容请前往: [ ...
- Android 事件分发机制分析及源码详解
Android 事件分发机制分析及源码详解 文章目录 Android 事件分发机制分析及源码详解 事件的定义 事件分发序列模型 分发序列 分发模型 事件分发对象及相关方法 源码分析 事件分发总结 一般 ...
最新文章
- C++中MessageBox()的详细用法(转载)
- JAVA构架之并发编程的一些总结
- 研究生就业竞争力弱化 “皇帝女儿也愁嫁”
- Unknown system variable 'query_cache_size'
- mongodb卸载_如何在Windows上安装MongoDB,启动,卸载
- [2018.04.29 T1] 图
- Electron介绍(一)
- dell5580bios恢复出厂_如何进入戴尔笔记本bios及恢复bios出厂设置
- echarts 柱状图颜色设置
- 五金机械行业外贸管理软件解决方案
- 关于视频后期美白的一些事
- 信用社网银服务器无响应,农村信用社网上银行显无法显示网页
- Dell Inspiron15-7567 拆机插放内存条步骤
- 阿里云服务器怎么样?详细介绍
- 指向结构体类型的指针
- python 复制某文件到另一个文件夹
- 莫比乌斯进阶:bzoj 2693 jzptab(Mobius)
- uniapp 调用安卓原生插件 安卓原生又调用了第三方sdk(第三方原生开发的aar怎么转成uni可以使用的aar)
- HUAWEI被google禁完整版Android的背后——中美两国的博弈。
- 网络安全学习(十六)
热门文章
- 实验二 多路复用器的实现-数字电路与逻辑设计-湖南大学课程实验
- window bat脚本 扩充变量 %dp0
- WINCE下的BLENDFUNCTION
- 看小电影没检查是HTTPS的,差点出事...
- Boot Camp 支持软件与系统要求对照表——用于手工下载Window 支持软件(驱动)
- php蜜欧斯,微信公共平台怎么连接数据库,开发语言是PHP
- id 重启event_系统已在未先正常关机的情况下重新启动 事件ID:41
- 图像拼接:matlab自带的拼接示例程序
- Beetle.SL对象封装协议详解
- Peercast简介、分析及常见问题处理 (一)