WPF:Graphics图画-Brushes画刷-Brushes画刷集合(4)
文章目录
GradientExample渐变示例
关注点:
- 虚线的动画效果
- 线性或径向渐变的方向及范围变化效果
虚线显示在跑动效果情况:
- 在设置线条Line的虚线属性Line.StrokeDashArray设置好下,进行虚线样式内虚线开始处的距离StrokeDashOffset的动画设置
- 取消虚线等显示只需设置所有的Line、Path的不透明度Opacity为0即可
以下为轴线的样式(虚线动画):
<Style x:Key="AxisMarkerStyle">
<Setter Property="Line.StrokeThickness" Value="2"/>
<Setter Property="Line.Stroke" Value="Black"/>
<Setter Property="Line.StrokeDashArray" Value="2,1"/>
<Setter Property="Line.StrokeEndLineCap" Value="Flat"/>
<Setter Property="Line.StrokeStartLineCap" Value="Flat"/>
<Style.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="StrokeDashOffset"
From="3" To="0" Duration="0:0:1" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
图中上3的代码:
- 设置线变画刷起始点/终点为0.25,0.5至0.75,0.5
- 设置Line点与样式
- 设置线上的小圆点Path
<Canvas Grid.Row="3" Grid.Column="4" Width="200" Height="100">
<!-- This rectangle is painted with a horizontal linear gradient.
Because the StartPoint is (0.25,0.5) and the EndPoint is (0.75,0.5),
the gradient doesn't completely fill the output area. The remaining
portion of the rectangle is filled based on the brush's SpreadMethod setting.
In this case, the SpreadMethod isn't specified, so the brush defaults to
Pad. -->
<Rectangle Width="200" Height="100">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.25,0.5" EndPoint="0.75,0.5">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<!-- Highlights the position of the brush's gradient stops. -->
<Line X1="49" Y1="50" X2="149" Y2="50" Style="{StaticResource AxisMarkerStyle}" Name="gradientLine3" />
<Path Style="{StaticResource GradientMarkerStyle}" Name="gradientPath3">
<Path.Data>
<GeometryGroup>
<EllipseGeometry Center="50,50" RadiusX="4" RadiusY="4" />
<EllipseGeometry Center="75,50" RadiusX="4" RadiusY="4" />
<EllipseGeometry Center="125,50" RadiusX="4" RadiusY="4" />
<EllipseGeometry Center="150,50" RadiusX="4" RadiusY="4" />
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
Path样式:设置透明填充、边界颜色及宽度
<Style x:Key="GradientMarkerStyle">
<Setter Property="Path.Fill" Value="#99FFFFFF"/>
<Setter Property="Path.Stroke" Value="Black"/>
<Setter Property="Path.StrokeThickness" Value="2"/>
</Style>
下图2图像的代码参数改变:
以下图中的径向渐变的中心点及最外层圆的中心为GradientOrigin="0,0.5" Center="0.5,0.5"
最外层圆的X/Y向半径大小为RadiusX="0.5" RadiusY="0.5"
<Canvas Grid.Row="5" Grid.Column="2" Width="200" Height="100">
<!-- This rectangle is painted with a radial gradient. -->
<Rectangle Width="200" Height="100">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="0,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<!-- Highlights the position of the brush's gradient stops. -->
<Line X1="101" Y1="50" X2="199" Y2="50" Style="{StaticResource AxisMarkerStyle}" Name="gradientLine5" />
<Path Style="{StaticResource GradientMarkerStyle}" Name="gradientPath5">
<Path.Data>
<GeometryGroup>
<EllipseGeometry Center="100,50" RadiusX="4" RadiusY="4" />
<EllipseGeometry Center="125,50" RadiusX="4" RadiusY="4" />
<EllipseGeometry Center="175,50" RadiusX="4" RadiusY="4" />
<EllipseGeometry Center="200,50" RadiusX="4" RadiusY="4" />
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
另见图参数形成的放射径向渐变效果:
示例使用默认坐标系来设置控制点。 默认坐标系是相对于边界框的:0 表示边界框的 0%,1 表示边界框的 100%。 可以通过将 MappingMode 属性设置为值 Absolute,更改此坐标系。 绝对坐标系与边界框不相关。 值直接在本地坐标系中解释。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论