通过查看源代码可以得出,几种类型的GradientShader类的继承关系是:

SkRefCnt类增加了引用计数的相关操作。它的子类都可以被多次引用,当引用计数为零的时候,自动释放。

Gradient_Shader定义了一些GradientShader效果的公共操作和接口虚函数。

四种类型的GradientShader具体类(Linear_Gradient/Radial_Gradient/Sweep_Gradient/Two_Point_Radial_Gradient)都继承于Gradient_Shader。

---------------------------------------------------

SkGradientShader是一个独立的类,只是封装了四个静态函数,分别调用Linear_Gradient/Radial_Gradient/Sweep_Gradient/Two_Point_Radial_Gradient的构造函数,从而创建具体的GrdientShader对象。相当于为所有的GradientShader创建函数提供了小的命名空间。通过这个命名空间方便的去调用创建函数。

class SK_API SkGradientShader 
{
public:
   
    static SkShader* CreateLinear(  const SkPoint pts[2],
                                    const SkColor colors[], const SkScalar pos[], int count,
                                    SkShader::TileMode mode,
                                    SkUnitMapper* mapper = NULL);

    static SkShader* CreateRadial(  const SkPoint& center, SkScalar radius,
                                    const SkColor colors[], const SkScalar pos[], int count,
                                    SkShader::TileMode mode,
                                    SkUnitMapper* mapper = NULL);

    static SkShader* CreateTwoPointRadial(const SkPoint& start,
                                          SkScalar startRadius,
                                          const SkPoint& end,
                                          SkScalar endRadius,
                                          const SkColor colors[],
                                          const SkScalar pos[], int count,
                                          SkShader::TileMode mode,
                                          SkUnitMapper* mapper = NULL);
  
    static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
                                 const SkColor colors[], const SkScalar pos[],
                                 int count, SkUnitMapper* mapper = NULL);
};

  以Linear_Gradient为例:

SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
                                         const SkColor colors[],
                                         const SkScalar pos[], int colorCount,
                                         SkShader::TileMode mode,
                                         SkUnitMapper* mapper)
{
    if (NULL == pts || NULL == colors || colorCount < 1) 
   {
        return NULL;
    }
    EXPAND_1_COLOR(colorCount);

    return SkNEW_ARGS(Linear_Gradient,
                      (pts, colors, pos, colorCount, mode, mapper));
}

  宏SkNEW_ARGS:

 #define SkNEW_ARGS(type_name, args)     new type_name args

  相当于SkGradientShader::CreateLinear函数返回:

return new Linear_Gradient(pts, colors, pos, colorCount, mode, mapper)

 从而调用:

    Linear_Gradient(const SkPoint pts[2],
                    const SkColor colors[], const SkScalar pos[], int colorCount,
                    SkShader::TileMode mode, SkUnitMapper* mapper)
        : Gradient_Shader(colors, pos, colorCount, mode, mapper),
          fStart(pts[0]),
          fEnd(pts[1])
    {
        pts_to_unit_matrix(pts, &fPtsToUnit);
    }

  

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