I am using “DrawLinearGradient” function to draw gradient line. But the 2 colors that i am using are not dividing equally in the line

江枫思渺然 提交于 2020-01-11 13:45:31

问题


I am using "DrawLinearGradient" function to draw gradient line. But the 2 colors that i am using are not dividing equally in the line.

CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
CGColor[] colors = {UIColor.Red.CGColor,UIColor.Green.CGColor};
float[] locations = {0.0f,0.5f,0.5f,1.0f};

CGGradient gradient = new CGGradient(colorSpace,colors,locations);
ColorMessage.FontSize = width;
context.SetLineWidth(width);
context.SaveState();
context.Clip();

context.DrawLinearGradient(gradient,penVertices[0],penVertices[count-1],0);

context.StrokePath();
gradient.Dispose();
colorSpace.Dispose();
context.RestoreState();

回答1:


I'm assuming that the Mono implementation has the same requirements as the original C implementation.

From the documentation of CGGradientCreateWithColors(colorSpace, colors, locations[]) (that should correspond to new CGGradient(colorSpace,colors,locations); in Mono) you can read

The locations array should contain the same number of items as the colors array.

In your code however you are passing two colors but four locations.

Since you are saying "dividing equally in the line" you should probably repeat both colors twice.

CGColor[] colors = {UIColor.Red.CGColor, UIColor.Red.CGColor, UIColor.Green.CGColor ,UIColor.Green.CGColor};
float[] locations = {0.0f,0.5f,0.5f,1.0f};


来源:https://stackoverflow.com/questions/16033481/i-am-using-drawlineargradient-function-to-draw-gradient-line-but-the-2-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!