WPF中的渐变画刷的一些案例

WPF的渐变画刷,这些画刷改变不同DrawingBrush对象的几何图形,创建渐变为不同形状的平铺模式。

下面是来自一个国外的一些渐变画刷案例:

案例1:定义一个简单的直线,这个几何图形是基于DrawingBrush的扩展,当直线是垂直或水平时,这条线的宽度扩展25个单位。

案例源码:

<!-- ========================================================
      AnimatedDrawingBrush1.xaml (c) 2006 by Charles Petzold
     ======================================================== -->
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Background>
        <DrawingBrush TileMode="FlipXY" 
                      ViewportUnits="Absolute" 
                      Viewport="0 0 25 25">
            <DrawingBrush.Drawing>
                <GeometryDrawing>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M 0 0 L 25 0">
                            <PathGeometry.Transform>
                                <RotateTransform x:Name="xform" />
                            </PathGeometry.Transform>
                        </PathGeometry>
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="1" />
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Page.Background>

    <Page.Triggers>
        <EventTrigger RoutedEvent="Page.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="xform" 
                                     Storyboard.TargetProperty="Angle"
                                     From="0" To="360" 
                                     Duration="0:0:10"
                                     RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Page.Triggers>
</Page>

 

效果如下图:

渐变前效果:

渐变后效果:

 

案例2:定义一个嵌套的视图盒子,这个盒子是使用Viewbox,当盒子的角度改变时,通过会裁剪盒子,这时每个盒子会相邻的盒子融合

案例源码:

<!-- ========================================================
      AnimatedDrawingBrush2.xaml (c) 2006 by Charles Petzold
     ======================================================== -->
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Background>
        <DrawingBrush TileMode="FlipXY" 
                      ViewportUnits="Absolute" Viewport="0 0 30 30"
                      ViewboxUnits="Absolute" Viewbox="-10 -10 20 20" >
            <DrawingBrush.Drawing>
                <GeometryDrawing>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M 0 0 L 10 10 L -10 10 Z">
                            <PathGeometry.Transform>
                                <RotateTransform x:Name="xform" />
                            </PathGeometry.Transform>
                        </PathGeometry>
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="1" />
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Page.Background>

    <Page.Triggers>
        <EventTrigger RoutedEvent="Page.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="xform" 
                                     Storyboard.TargetProperty="Angle"
                                     From="0" To="360" Duration="0:0:10"
                                     RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Page.Triggers>
</Page>


效果图如下:

渐变前的效果:

渐变后的效果:

 

案例3:定义了一个旋转的螺旋,旋转方向是逆时针

案例源码:

<!-- ========================================================
      AnimatedDrawingBrush3.xaml (c) 2006 by Charles Petzold
     ======================================================== -->
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Background>
        <DrawingBrush x:Name="brush" TileMode="Tile" 
                      ViewportUnits="Absolute" Viewport="0 0 20 20"
                      ViewboxUnits="Absolute" Viewbox="-80 -80 160 160" >
            <DrawingBrush.Drawing>
                <GeometryDrawing>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M 0 0 C   5   5,   5   5,   0  10
                                                     C  -5  15, -15  10, -20   0
                                                     C -25 -10, -15 -25,   0 -30
                                                     C  15 -35,  35 -20,  40   0
                                                     C  45  20,  25  45,   0  50
                                                     C -25  55, -55  30, -60   0
                                                     C -65 -30, -35 -65,   0 -70
                                                     C  35 -75,  70 -40,  80   0">
                            <PathGeometry.Transform>
                                <RotateTransform x:Name="xform" />
                            </PathGeometry.Transform>
                        </PathGeometry>
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="5" />
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Page.Background>

    <Page.Triggers>
        <EventTrigger RoutedEvent="Page.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <RectAnimation Storyboard.TargetName="brush" 
                                   Storyboard.TargetProperty="Viewport"
                                   From="0 0 20 20" To="20 20 20 20" Duration="0:0:01"
                                   RepeatBehavior="Forever" />
                    <DoubleAnimation Storyboard.TargetName="xform" 
                                     Storyboard.TargetProperty="Angle"
                                     From="0" To="-360" Duration="0:0:4"
                                     RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Page.Triggers>
</Page>


效果图如下:

 

 

来源网址:http://www.charlespetzold.com/blog/2006/07/230620.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值