zoukankan      html  css  js  c++  java
  • silverlight 中绘制扇形(前台+后台)

    以Y轴正方向为0度,顺时针递增,我们来做一个40度角的扇形,对称轴是Y轴

     

    前台:利用Blend

    1、按住shift画出一个圆形,去掉生成的Ellipse对象的Margin、Stroke属性,添加 Width, Height属性值(目的为了能比较准确的切割)。

    1. <Ellipse Fill="#FFF4F4F5" Width="200" Height="200"/>  

    2、在画一个矩形,去掉生成的Rectangle对象的VerticalAlignment属性,修改Height属性为圆形半径大小,修改Margin属性为距离顶部圆形半径的距离。

    1. <Rectangle Fill="#FFF4F4F5"  Height="100" Margin="0,100,0,0"/>  

    3、先选择圆形再选择矩形后右键->合并->相减,就可以得到一个半圆。

    4、在做一个相同的矩形,在其转换属性里设置中心点中上(0.5,0),在设置其旋转70度(Y轴左右个20度),重复3的动作,得到一个扇形。

    5、重复4的动作,只是旋转角度写-70或是110度,得到了最终的40度扇形。

    PS:最后获得的是一个Path对象,

    1. <Path Data="M34.209873,0 C44.565228,1.2682605E-06 54.552952,1.5740005 63.946861,4.4958038 L68.419853,6.0092916 L34.210033,100 L0,6.0092602 L4.4728832,4.4958038 C13.866797,1.5740012 23.854521,2.2721511E-06 34.209873,0 z" Fill="#FFF4F4F5" Height="100" Margin="165.79,50,165.79,0" Stretch="Fill" UseLayoutRounding="False" VerticalAlignment="Top"/>  

    后台:

    方法1:这种方法就是通过XamlReader读取上面Blend画出的Path对象的XAML标识获得Path(扇形)。

    [csharp] view plaincopy
    1. <span style="font-size:12px;color:#000000;">string xaml = "<Path ";  
    2. //引用    
    3. xaml += " xmlns=\"http://schemas.microsoft.com/client/2007\"";  
    4. //创建属性    
    5. xaml += " Fill=\"#AE1E5E19\" Height=\"20\" RenderTransformOrigin=\"0.5,1\" Stretch=\"Uniform\"";  
    6. xaml += string.Format(" Data=\"{0}\" />""M34.209953,0 C44.565323,3.5734599E-06 54.55307,1.5740019 63.946991,4.4958062 L68.419983,6.0092931 L34.210087,100 L0,6.0092659 L4.4728961,4.4958062 C13.86683,1.5739993 23.854574,3.1432953E-07 34.209953,0 z");  
    7. //创建路径对象    
    8. Path path;  
    9. //<Path x:Name="ArcPath" Data=/>  
    10. path = (Path)XamlReader.Load(xaml);</span>  


     

    方法2:通过C代码生成,要根据角度计算偏移值。

    [csharp] view plaincopy
    1. public class BaseStation  
    2.     {  
    3.         /// <summary>  
    4.         /// 扇形的半径大小,更改其值会影响扇形大小。  
    5.         /// </summary>  
    6.         private const int Radius = 10;  
    7.   
    8.         public static Path GetBaseStation(double rotationAngle)  
    9.         {  
    10.             var path = new Path  
    11.                            {  
    12.                                Fill = new SolidColorBrush(Colors.Green),  
    13.                                //以扇形的两直线连接顶点为几何中心进行定位和旋转。  
    14.                                RenderTransformOrigin = new Point(0.5, 1),  
    15.                                Stretch = Stretch.None,  
    16.                                UseLayoutRounding = false  
    17.                            };  
    18.             //以几何中心为准直线顶点的偏移值。  
    19.             var offsetx = Radius * Math.Cos(20);  
    20.             var offsety = Radius * Math.Sin(20);  
    21.   
    22.             //绘制起始直线  
    23.             var startLine = new LineSegment()  
    24.                                 {  
    25.                                     Point = new Point(0 - offsetx, 0 - offsety)  
    26.                                 };  
    27.             //绘制弧线,一条三次方贝塞尔曲线,Point1起点,Point2为峰值点,Point3为终点。  
    28.             var arcLine = new BezierSegment()  
    29.                               {  
    30.                                   Point1 = new Point(0 - offsetx, 0 - offsety),  
    31.                                   Point2 = new Point(0, 0 - Radius),  
    32.                                   Point3 = new Point(0 + offsetx, 0 - offsety)  
    33.                               };  
    34.             //绘制结束直线  
    35.             var endLine = new LineSegment()  
    36.                               {  
    37.                                   Point = new Point(0, 0)  
    38.                               };  
    39.             //把三条线段集合在一起。  
    40.             var segments = new PathSegmentCollection { startLine, arcLine, endLine };  
    41.   
    42.             //为直线的Data赋值,由三段封闭的线段绘制成一个扇形  
    43.             path.Data = new PathGeometry()  
    44.             {  
    45.                 Figures = new PathFigureCollection() { new PathFigure()  
    46.                                                            {  
    47.                                                                StartPoint = new Point(0, 0),  
    48.                                                                Segments = segments  
    49.                                                            }}  
    50.             };  
    51.   
    52.             //设置扇形对称轴偏转角度。  
    53.             path.RenderTransform = new CompositeTransform() { Rotation = rotationAngle };  
    54.   
    55.             return path;  
    56.         }  
    57.     }  


    通过类Path path= BaseStation.GetBaseStation(90);就可以获得对称轴为90度,角度为40的扇形。


     

    当然还有最直接的方法:Arc类

    命名空间: Microsoft.Expression.Shapes 
    程序集: Microsoft.Expression.Drawing(在 microsoft.expression.drawing.dll 中)

    只需要几行代码就可以了,也可以在Blend里直接拖到页面上:

    [csharp] view plaincopy
      1. var arc = new Arc  
      2.           {  
      3.                               Width = 250,  
      4.                               Height = 250,  
      5.                               ArcThickness = 1,  
      6.                               ArcThicknessUnit = Microsoft.Expression.Media.UnitType.Percent,  
      7.                               StartAngle = -20,  
      8.                               EndAngle = 20,  
      9.                               Stretch = Stretch.None,  
      10.                               UseLayoutRounding = false,  
      11.                               Stroke = new SolidColorBrush(Colors.Transparent),  
      12.                               StrokeThickness = 1,                                    };  
  • 相关阅读:
    关于windows CE Platform Builder中模拟器的限制
    送给每天都用电脑的人
    用eVC4开发SmartPhone、Pocket PC程序之 开发工具下载、安装、配置
    在手机上显示图片
    如何禁止回车的使用
    弹出窗口的方法
    Request.Form同Request.querystring的区别.txt
    找前几天
    手机上可移动的图片
    httphandler是做什么的?
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/2942840.html
Copyright © 2011-2022 走看看