天天看點

windows phone 了解LinearGradientBrush類和RadialGradienBrush類(11)

我們了解到在能在xaml中完成的設計,一般在隐藏檔案中也可通過代碼完成;本節中的案例是實作對同一設計效果的不同寫法;例如在隐藏檔案中代碼如下:

View Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;

namespace LinearGB

{

    public partial class MainPage : PhoneApplicationPage

    {

        // 構造函數

        public MainPage()

        {

            InitializeComponent();

            Init();

        }

        private void Init()

            TextBlock tb = new TextBlock();

            tb.Name = "tbName";

            tb.Text = "顯示顔色";

            tb.VerticalAlignment = VerticalAlignment.Center;

            tb.HorizontalAlignment = HorizontalAlignment.Center;

            //執行個體化

            //LinearGradientBrush lgb1 = new LinearGradientBrush();

            LinearGradientBrush lgb2 = new LinearGradientBrush();

            GradientStopCollection gsc = new GradientStopCollection();

            //設定過渡點的顔色和偏移量

            GradientStop gt1 = new GradientStop();

            //Color c = new Color();

            //c.R = 255;

            //c.A = 230;

            //c.G = 22;

            //c.B = 43;

            //gt1.Color=c;

            gt1.Color = Color.FromArgb(255, 164, 143, 112);

            gt1.Offset = 0.2;

            gsc.Add(gt1);

            GradientStop gt2 = new GradientStop();

            //Color c2 = new Color();

            //c2.R = 251;

            //c2.A = 231;

            //c2.G = 12;

            //c2.B = 13;

            //gt2.Color = c2;

            gt2.Color = Color.FromArgb(229, 178, 234, 188);

            gt2.Offset = 0.7;

            gsc.Add(gt2);

            //設定線性漸變色的起始坐标

            Point p1 = new Point(0, 0);

            lgb2.StartPoint = p1;

            //設定線性漸變色的終止坐标

            Point p2 = new Point(1, 1);

            lgb2.EndPoint = p2;

            //設定漸變停止點

            lgb2.GradientStops = gsc;

            //設定前景色

            tb.Foreground = lgb2;

            ContentPanel.Children.Add(tb);

}

 上面代碼實作的效果:

 從xaml檔案中同樣也可以實作類似的效果,ContentPanel下的textblock内容如下:

<TextBlock x:Name="tbName" Text="顯示顔色" VerticalAlignment="Center" HorizontalAlignment="Center" >

                <TextBlock.Foreground>

                    <LinearGradientBrush StartPoint="0 0" EndPoint="1 1">

                        <LinearGradientBrush.GradientStops>

                        <GradientStopCollection>

                            <GradientStop Offset="0.2" Color="AliceBlue"></GradientStop>

                            <GradientStop Offset="0.7" Color="Brown"></GradientStop>

                        </GradientStopCollection>

                         </LinearGradientBrush.GradientStops>

                    </LinearGradientBrush>

                </TextBlock.Foreground>

            </TextBlock>

 實作的效果:

官方解釋

下圖示範對角漸變。 其中添加了一條線,用于突出顯示漸變從起點到終點的内插路徑。

對角方向的線性漸變

下一幅插圖顯示的是同一線性漸變,但它具有突出顯示的漸變停止點。

具有突出顯示的漸變停止點的對角線性漸變

<TextBlock x:Name="tbRadia" VerticalAlignment="Center"  HorizontalAlignment="Center" Text="擴散式輻射">

                     <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5"

        RadiusX="0.5" RadiusY="0.5">

        <GradientStop Color="Yellow" Offset="0" />

        <GradientStop Color="Red" Offset="0.25" />

        <GradientStop Color="Blue" Offset="0.75" />

        <GradientStop Color="LimeGreen" Offset="1" />

      </RadialGradientBrush>

 效果圖:

徑向漸變中的漸變停止點

本文轉自shenzhoulong  51CTO部落格,原文連結:http://blog.51cto.com/shenzhoulong/835534,如需轉載請自行聯系原作者

繼續閱讀