【问题标题】:SweepGradient ArrayIndexOutOfBoundsExceptionSweepGradient ArrayIndexOutOfBoundsException
【发布时间】:2016-12-13 05:48:07
【问题描述】:

我像这样创建SweepGradient

int[] colors = { Color.RED, Color.BLUE };
// float[] positions = {0,1}; => this will work without error
float[] positions = { 0 , 280f/360 };
SweepGradient gradient = new SweepGradient(width / 2, height / 2, colors, positions);

当我设置float[] positions = {0,1} 时,它会正常工作(没有错误)。
当我更改为float[] positions = { 0 , 280f/360 } 时,在Preview mode of AndroidStudio 中显示渲染错误,但在模拟器中它工作正常 为什么会这样?我该如何解决?

【问题讨论】:

  • 预览不准确,可能渲染不正确。这是一个普遍的问题,并且不会很快解决。忽略预览错误,直到它在设备上运行

标签: android indexoutofboundsexception sweepgradient


【解决方案1】:
    int colors[] = {Color.RED, Color.BLUE, Color.BLUE};
    float positions[] = {0.f, 280f / 360, 1.f};

确保positoins 数组跨越整个范围,即 [0.f, 1.0f]。现在预览也将正确显示。

【讨论】:

  • 我会检查的,但我只想要 2 种颜色而不是 3 种三种颜色。我怎样才能实现它
  • 请注意最后一个颜色与倒数第二个颜色相同。这将显示该范围内的恒定颜色。您能否通过图片分享确切的预期结果?
【解决方案2】:

可能为 NULL。颜色数组中每个对应颜色的相对位置,从0开始到1.0结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2019-06-19
    • 2014-05-19
    • 2013-02-25
    相关资源
    最近更新 更多