zoukankan      html  css  js  c++  java
  • [Aaronyang] 写给自己的WPF4.5 笔记19[Visual类图文并茂讲解]

    文章虽小,内容还好,且看且珍惜。 aaronyang版权所有,不许转载,违者必究

    当界面上使用数千个矢量图形,例如实时统计图,粒子碰撞,比如超级玛丽游戏,图像一直在绘,过量的使用WPF的元素系统和Shape类会使用程序变慢,所以我们需要使用Visual类手动进行渲染。

    Visual类是很多WPF元素的父类。所以掌握它当然很重要了。

    Visual的开销小于Geometry小于Path


    Visual作为抽象类,有UIElement这个子类,也有Viewport3DVisual类(3D知识中的)

    放置Visual的对象的容器:ContainerVisual类,继承该类的子类有DrawingVisual类。

    DrawingVisual.RenderOpen()会返回个可用于定义可视化内容的DrawingContext对象。

    DrawingContext类使用完要Close,类似文件流读写完要close,所以你可以使用using关键字了,使用完自动释放对象。

    有了DrawingContext后就可以很轻松的开始绘制你想要的图形了。

    除了基本的DrawLine,DrawRectangle,DrawRoundedRectangle,DrawEllipse,当然还有上一篇博客说到的DrawGeometry,还有DrawDrawing来放置Geometry,Drawing对象。

    同样的类似winform的验证码绘制代码,DrawText,DrawImage(例如做水印),DrawVedio。

    还有些方法可能第一次接触,例如Pop()撤销上一次Push操作,PushClip(),PushEffect(),PushOpacity(),PushOpacityMask(),PushTransform()等

    文章内容已经迁移到:http://www.ayjs.net/2015/03/59/

    ImageBrush还有TileMode就是平铺方式,除了百分比的区域截图,还有绝对尺寸的区域截图ViewportUnits

    还有个VisualBrush,做复制样子的。比如应用,做倒影,指定Visual的属性就可以了,倒影,然后你再旋转图形,然后设置OpacityMask

      <local2:AyImage4Button x:Name="CloseBtn" Width="32" Height="24" Margin="605,8,0,17"
                                    HorizontalAlignment="Left" VerticalAlignment="Center" Icon="close.png"
                                />
            <Rectangle Margin="605,48,155,5" Width="32" Height="24">
                <Rectangle.Fill>
                    <VisualBrush Visual="{Binding ElementName=CloseBtn}"/>
                </Rectangle.Fill>
            </Rectangle>

    还有个画刷叫BitmapCacheBrush,用法类似VisualBrush,设置Rectangle的Fill的Brush为BitmapCacheBrush,然后使用Target替代Visual,然后指定BitmapCache为,例如BitmapCache使用位图缓存,当然第一次缓存就会有一些延迟了。这里暂时不讲了。比如一个小球在运动,背景是死的,球在背景上动,那么如果背景不是缓存的,那么背景将会一直刷新,就会很费内存,此时的画刷就可以使用BitmapCacheBrush了,它的好处不言而喻了。

    接下来,我需要讲一下RenderTransform和LayoutTransform的区别。

    LayoutTransform是在转换操作之前就计算好布局了。而RenderTransform是在呈现时候,例如,两个旋转的按钮

     <StackPanel  Margin="25"  Background="LightYellow">
          <Button Padding="5" HorizontalAlignment="Left">
            <Button.RenderTransform>
              <RotateTransform Angle="35" CenterX="45" CenterY="5" />
            </Button.RenderTransform>
            <Button.Content>I'm rotated 35 degrees</Button.Content>
          </Button>
          <Button Padding="5" HorizontalAlignment="Left">I'm not</Button>
        </StackPanel>
    
        <StackPanel  Margin="25"  Background="LightYellow">
          <Button Padding="5" HorizontalAlignment="Left">
            <Button.LayoutTransform>
              <RotateTransform Angle="35" CenterX="45" CenterY="5" />
            </Button.LayoutTransform>
            <Button.Content>I'm rotated 35 degrees</Button.Content>
          </Button>
          <Button Padding="5" HorizontalAlignment="Left">I'm not</Button>
        </StackPanel>

    可以看出区别了吧。有时候如果变化的时候发现不对,你就换一下转换布局的方式     o(∩_∩)o 哈哈

    关于动画一章,我为什么还不讲,原因是,Blend中基本的太容易做了,建议大家还是试试,前台代码转后台代码的写法试试。练习,因为我的一些想要的动画都是先用blend写好动画草稿,然后自己在后台转换为后台代码去是实现的,例如那个环状图AyArcChart。

    好了,从下篇开始,我就要一直讲3d的知识了,当然也会有很炫的3d的窗口动画。

           =============潇洒的版权线==========www.ayjs.net===== Aaronyang ========= AY =========== 安徽 六安 杨洋 ==========   未经允许不许转载 =========

           -------------------小小的推荐,作者的肯定,读者的支持。推不推荐不重要,重要的是希望大家能把WPF推广出去,别让这么好的技术消失了,求求了,让我们为WPF技术做一份贡献。-----------------

  • 相关阅读:
    一个好的时间函数
    Codeforces 785E. Anton and Permutation
    Codeforces 785 D. Anton and School
    Codeforces 510 E. Fox And Dinner
    Codeforces 242 E. XOR on Segment
    Codeforces 629 E. Famil Door and Roads
    Codeforces 600E. Lomsat gelral(Dsu on tree学习)
    Codeforces 438D The Child and Sequence
    Codeforces 729E Subordinates
    【ATcoder】D
  • 原文地址:https://www.cnblogs.com/AaronYang/p/4335570.html
Copyright © 2011-2022 走看看