如何根据前景图像动态改变android中状态栏的颜色?
我想实现与我们在WhatsApp中看到的功能相同的功能,当看到一个人的个人资料时,状态栏的颜色会根据情况而改变如何根据前景图像动态改变android中状态栏的颜色?,android,android-layout,user-interface,material-design,statusbar,Android,Android Layout,User Interface,Material Design,Statusbar,我想实现与我们在WhatsApp中看到的功能相同的功能,当看到一个人的个人资料时,状态栏的颜色会根据情况而改变 根据图像的颜色。称为Pallete,使用下面的功能,只需传递位图图像即可 private void setUpPalette(Bitmap bitmap) { // you passed your Bitmap image; Palette.from(bitmap). generate(new Palette.PaletteAsyncListe
根据图像的颜色。称为Pallete,使用下面的功能,只需传递位图图像即可
private void setUpPalette(Bitmap bitmap) {
// you passed your Bitmap image;
Palette.from(bitmap).
generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
if (palette != null) {
//default color is yellow
// set the color to toolbar, whatever
int extColor = palette.getVibrantColor(ContextCompat.getColor(MainActivity.this, R.color.yellow));
if (getWindow() != null) {
getWindow().setStatusBarColor(ContextCompat.getColor(this, extColor));
}
} else {
if (getWindow() != null) {
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.fail_safe));
}
}
}
});
}
您将在图像中找到最鲜艳的颜色,您可以更改getWindow.setStatusBarColorgetResources.getColorR.color.color 要获得主色调,您必须使用:
// Generate palette asynchronously and use it on a different
// thread using onGenerated()
public void changeStatusBarColorAsync(Bitmap bitmap) {
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance
Palette.Swatch vibrant = p.getVibrantSwatch();
int color = ContextCompat.getColor(getContext(),R.color.default_title_background);
if(vibrant != null){
color = vibrantSwatch.getTitleTextColor();
}
getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
}
});
}
什么是Whatsapp?我的iphone上没有。我该如何核实你所描述的情况?这就是为什么你应该清楚地提到你现在想要说的,就像我想要XYZ做的一样。检查这个可能重复的extcolor是int,但是getcolor方法需要资源的id,因此它给了我erroris extcolor hex color??extcolor是int,但是getcolor方法需要资源的id,因此它给我errorContextCompat.getColorcontext,id,第一个参数是上下文,第二个是id