Palette的简单使用

Palette(调色板)官方文档的描述是:用于从图片中提取颜色的工具类。

可以从图片中提取出多种风格的颜色:

  • Vibrant(充满活力的)
  • Vibrant Dark(充满活力,黑暗的)
  • Vibrant Light(充满活力的,明亮的)
  • Muted(柔和的)
  • Muted Dark(柔和的,黑暗的)
  • Muted Light(柔和的,明亮的)

用来获取 Palette的两种途径:

// 同步:适用于较小尺寸的图片
 Palette p = Palette.from(bitmap).generate();

// 异步:避免阻塞,适用于加载较大尺寸的图片
 Palette.from(bitmap).generate(new PaletteAsyncListener() {
     public void onGenerated(Palette p) {
         // Use generated instance
     }
 });

下面使用的是同步的方式提取颜色:

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //可能会阻塞线程
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.tree);

        Palette palette = Palette.from(bitmap).generate();

        if(palette!=null){


            int rgb_LightMutedColor = palette.getLightMutedColor(Color.BLUE);
            int rgb_DarkMutedColor =  palette.getDarkMutedColor(Color.BLUE);
            int rgb_VibrantColor = palette.getVibrantColor(Color.BLUE);
            int rgb_LightVibrantColor = palette.getLightVibrantColor(Color.BLUE);
            int rgb_DarkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);
            int rgb_MutedColor = palette.getMutedColor(Color.BLUE);

            findViewById(R.id.textview).setBackgroundColor(rgb_LightMutedColor);
            findViewById(R.id.textview1).setBackgroundColor(rgb_VibrantColor);
            findViewById(R.id.textview2).setBackgroundColor(rgb_DarkMutedColor);
            findViewById(R.id.textview3).setBackgroundColor(rgb_LightVibrantColor);
            findViewById(R.id.textview4).setBackgroundColor(rgb_DarkVibrantColor);
            findViewById(R.id.textview5).setBackgroundColor(rgb_MutedColor);


        }else{

            Log.i("Tag","提示:"+"为空");

        }


    }
}

关键的代码其实也就几行,使用起来还是蛮简单的,能有效的提升app的
逼格,配色很重要嘛。

AndroidStudio使用前要添加依赖:

 compile 'com.android.support:palette-v7:23.0.1'

下面附上运行效果图:这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值