/%E5%A6%82%E4%BD%95%E5%B0%86%E6%96%87%E5%AD%97%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%AF%B9%E5%BA%94%E7%9A%84PathGeometry.html
PathGeometry 表示更为复杂的由弧线、曲线以及直线段构成的图形,并且可以是闭合的,也可以是不闭合的。 StreamGeometry 相当于是PathGeometry的只读轻量级类。 PathGeometry 前边几种方式都是以WPF内置的几何图形(Geometry)绘制或者组合来定义形状,PathGeometry则提供更小粒度的绘制元素PathSegment,PathSegment <Path Stroke="Blue"> <Path.Data> <PathGeometry> <PathGeometry.Figures> > </PathGeometry> </Path.Data> </Path> StreamGeometry 与PathGeometry类一样,StreamGeometry可以定义包含曲线 与PathGeometry不同的是,StreamGeometry的内容不支持数据绑定、动画或修改。
虽然PathGeometry也能实现基本的几何图形,但是用WPF默认提供的类,则更简单,也方便理解。 关于PathGeometry的示例代码如下所示: <Path Stroke="Black" StrokeThickness="1" > <Path.Data> <PathGeometry PathGeometry pathGeometry = new PathGeometry(); pathGeometry.Figures.Add(pathFigure); // Display the PathGeometry. 与 PathGeometry 不同,StreamGeometry 的内容不支持数据绑定、动画或修改。
Stroke="Black" StrokeThickness="2" Margin="10,10,10,10"> <Path.Data> <PathGeometry > </PathGeometry> </Path.Data> </Path> 其实,可以不加PathGeometry.Figures "Black" StrokeThickness="2" Margin="200,10,10,10"> <Path.Data> <PathGeometry > <PathGeometry.Figures> <PathFigure StartPoint=" > </<em>PathGeometry</em>> </Path.Data> </Path> 如果我们把第一个图,边框变大
Stroke="Black" StrokeThickness="2" Margin="10,10,10,10"> <Path.Data> <PathGeometry > </PathGeometry> </Path.Data> </Path> 其实,可以不加PathGeometry.Figures "Black" StrokeThickness="2" Margin="200,10,10,10"> <Path.Data> <PathGeometry > <PathGeometry.Figures> <PathFigure StartPoint=" > </<em>PathGeometry</em>> </Path.Data> </Path> 如果我们把第一个图,边框变大
常见的Geometry类型有LineGeometry,RectangleGeometry,EllipseGeometry等简单的图形,还可以用PathGeometry创建复杂的图形。 PathGeometry 对象由一个或多个 PathFigure 对象组成;每个 PathFigure 代表不同的“图形”或形状。 <Path Stroke="Black" StrokeThickness="1"> <Path.Data> <PathGeometry> <PathGeometry.Figures > </PathGeometry> </Path.Data> </Path> 上述代码中,Figures作为PathGeometry的默认属性,Segments作为PathFigure的默认属性 ,可以省略,简化后的代码如下所示: <Path Stroke="Black" StrokeThickness="1"> <Path.Data> <PathGeometry>
Windows.UI.Xaml.Shapes.Path path_figure = new Path() { Data = new PathGeometry Stroke = new SolidColorBrush(Colors.Gray) }; 如果觉得这样太快了,我们可以弄个差 PathGeometry path_figure = new PathGeometry(); for (int i = 0; i < point.Count; i++) {
<Path Stroke="Red" StrokeThickness="1" x:Name="p" > <Path.Data> <PathGeometry > <PathGeometry.Figures> <PathFigure> </PathFigure.Segments> </PathFigure> </PathGeometry.Figures > </PathGeometry> </Path.Data> </Path> </Canvas> </UserControl tbRightPoint.SetValue(Canvas.TopProperty, _rightPoint.Y + 10); PathFigure figure = (p.Data as PathGeometry
绘制之前需要拿到底色绘制用的 PathGeometry 对象,如下面代码 // 创建底色几何 var pathGeometry = BuildShapePathGeometry (shapePathList); private PathGeometry BuildShapePathGeometry(ShapePath[] shapePathList) { PathGeometry? GetPathGeometry(ShapePath shapePath) { var pathGeometry = new PathGeometry(); = pathFigureCollection; return pathGeometry; } 获取各段的 Geometry 的画刷,通过 GetShapeFillBrush
StrokeThickness="8" Width="200" Height="200"> <Path.Data> <PathGeometry RotateTransform Angle="45"/> </TransformGroup> </Path.RenderTransform> <Path.Data> <PathGeometry SweepDirection="Clockwise" x:Name="pathArc"/> </PathFigure> </PathGeometry > </Path.RenderTransform> <Path.Data> <PathGeometry SweepDirection="Clockwise" x:Name="pathArc"/> </PathFigure> </PathGeometry
<GeometryGroup> <LineGeometry StartPoint="50,50" EndPoint="200,50"/> <PathGeometry > <PathGeometry.Figures> <PathFigureCollection> </PathFigure> </PathFigureCollection> </PathGeometry.Figures > </PathGeometry> </GeometryGroup> </Path.Data> </Path></Canvas>结果如下图所示
" StrokeThickness="1" UseLayoutRounding="True" VerticalAlignment="Center"> <Path.Data> <PathGeometry > <PathGeometry.Figures> <PathFigure StartPoint="0,0" IsClosed="True"> > </PathGeometry> </Path.Data> </Path> 效果图如下: ? " StrokeThickness="1" UseLayoutRounding="True" VerticalAlignment="Center"> <Path.Data> <PathGeometry > </PathGeometry> </Path.Data> </Path> 效果图如下: ?
<Path Stroke="Black" StrokeThickness="10"> <Path.Data> <PathGeometry > <PathGeometry.Figures> <PathFigure StartPoint=" > </<em>PathGeometry</em>> </Path.Data> </Path> ? StrokeThickness="10"> <Path.Data> <PathGeometry > <PathGeometry.Figures>
#000000", BulgeStyle bulgeStyle = BulgeStyle.Dotted) { var path = new Path(); var pathGeometry = new PathGeometry(); var pathFigure = new PathFigure(); pathFigure.StartPoint = startPoint LineSegment() { Point = relativePoint }); pathFigure.Segments = segmentCollection; pathGeometry.Figures = new PathFigureCollection() { pathFigure }; path.Data = pathGeometry; path.Stroke =
double y) { pf.Segments.Add(new LineSegment(new Point(x, y), true)); } public static PathGeometry GetRoundedRect(Rect rect, double cr) { PathGeometry pg = new PathGeometry(); PathFigure resultGeometry = new PathGeometry(); } 也就是说在 PathGeometry 底层已经判断了此情况,如果有 NaN 的情况就替换为空的 PathGeometry System.Windows.Media.PathGeometry.InternalCombine(System.Windows.Media.Geometry geometry1 = {System.Windows.Media.PathGeometry System.Windows.Media.Geometry.Combine(System.Windows.Media.Geometry geometry1 = {System.Windows.Media.PathGeometry
在和朋友讨论后,我决定尝试使用 ID2D1PathGeometry 来重构绘制逻辑。 ID2D1PathGeometry 允许我们先在内存中构建一个完整的几何路径,然后一次性地将其提交给 GPU 进行绘制。 从代码表面上看,使用 ID2D1PathGeometry 的版本涉及到了更多的 API 调用:CreatePathGeometry、Open、BeginFigure、AddLine、EndFigure、 我曾经误以为,DrawLine 是一个非常底层的、直接的绘制指令,而 ID2D1PathGeometry 是一个更上层、更抽象的封装,性能可能会更差。 而使用 ID2D1PathGeometry 的方案,虽然在 CPU 端看起来代码更复杂,但所有的路径构建工作(AddLine 等)都「只在内存中进行,不涉及与 GPU 的直接交互」。
路径动画 路径动画是一种使用PathGeometry作为输入的动画时间线(AnimationTimeline),可以定义一个几何路径并使用它来设置路径动画的PathGeometry属性,而不是使用From 常见的路径动画如下表所示: 路径动画说明: MatrixAnimationUsingPath 从其 PathGeometry 生成 Matrix 值。 PointAnimationUsingPath 从其 PathGeometry 的 x 和 y 坐标生成 Point 值。 DoubleAnimationUsingPath 从其 PathGeometry 生成 Double 值。 > <PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200
PathFigure>() { pathFigure, }; var pathGeometry = new PathGeometry(pathFigures); var path = new Path() { Fill = item.PieColor, Data = pathGeometry, DataContext = piebase }; _canvas.Children.Add(path); pathDataContext.PolylineEndPoint = point3; return line3; } PathGeometry pathGeometry = geo.GetFlattenedPathGeometry(); return pathGeometry; } } }
PathGeometry: 表示一个可能由弧、曲线、椭圆、直线和矩形组成的基于矢量的复杂形状。 RectangleGeometry: 描述二维矩形这一几何图形。 5.2 PathGeometry 在所有Geometry中PathGeometry是功能最强大的,唯一的缺点是太多复杂。 > <PathGeometry.Figures> <PathFigureCollection> </PathFigure> </PathFigureCollection> </PathGeometry.Figures > </PathGeometry> </GeometryGroup> </Path.Data> </Path> ?
Windows.UI.Xaml.Shapes.Path path_figure = new Path() { Data = new PathGeometry Stroke = new SolidColorBrush(Colors.Gray) }; 如果觉得这样太快了,我们可以弄个差 PathGeometry path_figure = new PathGeometry(); for (int i = 0; i < point.Count; i++) {