1. 程式人生 > >Windows Phone開發(34):路徑標記語法

Windows Phone開發(34):路徑標記語法

如果你覺得前面所討論的繪製各種幾何圖形的方法過於複雜,那麼,今天我們也來一次“減負”吧。當然,我們是很輕鬆的,本教程是不用考試的,也不會班級排名,僅僅為讀者朋友們提供一種思路罷了。

本節我們聊一下路徑標記法,有了這個東東,你會覺得繪製路徑會輕鬆了不少,事不宜遲,路徑標記法到底有多方便,先看一個例項再說吧。

[html] view plaincopyprint?
  1. <PathVerticalAlignment="Stretch"HorizontalAlignment="Stretch"
  2.       Stroke="LightGreen"StrokeThickness="6"
  3.       Data
    ="M12,2 L35,28 175,69 H80 V260 M185,400 C60,40 135,100 300,250"/>

看看效果圖。

怎麼樣?是不是簡潔了許多?

一、移動指令

這個好理解,就是移動到某個點,接下來繪製的一系列圖形以該點作為起點,語法如下:

[html] view plaincopyprint?
  1. M<點座標>或m<點座標>


M表示絕對定位,m表示相對於上一個點的偏移量,如果移動指令後接著多個點,則會建立連線這些點的直線,看下面的例子。

[html] view plaincopyprint?
  1. <PathHorizontalAlignment
    ="Stretch"
  2.       VerticalAlignment="Stretch"
  3.       Stroke="Yellow"StrokeThickness="6"
  4.       Data="M50,26 124,39 220,97 m200,145 95,335"/>


先看執行效果。

注意上面的M與m的區別,距離上一個結束點X軸+200,Y軸+145的地方,也就是一個偏移量。

二、繪製指令

這裡我僅僅列舉幾個例子,具體內容大家可以參考MSDN。

1、繪製直線。

語法:

[html] view plaincopyprint?
  1. <結束點>或l <結束點>


例子:

[html]
view plaincopyprint?
  1. <PathHorizontalAlignment="Stretch"
  2.   VerticalAlignment="Stretch"
  3.   Stroke="Yellow"
  4.   StrokeThickness="6"
  5.   Data="M21,15 L30,17 200,79 150,300 160,410"/>

執行效果。

2、貝塞爾曲線

(1)三次方貝塞爾曲線

語法:C 控制點1 控制點2 終點 ,或c 控制點1 控制點2 終點。

例子:

[html] view plaincopyprint?
  1. <PathHorizontalAlignment="Stretch"
  2.   VerticalAlignment="Stretch"
  3.   Stroke="Yellow"
  4.   StrokeThickness="6"
  5.   Data="M10,5 C60,75 150,160 30,200"/>


(2)二次貝塞爾曲線

語法:Q 控制點 終點 或 q 控制點 終點

例子:

[html] view plaincopyprint?
  1. <PathHorizontalAlignment="Stretch"
  2.       VerticalAlignment="Stretch"
  3.       Stroke="Yellow"
  4.       StrokeThickness="6"
  5.       Data="M10,5 Q200,55 200,385"/>


3、繪製弧線

語法:

A size rotationAngle isLargeArcFlag sweepDirectionFlag endPoint

- 或 -

a size rotationAngle isLargeArcFlag sweepDirectionFlag endPoint

size:圓弧的大小,X表示X軸上的半徑長度,Y表示Y軸上的半徑度度。

rotationAngle:圓弧的角度。

isLargeArcFlag如果弧線的角度應大於或等於 180 度,則設定為 1;否則設定為 0。

sweepDirectionFlag如果弧線按照正角方向繪製,則設定為 1;否則設定為 0。

endPoint:終點。

例子:

[html] view plaincopyprint?
  1. <PathHorizontalAlignment="Stretch"
  2.       VerticalAlignment="Stretch"
  3.       Stroke="Yellow"
  4.       StrokeThickness="6"
  5.       Data="M10,30 A185,230 90 0 1 200,435"/>


也許你會發現,儘管使用這種方法,但是畫起圖來也是不方便,現在,我明白我為什麼不重點敘述這些內容的原因了,對於XAML手動構圖,我只是簡單帶過,在實際開發中,效率不高,而且難度較大。

下一節中,我向大家介紹一種更簡單的繪圖方案。