使用调色板Palette在背景图中取色

Palette是android.support.v7.graphics包中定义的用于提取背景中的颜色的类,该类用final修饰,不可被继承。


在使用Palette前,需要在build.gradle中加入依赖:

dependencies{
compile 'com.android.support:palette-v7:23.1.1'
}

Palette可提取的颜色按类型可分为以下几种:

  • Vibrant ——动感的
  • Vibrant Dark ——动感的亮
  • Vibrant Light ——动感的暗
  • Muted ——柔和的
  • Muted Dark ——柔和的亮
  • Muted Light ——柔和的暗

Palette采用工厂模式(Builder)创建调色板对象,如下所示:

Palette.Builder builder = Palette.from(BitmapFactory.decodeResource(getResources(), R.mipmap.picture));

下面将按照Palette的取色类型提取相应颜色:

 builder.generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                Palette.Swatch vibrant = palette.getVibrantSwatch();
                if (vibrant != null) {
                    TextView textView = (TextView) findViewById(R.id.vibrant);
                    textView.setBackgroundColor(vibrant.getBodyTextColor());
                }

                Palette.Swatch vibrantlight = palette.getLightVibrantSwatch();
                if (vibrantlight != null) {
                    TextView textView = (TextView) findViewById(R.id.vibrant_light);
                    textView.setBackgroundColor(vibrantlight.getBodyTextColor());
                }

                Palette.Swatch vibrantdark = palette.getDarkVibrantSwatch();
                if (vibrantdark != null) {
                    TextView textView = (TextView) findViewById(R.id.vibrant_dark);
                    textView.setBackgroundColor(vibrantdark.getBodyTextColor());
                }


                Palette.Swatch muted = palette.getMutedSwatch();
                if (muted != null) {
                    TextView textView = (TextView) findViewById(R.id.muted);
                    textView.setBackgroundColor(muted.getBodyTextColor());
                }


                Palette.Swatch mutedDark = palette.getDarkMutedSwatch();
                if (mutedDark != null) {
                    TextView textView = (TextView) findViewById(R.id.muted_dark);
                    textView.setBackgroundColor(mutedDark.getBodyTextColor());
                }


                Palette.Swatch mutedLight = palette.getLightMutedSwatch();
                if (mutedLight != null) {
                    TextView textView = (TextView) findViewById(R.id.muted_light);
                    textView.setBackgroundColor(mutedLight.getBodyTextColor());
                }

            }
        });

展示的图片及提取的颜色样本(左下角依次排列)见下图:


Palette
从左至右依次为Vibrant (动感的)、Vibrant Dark (动感的亮)、Vibrant Light (动感的暗)、Muted (柔和的)、Muted Dark (柔和的亮)、Muted Light (柔和的暗)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值