Android 用梯度画线

Android 用梯度画线,android,canvas,line,Android,Canvas,Line,我在不同的位置画了多条线。 例如: canvas.drawLine(startXLine1 ,stopXLine1, startYLine1, stopYLine1, paint) canvas.drawLine(startXLine2 ,stopXLine2, startYLine2, stopYLine2, paint) 我希望每条线都有这样的渐变: 当我尝试这个时,我没有这个效果,但是这个方向的渐变是蓝色(左)--->白色(右) 像这样: 有人可以帮我做这件事吗?要像图片一样填充背景:

我在不同的位置画了多条线。 例如:

canvas.drawLine(startXLine1 ,stopXLine1, startYLine1, stopYLine1, paint)
canvas.drawLine(startXLine2 ,stopXLine2, startYLine2, stopYLine2, paint)
我希望每条线都有这样的渐变:

当我尝试这个时,我没有这个效果,但是这个方向的渐变是蓝色(左)--->白色(右) 像这样:


有人可以帮我做这件事吗?

要像图片一样填充背景:

Shader shader = new LinearGradient(0, 0, 0, h /*canvas height*/, res.getColor(R.color.blue),  res.getColor(R.color.white), Shader.TileMode.MIRROR /*or REPEAT*/);

paint.setShader(shader);

它仍然有错误的方向。我希望顶部为蓝色,底部为白色,左侧为蓝色,右侧为白色。我已编辑了答案,请使用更新的建议重试。让我知道:)方向是平等的这对我来说很有用。检查
h
是否不为零,或者将您传递的内容作为coords发布。
Shader shader = new LinearGradient(0, 0, 0, h /*canvas height*/, res.getColor(R.color.blue),  res.getColor(R.color.white), Shader.TileMode.MIRROR /*or REPEAT*/);

paint.setShader(shader);