MaterialDesign之Palette的使用
2023年7月13日发(作者:)
MaterialDesign之Palette的使⽤Palette,即调⾊板, 作⽤是从图⽚中提取相应的⾊调,然后把这些提取的⾊调赋给相应的组件,使界⾯看起来更加优雅.⼀、Palette的使⽤1、添加依赖compile 't:palette-v7:25.3.1'2、⽣成Palette对象同步:r bulider = (bitmap);Palette palette = te();异步: r bulider = (bitmap); te(new eAsyncListener() { @Override public void onGenerated(Palette palette) { } });异步获取到的Palette对象会回调在 onGenerated⽅法的参数中这⾥需要⼀个Bitmap对象,即我们要提取颜⾊图⽚的位图对象.如果图⽚过⼤时,可能会阻塞主线程,所以更倾向于使⽤异步⽅法.3、获取样本这⾥可以从位图中获取6种⾊调的样本 s1 = rantSwatch();//充满活⼒的样本 s2 = kVibrantSwatch(); //充满活⼒的暗⾊样本 s3 = htVibrantSwatch(); //充满活⼒的亮⾊样本 s4 = edSwatch(); //柔和的样本 s5 = kMutedSwatch(); //柔和的暗⾊样本 s6 = htMutedSwatch(); //柔和的亮⾊样本具体使⽤哪种样本根据实际需求.这⾥需要注意的是,有时候我们会获取不到对象,返回⼀个null值,所以实际使⽤时需要判断是否为null.4、样本中提取信息返回该样本的像素的总数int population = ulation();返回⼀个 HSL颜⾊值float[] hsl = ();返回⼀个 RGB 颜⾊值int rgb = ();返回⼀个适合做内容体颜⾊的颜⾊值int bodyTextColor = yTextColor();返回⼀个适合做标题颜⾊的颜⾊值int titleTextColor = leTextColor();5、过时⽅法早期是直接通过generate( )⽣成Palette对象的同步:Palette p = te(Bitmap bitmap);Palette p = te(Bitmap bitmap, int numColors);异步:teAsync(bitmap, new eAsyncListener() { @Override public void onGenerated(Palette palette) { }});teAsync(bitmap, 24, new eAsyncListener() { @Override public void onGenerated(Palette palette) { }});
这⾥可以设置palette的numColors,numColors越⼤,会增加计算的时间,⽽越⼩,可以选择的⾊彩也越⼩,不设置默认是16.⼆、Demo⼆、Demo布局⽂件 Activity中public class MainActivity extends AppCompatActivity { private TextView tv1; private TextView tv2; private TextView tv3; private TextView tv4; private TextView tv5; private TextView tv6; private Button bt1; private Button bt2; private ImageView iv; @Override public void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main); tv1 = (TextView) findViewById(1); tv2 = (TextView) findViewById(2); tv3 = (TextView) findViewById(3); tv4 = (TextView) findViewById(4); tv5 = (TextView) findViewById(5); tv6 = (TextView) findViewById(6); bt1 = (Button) findViewById(1); bt2 = (Button) findViewById(2); iv = (ImageView) findViewById(); lickListener(new kListener() { @Override public void onClick(View v) { changBg(); } }); lickListener(new kListener() { @Override public void onClick(View v) { changBg(); } }); } private void changBg(int picId) { geResource(picId); Bitmap bitmap = Resource(getResources(), picId); r bulider = (bitmap); Palette palette = te(); s1 = rantSwatch(); s2 = kVibrantSwatch(); s3 = htVibrantSwatch(); s4 = edSwatch(); s5 = kMutedSwatch(); s6 = htMutedSwatch(); if (s1 != null) { //充满活⼒的样本 int rgb = (); kgroundColor(rgb); } if (s2 != null) { //充满活⼒的暗⾊样本 int rgb = (); kgroundColor(rgb); kgroundColor(rgb); } if (s3 != null) { //充满活⼒的亮⾊样本 int rgb = (); kgroundColor(rgb); } if (s4 != null) { //柔和的样本 int rgb = (); kgroundColor(rgb); } if (s5 != null) { //柔和的暗⾊样本 int rgb = (); kgroundColor(rgb); } if (s6 != null) { //柔和的亮⾊样本 int rgb = (); kgroundColor(rgb); } }}效果图
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689217825a222502.html
评论列表(0条)