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

介绍Android Palette类的功能及其使用方法,该类能够从Bitmap中提取多种类型的主导色,包括动感色(Vibrant)和柔和色(Muted)等,并展示了如何在应用中实现颜色提取。

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


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

dependencies{
compile 'com.android.support:palette-v7:23.1.1'
}
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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

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

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

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

下面将按照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());
                }

            }
        });
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值