0

我正在使用图形画布、颜色和绘画类在 Android 中绘制矩形。如果我将矩形存储在一个数组中,每个矩形都有一种颜色,我将如何让颜色从一端渐变到另一端?我一直在尝试使用 Color.red(colorInt) 等修改 rgb 值,但我得到了一些奇怪的结果。

编辑:每个矩形本身没有渐变。我需要每个都是纯色。我尝试根据矩形在数组中的位置将每个颜色分量乘以权重,但它无效。

这是我尝试过的一些代码。

if(lt != null && rt != null)
{
    int r = (int) ((Color.red(lt.getColor()) * (1.0 - weight)) - (Color.red(rt.getColor()) * weight));
    int g = (int) ((Color.green(lt.getColor()) * (1.0 - weight)) - (Color.green(rt.getColor()) * weight));
    int b = (int) ((Color.blue(lt.getColor()) * (1.0 - weight)) - (Color.blue(rt.getColor()) * weight));

    color = Color.argb(ALPHA, r, g, b);
}
4

1 回答 1

0

这可能不是您问题的确切答案,但可能会帮助您将渐变设置为一个视图:

View yourView = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

yourView .setBackgroundDrawable(gd);

有关GradientDrawable 的更多信息。

于 2013-11-15T09:56:01.437 回答