tips:M D主题 Theme.Material(dark) Theme.Material.Light(light) Theme.Material.Light.DarkActionBar
1.Palette实例化
// 同步
Palette p = Palette.from(bitmap).generate();
// 异步(建议)
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance } });
2.使用Palette提取颜色
getVibrantSwatch()->充满活力的
getDarkVibrantSwatch()->充满活力的黑
getLightVibrantSwatch()->充满活力的亮色
getMutedSwatch()->柔和的
getDarkMutedSwatch()->柔和的黑
getLightMutedSwatch()->柔和的亮色
3.从色样中提取相应颜色
getPopulation() 样本中的像素数量
getRgb() 颜色的RGB值
getHsl() 颜色的HSL值
getBodyTextColor() 主体文字的颜色值
getTitleTextColor() 标题文字的颜色值
练习:
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.ColorDrawable; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.graphics.Palette; import android.view.Window; import android.widget.TextView; 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.mipmap.robot); final TextView tv=findViewById(R.id.textView); //异步处理 Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public void onGenerated(@NonNull Palette p) { Palette.Swatch swatch=p.getLightMutedSwatch(); if (swatch!=null){ tv.setBackgroundColor(swatch.getRgb()); Window window=getWindow(); window.setStatusBarColor(swatch.getRgb()); tv.setTextColor(swatch.getTitleTextColor()); } } }); } }备注:https://blog.youkuaiyun.com/LABLENET/article/details/52340634(补充学习)