【问题标题】:How to change shade of color dynamically android?如何动态改变颜色的阴影android?
【发布时间】:2015-04-25 18:20:03
【问题描述】:

我正在使用 Palette 类以编程方式从图像中获取最主要的颜色,然后我想将其用于状态栏和工具栏。根据材料设计指南,状态栏颜色应该比工具栏颜色深两个色度。

  Bitmap bitmap = ((BitmapDrawable) ((ImageView)mImageView).getDrawable()).getBitmap();
    if (bitmap != null) {
        palette = Palette.generate(bitmap);
        vibrantSwatch = palette.getVibrantSwatch();
        darkVibrantSwatch = palette.getDarkVibrantSwatch();
    }

对于较深的颜色,我使用的是 darkVibrantSwatch,对于较浅的颜色,我使用的是 VibrantSwatch。但在大多数情况下,这些结果彼此非常不同,因此基本上变得无法使用。有什么解决方法吗? 也许如果它可能只得到一种颜色,比如 darkVibrantSwatch,然后以编程方式生成一种颜色浅两个色调的颜色?

【问题讨论】:

    标签: android android-5.0-lollipop material-design palette


    【解决方案1】:

    我不确定是否能准确调亮 2 种色调,但您可以尝试使用 SHADE_FACTOR 看看是否能达到您想要的效果。

      private int getDarkerShade(int color) {
            return Color.rgb((int)(SHADE_FACTOR * Color.red(color)),
                    (int)(SHADE_FACTOR * Color.green(color)),
                    (int)(SHADE_FACTOR * Color.blue(color)));
        }
    

    代码 sn-p 取自here

    【讨论】:

      【解决方案2】:

      一种行之有效的方法是修改颜色的 HSV 表示中的亮度值:

      import android.graphics.Color;
      public static int modifyBrightness(int color, float factor) {
          float[] hsv = new float[3];
          Color.colorToHSV(color, hsv);
          hsv[2] *= factor;
          return Color.HSVToColor(hsv);
      }
      

      要使状态栏的颜色更深,请使用系数 0.8:

      int darkerColor = modifyBrightness(color, 0.8);
      

      【讨论】:

        猜你喜欢
        • 2016-11-28
        • 2017-12-02
        • 2019-06-30
        • 1970-01-01
        • 2018-12-02
        • 2015-05-25
        • 1970-01-01
        • 2021-12-26
        • 2018-02-05
        相关资源
        最近更新 更多