【问题标题】:How to change color of status bar dynamically in android based upon the foreground image?如何根据前景图像在android中动态更改状态栏的颜色?
【发布时间】:2019-09-28 06:29:36
【问题描述】:

我想实现与我们在 WhatsApp 中看到的相同的功能,同时看到一个人的个人资料,状态栏的颜色会根据 根据图像的颜色。

【问题讨论】:

标签: android android-layout user-interface material-design statusbar


【解决方案1】:

它叫做 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));
                                                    }
                    }
                }
            });
}

【讨论】:

  • extcolor 是 int 但 getcolor 方法需要资源的 id,因此它给了我错误
  • ContextCompat.getColor(context , id),第一个参数是上下文,第二个是id
【解决方案2】:

您必须使用Palette library 来获得主色:

// 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));
    }


  });
}

【讨论】:

    【解决方案3】:

    https://stackoverflow.com/a/28145358/9186913你会发现图像中最鲜艳的颜色,你可以改变getWindow().setStatusBarColor(getResources().getColor(R.color.color));

    【讨论】:

    • extcolor 是 int 但 getcolor 方法需要资源的 id,因此它给了我错误
    • 是 extcolor 十六进制颜色??
    猜你喜欢
    • 2015-05-09
    • 2013-09-03
    • 2012-02-21
    • 2016-02-25
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    相关资源
    最近更新 更多