前言
经过这么多年的UI发展,对UI对变换能力要求越来越高,从拟物化到扁平化再到Android到Material Design ,UI越来越友好,丰富而简约。设计是艺术性的,也是需要技术做支撑的 ,更主要的是对用户是友好的,在设计不同质化的前提下,要减少用户的学习成本,尽量设计简洁操作路径流畅,不为花哨而花哨,尽量符合自然。
在有模块或分类呈现时 ,往往我们的设计都会在每个Item的底部加入图片,作为丰富Item的背景图或是表示模块的意象图 。在图片上面,我们往往会有title和description,当图片颜色与文字颜色相冲时,Item上当字就会看不清楚,我们常用的做法是,在文字下方增加一个半透明的黑色背景 。在以前我们或许没辙,但现在,Google为我们提供了一个图片但取色器,我们可以通过palette来获取到ImageView中图片的部分颜色值。
Palette 取色器
引入palette库
compile 'com.android.support:palette-v7:26.0.0-alpha1'
获取ImageView的BitMap
// 拿到ImageView的bitmap
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
使用Palette进行取色
// 进行bitmap色彩分析
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// 得到暗柔和的颜色
int darkMutedColor = palette.getDarkMutedColor(Color.MAGENTA);
// 暗 , 鲜艳
int darkVibrantColor = palette.getDarkVibrantColor(Color.RED);
// ...
// 色值取样
// 暗色 柔和系
// Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch();
// 亮色 柔和系取样
Palette.Swatch lightMutedSwatch = palette.getLightMutedSwatch();
// 得到描述文本颜色取样
int bodyTextColor = lightMutedSwatch.getBodyTextColor();
// 得到title文本颜色取样
int titleTextColor = lightMutedSwatch.getTitleTextColor();
// 得到文本模块背景色取样
int rgb = lightMutedSwatch.getRgb();
// 通过取样色值和透明度,得到颜色值并设置
linearLayout.setBackgroundColor(getTranslucentColor(0.5F,rgb));
desc.setTextColor(bodyTextColor);
title.setTextColor(titleTextColor);
linearLayout.setVisibility(View.VISIBLE);
}
});
/**
* 根据rgb颜色值进行透明度计算
* @param percent
* @param rgb
* @return
*/
private int getTranslucentColor(float percent,int rgb) {
int red = Color.red(rgb);
int green = Color.green(rgb);
int blue = Color.blue(rgb);
int alpha = Color.alpha(rgb);
alpha = Math.round(percent * alpha);
return Color.argb(alpha,red,green,blue);
}
Palette类使用起来非常方便,所提供的选择也比较多,可以获取明暗光,柔和,艳丽等多种组合,也可以使用取样值 ,Google推荐 ,内部通过算法来进行推倒,找到适合的色值来进行组合。
palette.getDarkMutedColor(@ColorInt final int defaultColor); // 暗 ,柔和
palette.getDarkVibrantColor(@ColorInt final int defaultColor); // 暗 ,艳丽
palette.getLightMutedColor(@ColorInt final int defaultColor) // 亮 ,柔和
palette.getLightVibrantColor(@ColorInt final int defaultColor) // 亮 ,艳丽
palette.getMutedColor(@ColorInt final int defaultColor) // 柔和
palette.getVibrantColor(@ColorInt final int defaultColor) // 艳丽
结语
对于这些需要高深算法的库,在不是有特别需求都情况下,一般都不会研究他都源码,只要会用就OK了,毕竟是做应用,每天总有写不完的需求。
网友评论