函数声明:

在class SK_API SkGradientShader 中。(include\effects\SkGradientShader.h)

    /** Returns a shader that generates a linear gradient between the two
        specified points.
        <p />
        CreateLinear returns a shader with a reference count of 1.
        The caller should decrement the shader's reference count when done with the shader.
        It is an error for count to be < 2.
        @param  pts The start and end points for the gradient.
        @param  colors  The array[count] of colors, to be distributed between the two points
        @param  pos     May be NULL. array[count] of SkScalars, or NULL, of the relative position of
                        each corresponding color in the colors array. If this is NULL,
                        the the colors are distributed evenly between the start and end point.
                        If this is not null, the values must begin with 0, end with 1.0, and
                        intermediate values must be strictly increasing.
        @param  count   Must be >=2. The number of colors (and pos if not NULL) entries. 
        @param  mode    The tiling mode
        @param  mapper  May be NULL. Callback to modify the spread of the colors.
    */
    static SkShader* CreateLinear(  const SkPoint pts[2],
                                    const SkColor colors[], const SkScalar pos[], int count,
                                    SkShader::TileMode mode,
                                    SkUnitMapper* mapper = NULL);

  SkPoint pts[2]; 起始点和结束点,影响了Gradient的起始位置和过渡完 count个颜色需要的像素。

  SkColor colors[] 过渡的颜色表

  SkScalar pos[] 过渡色百分比,必须以0开始,以1.0结束,中间值严格增长

  int count   过渡的颜色数

  SkShader::TileMode mode 绘制范围大于 pts的两个点指定的距离时的重复模式

  SkUnitMapper *mapper ;

  举例一:

  pts 分别为 [0,0]、[50,50], 画 区域SkRect(0,0,100,100),重复模式kMirror_TileMode,颜色表用红绿蓝白黑

  则Gradient过渡完颜色表的颜色时,需要从点(0,0)到点(50,50),Gradient方向是斜向右下45°

  范围(0,0,100,100)超出范围,重复模式是kMirror_TileMode,镜像重复  

  本来是红绿蓝白黑 超出部分挨着就是  黑白蓝绿红 再次 红绿蓝白黑 ……

     

   如果画图的范围小于 (0,0,50,50),就按比例裁剪

  画到(0,0,50,50)是: 画到(0,0,20,50)则是:

  举例二:

  如果pts 起始点不是(0,0),比如(x,y),如果在(x,y)左侧有绘制Gradient,则按照 mode模式重复。

  pts(50,50),(100,50),方向是从左到右,重复模式kMirror_TileMode,颜色表用红绿蓝白黑

  画(0,0,100,100)是从左到右过渡,从起始点的x坐标开始往右50像素是红绿蓝白黑的过渡,

             从起始点的x坐标(50)开始往左,是右边的kMirror_TileMode模式的重复:从坐标50往左是 红绿蓝白黑。

  举例三:

  参数count ,如果小于colors的元素个数,则只取colors的前count个颜色,如果大于,则填充了透明色

  (但是如果count比colors的元素个数多了超过1,比如大3,就填充3个空颜色,但是空颜色之间的过渡有颜色)

  pts (50,50),(100,50);画范围SkRect r = {0,0,SkIntToScalar(200),SkIntToScalar(100)};

  如:SkColor colors[] ={SK_ColorRED,SK_ColorGREEN,SK_ColorBLUE,SK_ColorWHITE,SK_ColorBLACK};

  //红、绿、蓝、白、黑,这些宏的alpha值都是FF,即不透明

  count = SK_ARRAY_COUNT(colors);//5

 ,count =  2:,count =8:

 

  

举例四:

  三种mode的效果图

  

例四代码:

//画到屏幕上
void Flip()
{
	HDC dc = GetDC(g_hWnd);
	SetDIBitsToDevice(dc, 0, 0, 800, 480, 0, 0, 0, 800, pskBitmap->getPixels(), lpbmi, DIB_RGB_COLORS);
	ReleaseDC(g_hWnd,dc);
}


void PaintShader()
{
	SkPaint paint;
	paint.setAntiAlias(true);
	//paint.setStyle(SkPaint::kStroke_Style);
	//paint.setStrokeWidth(SkScalarHalf(SkIntToScalar(3)));
	//paint.setStyle(SkPaint::kFill_Style);
	SkPoint p = SkPoint::Make(50,50);
	SkPoint q = SkPoint::Make(100,50);
	SkPoint pts[] ={p,q};
	SkScalar t,temp,x,y;
	SkColor colors[] ={
		SK_ColorRED,SK_ColorGREEN,SK_ColorBLUE,SK_ColorWHITE,SK_ColorBLACK
	};
	//红、绿、蓝、白、黑,这些宏的alpha值都是FF,即不透明
	SkShader *shader ;
	shader =SkGradientShader::CreateLinear(
		pts,colors,NULL,SK_ARRAY_COUNT(colors),SkShader::kMirror_TileMode);
	//pts决定Gradient的起始点和走向,以及过渡完gClors的颜色需要的像素距离
	paint.setShader(shader);
	shader->unref();
	SkRect r = {0,0,SkIntToScalar(200),SkIntToScalar(100)};
	// 如果r范围小于pts,则只显示完整过渡colors需要像素的一部分,如果大于就会按mode重复
	//kMirror_TileMode就是红绿蓝白黑+白蓝绿红+绿蓝白黑+白...重复
	pskCanvas->drawRect(r,paint);
	
	shader =SkGradientShader::CreateLinear(
		pts,colors,NULL,SK_ARRAY_COUNT(colors),SkShader::kRepeat_TileMode);
	paint.setShader(shader);
	shader->ref();
	r.setLTRB(0,SkIntToScalar(110),SkIntToScalar(200),SkIntToScalar(210));
	pskCanvas->drawRect(r,paint);

	shader =SkGradientShader::CreateLinear(
		pts,colors,NULL,SK_ARRAY_COUNT(colors),SkShader::kClamp_TileMode);
	paint.setShader(shader);
	shader->ref();
	r.setLTRB(0,SkIntToScalar(220),SkIntToScalar(200),SkIntToScalar(320));
	pskCanvas->drawRect(r,paint);


}

void MyLButtonDown(POINT pt)
{
	PaintShader();

	Flip();

}

  举例五:

  关于constSkScalar pos[]参数,以0开始,以1.0结束,中间值严格增长。

  我个人理解:constSkColor colors[]中两个颜色的过渡所占百分比为pos[]中对应两个值的差值

  SkColor colors[] ={SK_ColorRED,SK_ColorGREEN,SK_ColorBLUE,SK_ColorWHITE,SK_ColorBLACK};
  SkScalar scalars[] ={0.0, 0.2, 0.2, 0.3,1.0};

  //红绿过渡占20%,绿蓝没有过渡,蓝白过渡占10%,白黑过渡70%(对应值的差)

  

	SkPoint p = SkPoint::Make(0,50);
	SkPoint q = SkPoint::Make(800,50);
	SkPoint pts[] ={p,q};
	SkColor colors[] ={
		SK_ColorRED,SK_ColorGREEN,SK_ColorBLUE,SK_ColorWHITE,SK_ColorBLACK
	};
	SkScalar scalars[] ={0.0, 0.2, 0.2, 0.3,1.0};
	//红绿过渡占20%,绿蓝没有过渡,蓝白过渡占10%,白黑过渡70%(对应值的差)
	SkShader *shader ;
	shader =SkGradientShader::CreateLinear(
		pts,colors,scalars,SK_ARRAY_COUNT(colors),SkShader::kMirror_TileMode);
	//pts决定Gradient的起始点和走向,以及过渡完gClors的颜色需要的像素距离
	paint.setShader(shader);
	shader->unref();
	SkRect r = {0,0,SkIntToScalar(800),SkIntToScalar(100)};

  pts差值800,就是过渡完5个颜色要800像素, 画宽带为800像素的矩形

  红绿颜色过渡占0.2(0.2-0),有160像素

  绿蓝过渡占0(0.2-0.2)   没有绿蓝过渡区

  蓝白过渡占0.1(0.3-0.2)  有80像素

  白黑过渡占0.7(1.0-0.3)  560像素

效果图:

 

 

ezhong的博客园:http://www.cnblogs.com/ezhong/