Shader SweepGradient positions参数用法

之前有一个项目需要这样的一个控件:

215239_60X6_2555140.png


自然想到用SweepGradient来渲染这个半环,参考了下官方的ColorPickerDemo,发现其中是一个完整的圆环,并不需要指定SweepGradient 的第四个参数positions.

于是查阅了API和stackoverflow,弄清了positions的用法

public SweepGradient (float cx, float cy, int[] colors, float[] positions)

Parameters

cx float: The x-coordinate of the center   圆心的X坐标

cy float: The y-coordinate of the center   圆心的Y坐标

colors int: The colors to be distributed between around the center. There must be at least 2 colors in the array.

                着色器的颜色数组

positions float: May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.

                positions数组指定颜色在圆上的渲染分布,守护彩虹的任务就交给它了。

假如要绘制完整的圆,positions指定为null就可以了,默认会按照颜色数组的顺序帮你渲染。

假如是像我要做的控件,是有缺口的圆,则需要我们去指定各个颜色的位置。

渲染的方向是顺时针,位置从0开始,到1结束,而且数组要呈单调递增,不然可能会有可怕的后果。。

所以看回上面的控件,颜色数组为{"绿色","黄色","红色","紫色","蓝色"},

绿色在180度,所以他对应的位置参数是0.5 (顺时针算)

以此类推,黄色 225度,0.625

                 红色 270度,0.75

                 紫色 315度,0.875

                 蓝色 360度,1

所以对应的positions数组就是{ 0.5f, 0.625f, 0.75f, 0.875f, 1f }

至此,着色器设置完毕


转载于:https://my.oschina.net/u/2555140/blog/631847

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值