android5.x之Palette调色板

Palette类可以分析一张图片,取出这张图片的特征色,然后为View中的文字,背景等设置颜色,让整个界面在色调上看上去更和谐更美观。

使用Palette,首先要导入sdk\extras\android\support\v7\palette

[img]http://dl2.iteye.com/upload/attachment/0110/2294/c42a7eaf-059c-361a-88ab-c39d995101cb.png[/img]

然后在你的工程中引入Palette工程

[img]http://dl2.iteye.com/upload/attachment/0110/2296/81a30354-f8a5-322b-8350-8bd051029dfd.png[/img]


接下来就可以使用了

public class MainActivity extends Activity {

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

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.gg1);

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

//暗鲜艳色
int darkVibrantColor=palette.getDarkVibrantColor(android.R.color.holo_blue_dark);
//暗柔和的颜色
int darkMutedColor=palette.getDarkMutedColor(android.R.color.holo_orange_dark);

//亮鲜艳色(淡色)
int lightVibrantColor=palette.getLightVibrantColor(android.R.color.holo_blue_bright);
//亮柔和色(淡色)
int lightMutedColor=palette.getLightMutedColor(android.R.color.holo_orange_light);

//柔和色
int mutedColor=palette.getMutedColor(android.R.color.holo_red_dark);
//鲜艳色
int vibrantColor=palette.getVibrantColor(android.R.color.holo_red_light);

final TextView tv_0 = (TextView) findViewById(R.id.tv_0);
final TextView tv_1 = (TextView) findViewById(R.id.tv_1);
final TextView tv_2 = (TextView) findViewById(R.id.tv_2);
tv_0.setBackgroundColor(darkVibrantColor);
tv_0.setTextColor(lightVibrantColor);
tv_1.setBackgroundColor(darkMutedColor);
tv_1.setTextColor(lightMutedColor);
tv_2.setBackgroundColor(mutedColor);
tv_2.setTextColor(vibrantColor);
}

}


效果图:
[img]http://dl2.iteye.com/upload/attachment/0110/2298/bb4492ce-f60e-3ad5-b700-b618af37860b.png[/img]

Palette还有个异步的方法:

Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {

@Override
public void onGenerated(Palette palette) {
// TODO Auto-generated method stub

}
});


PS:不同版本的Palette可能用法不同,我用的是最新的Android Support Library 22.2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值