美文网首页Android UI术与道
AndroidUI初探⑤Palette调色板

AndroidUI初探⑤Palette调色板

作者: 逝我 | 来源:发表于2017-05-14 22:20 被阅读87次

前言

经过这么多年的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了,毕竟是做应用,每天总有写不完的需求。

相关文章

  • AndroidUI初探⑤Palette调色板

    前言 经过这么多年的UI发展,对UI对变换能力要求越来越高,从拟物化到扁平化再到Android到Material ...

  • 主题颜色提取 ——— Palette

    Palette 调色板 Palette 是 Android L SDK 中的新特性。可以使用 Palette 从图...

  • Material design - Style

    Color - 颜色 Color palette - 调色板 Material takes cues from c...

  • Android Palette实现原理

    Palette介绍和用法 Palette是调色板,可以用来获取一张Bitmap的主色调,使用方式如下: 其中max...

  • Palette(调色板)

    利用Palette库来取得图片中的主要色彩 使用这个Android的开源库android-support-v7-p...

  • Palette

    Palette 单词本意是调色板的意思,所以在Android中Palette肯定会与颜色有关,它在作用是根据当前图...

  • python-seaborn

    导入相关库 主题颜色 color_palette 调色板 折线图 盒图

  • Palette

    今天听了一首歌叫《Palette》,单曲循环到现在。 Palette是调色板之意,歌词像写给IU25岁的她自...

  • MaterialDesign学习篇(六),调色板Palette的

    什么是Palette Palette的意思是调色板,它的作用是从图像中提取出突出的颜色,这样我们可以将提取出来的颜...

  • Seaborn 颜色画板

    怎样调用调色板? color_palette()能传入任何Matplotlib所支持的颜色 color_palet...

网友评论

    本文标题:AndroidUI初探⑤Palette调色板

    本文链接:https://www.haomeiwen.com/subject/ffqcxxtx.html