使用tPath的FireMonkey绘图不会填充形状

时间:2017-05-23 12:26:10

标签: drawing firemonkey shape

我想使用TPathData绘制形状并用任意颜色填充它们。我正在使用以下代码,在Button1Click,我从Embarcadero文档的示例中提取:

procedure TformPathDrawing.Button1Click(Sender: TObject);
var path: TPathData;
begin
  Image1.Bitmap.Canvas.Fill.Color := TAlphaColorRec.Blue;
  path      := TPathData.Create;
  path.Data := 'M 01,00 L 02,01 L 01,02 L 00,01 L 01,00';
  Image1.Bitmap.Clear ($FFFFFF);
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillPath (path, 200);
  Image1.Bitmap.Canvas.EndScene;
end;

执行此代码时,正如预期的那样,渲染了一个romboid,但它没有填充第一个命令中的颜色集。谁知道什么是错的?感谢。

3 个答案:

答案 0 :(得分:0)

这是正确的代码。您可以在FMX.Objects.pas程序TCustomPath.UpdateCurrent和其他中找到其他详细信息;或者只是调试TPath可视组件。

     {aPath - is vector path of image;
     aStretch - how to draw vector data - true - stretch draw, false = fit to bitmap
     aBitmap - must be created. In bitmap you should specify:
     Width, Height, Bitmap.Canvas.Fill.Color, Bitmap.Canvas.Stroke.Color
     and, if you need, another drawing stuff like Gradient, stroke thikness, 
     background texture) }

procedure TForm1.DrawVectorPath(const aPath: string; aBitmap: TBitmap; aStretch: boolean);
var
  vPath: TPathData;
  R: TRectF;
begin
  Assert(aBitmap <> nil);
  vPath := TPathData.Create;
  try
    vPath.Data := aPath;
    aBitmap.Clear($FFFFFF);
    if aStretch then
    begin
      R := vPath.GetBounds;
      vPath.Translate(-R.Left, -R.Top);
      vPath.Scale(aBitmap.Width / R.Width, aBitmap.Height / R.Height);
    end
    else // Fit image
    begin
      R := TRect.Create(0, 0, aBitmap.Width, aBitmap.Height);
      vPath.FitToRect(R);
    end;

    aBitmap.Canvas.BeginScene;
    aBitmap.Canvas.FillPath(vPath, 1);
    aBitmap.Canvas.DrawPath(vPath, 1);
    aBitmap.Canvas.EndScene;
  finally
    vPath.Free;
  end;
end;

答案 1 :(得分:0)

遵循有效的代码(在我的情况下):

procedure TformPathDrawing.Button1Click(Sender: TObject);
begin
Image1.Bitmap.Canvas.Fill.Color := TAlphaColorRec.Blue;
path.Clear;
path.Data := 'M 01,00 L 02,01 L 01,02 L 00,01 L 01,00';
Image1.Bitmap.Canvas.BeginScene;
Image1.Bitmap.Canvas.FillPath (path, 1);
Image1.Bitmap.Canvas.EndScene;
end;

在我的代码中,变量path是在绘制代码之外创建的。由于path.Data;是附加的,因此在添加当前path.Data;之前,必须放置path.Clear;语句,以清除点数组中剩余的内容。希望这有助于其他人。

答案 2 :(得分:-1)

因为评论不能包含代码(至少没有格式化)。这是我的代码,有效。

procedure TForm7.RadioButton6Click(Sender: TObject);
var path: TPathData;
begin
  Image1.Bitmap.Canvas.Fill.Color := TAlphaColorRec.Blue;
  Image1.Bitmap.Canvas.Stroke.Color := TAlphaColorRec.black;
  path      := TPathData.Create;
  path.Data := 'M 01,00 L 20,01 L 10,20 L 00,10 L 01,00';
  Image1.Bitmap.Clear ($FFFFFF);
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillPath (path, 200);
  Image1.Bitmap.Canvas.EndScene;
end;

请注意,也会设置笔触颜色。这是我能在这里找到的唯一区别。