如何动态更改颜色阴影安卓



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

  Bitmap bitmap = ((BitmapDrawable) ((ImageView)mImageView).getDrawable()).getBitmap();
    if (bitmap != null) {
        palette = Palette.generate(bitmap);
        vibrantSwatch = palette.getVibrantSwatch();
        darkVibrantSwatch = palette.getDarkVibrantSwatch();
    }
对于较深的颜色,我使用darkVibrantSwatch,

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

我不确定是否要让 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)));
    }

从这里获取的代码片段

一种行之有效的方法是修改颜色的 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);

最新更新