與眾不同 windows phone (17) - Graphic and Animation(畫圖和動畫)


[索引頁]
[源碼下載]


與眾不同 windows phone (17) - Graphic and Animation(畫圖和動畫)



作者:webabcd


介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之畫圖和動畫

  • 圖形
  • 畫筆
  • 轉換
  • 動畫
  • 緩動



示例
1、圖形(Shape)
ShapeDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.GraphicAndAnimation.ShapeDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Name="root" Orientation="Vertical" HorizontalAlignment="Left">

            <!--
                以 xaml 的方式繪制圖形
                更多詳細內容請參見:
                    http://www.cnblogs.com/webabcd/archive/2008/10/23/1317407.html
                    http://www.cnblogs.com/webabcd/archive/2008/10/27/1320098.html
            -->
            
            <!--畫矩形-->
            <Rectangle Width="200" Height="50" Fill="Red" Stroke="Yellow" StrokeThickness="3" />

            <!--畫多條連接起來的直線-->
            <Polyline Points="10,100 50,10 100,100" Stroke="Green" StrokeThickness="20" StrokeLineJoin="Round" />

            <!--畫直線-->
            <Line X1="0" Y1="0" X2="400" Y2="0" Stroke="Blue" StrokeThickness="10" StrokeDashArray="2,4,6" StrokeDashCap="Triangle" />

            <!--畫橢圓-->
            <Ellipse Stroke="Red" Fill="Yellow" StrokeThickness="6" Width="100" Height="50"></Ellipse>

        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

ShapeDemo.xaml.cs

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 Demo.GraphicAndAnimation
{
    public partial class ShapeDemo : PhoneApplicationPage
    {
        public ShapeDemo()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ShapeDemo_Loaded);
        }

        void ShapeDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // 以 代碼 的方式繪制圖形

            // 畫多邊形
            Polygon polygon = new Polygon();
            polygon.Stroke = new SolidColorBrush(Colors.Purple);
            polygon.StrokeThickness = 3d;
            polygon.Points.Add(new Point(0, 0));
            polygon.Points.Add(new Point(100, 0));
            polygon.Points.Add(new Point(0, 100));
            polygon.Points.Add(new Point(100, 100));

            root.Children.Add(polygon);
        }
    }
}


2、畫筆(Brush)
BrushDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.GraphicAndAnimation.BrushDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Name="root" Orientation="Vertical" HorizontalAlignment="Left">

            <!--
                以 xaml 的方式應用畫筆
                更多詳細內容請參見:
                    http://www.cnblogs.com/webabcd/archive/2008/10/30/1322658.html
            -->

            <Ellipse Margin="10" Width="200" Height="100" Stroke="Yellow" StrokeThickness="3">
                <Ellipse.Fill>
                    <!--單色畫筆-->
                    <SolidColorBrush Color="#FF0000" Opacity="0.5"  />
                </Ellipse.Fill>
            </Ellipse>

            <MediaElement x:Name="mediaElement" Source="Assets/Demo.mp4" Width="0" Height="0" />
            <Rectangle Width="300" Height="100">
                <Rectangle.Fill>
                    <!--視頻畫筆-->
                    <VideoBrush SourceName="mediaElement" />
                </Rectangle.Fill>
            </Rectangle>
            
        </StackPanel>
    </Grid>
 
</phone:PhoneApplicationPage>

BrushDemo.xaml.cs

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 Demo.GraphicAndAnimation
{
    public partial class BrushDemo : PhoneApplicationPage
    {
        public BrushDemo()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(BrushDemo_Loaded);
        }

        void BrushDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // 以 代碼 的方式應用畫筆

            // 使用放射性漸變畫筆
            GradientStop gs1 = new GradientStop();
            gs1.Color = Colors.Red;
            gs1.Offset = 0d;
            GradientStop gs2 = new GradientStop();
            gs2.Color = Colors.Green;
            gs2.Offset = 0.3d;
            GradientStop gs3 = new GradientStop();
            gs3.Color = Colors.Blue;
            gs3.Offset = 1d;

            LinearGradientBrush brush = new LinearGradientBrush();
            brush.StartPoint = new Point(0, 0);
            brush.EndPoint = new Point(1, 1);
            brush.GradientStops.Add(gs1);
            brush.GradientStops.Add(gs2);
            brush.GradientStops.Add(gs3);

            Rectangle rect = new Rectangle();
            rect.Width = 200d;
            rect.Height = 200d;
            rect.Fill = brush;

            root.Children.Add(rect);
        }
    }
}


3、轉換(Transform)
TransformDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.GraphicAndAnimation.TransformDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Name="root" Orientation="Vertical">

            <!--
                以 xaml 的方式應用轉換
                更多詳細內容請參見:
                    http://www.cnblogs.com/webabcd/archive/2008/11/03/1325150.html
                    http://www.cnblogs.com/webabcd/archive/2010/08/25/1807797.html
            -->

            <!--復合轉換-->
            <Rectangle Height="100" Width="100" Fill="Red">
                <Rectangle.RenderTransform>
                    <CompositeTransform SkewX="30" Rotation="60" ScaleX="0.6" ScaleY="0.3" />
                </Rectangle.RenderTransform>
            </Rectangle>

            <!-- 用 TransformGroup(多個單一轉換組合在一次) 的方式達到上面的 CompositeTransform 的相同效果 -->
            <Rectangle Height="100" Width="100" Fill="Red">
                <Rectangle.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleX="0.6" ScaleY="0.3" />
                        <SkewTransform AngleX="30" />
                        <RotateTransform Angle="60" />
                    </TransformGroup>
                </Rectangle.RenderTransform>
            </Rectangle>
            
        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

TransformDemo.xaml.cs

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 Demo.GraphicAndAnimation
{
    public partial class TransformDemo : PhoneApplicationPage
    {
        public TransformDemo()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(TransformDemo_Loaded);
        }

        void TransformDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // 以 代碼 的方式應用轉換

            // 使用旋轉轉換
            RotateTransform rt = new RotateTransform();
            rt.Angle = 30;

            Rectangle rect = new Rectangle();
            rect.Width = 200d;
            rect.Height = 200d;
            rect.Fill = new SolidColorBrush(Colors.Green);
            rect.RenderTransform = rt;

            root.Children.Add(rect);
        }
    }
}


4、動畫(Animation)
AnimationDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.GraphicAndAnimation.AnimationDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Name="root" Orientation="Vertical">
            
            <!--
                以 xaml 的方式增加動畫效果
                更多詳細內容請參見:
                    http://www.cnblogs.com/webabcd/archive/2008/11/06/1327758.html
            -->

            <!--給 Rectangle 的 Width 增加動畫效果-->
            <StackPanel.Resources>
                <BeginStoryboard x:Name="beginStoryboard">
                    <Storyboard x:Name="storyboard">
                        <DoubleAnimation 
                            Storyboard.TargetName="rectangle" 
                            Storyboard.TargetProperty="Width"
                            From="200"
                            To="100"
                            Duration="00:00:03"
                            AutoReverse="True"
                            RepeatBehavior="Forever">
                        </DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </StackPanel.Resources>

            <Rectangle x:Name="rectangle" Width="200" Height="100" Fill="Red" StrokeThickness="6" />

        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

AnimationDemo.xaml.cs

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 Demo.GraphicAndAnimation
{
    public partial class AnimationDemo : PhoneApplicationPage
    {
        public AnimationDemo()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(AnimationDemo_Loaded);
        }

        void AnimationDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // 以 代碼 的方式增加動畫效果

            // 畫個圓
            Ellipse ellipse = new Ellipse();
            ellipse.Width = 100d;
            ellipse.Height = 100d;
            ellipse.Fill = new SolidColorBrush(Colors.Green);
            root.Children.Add(ellipse);


            // 為上面畫的圓增加顏色動畫效果
            ColorAnimation ca = new ColorAnimation();
            ca.Duration = TimeSpan.FromSeconds(2);
            ca.From = Colors.Green;
            ca.To = Colors.Blue;
            ca.AutoReverse = true;
            ca.RepeatBehavior = RepeatBehavior.Forever;

            Storyboard.SetTarget(ca, ellipse);
            Storyboard.SetTargetProperty(ca, new PropertyPath("(Ellipse.Fill).(SolidColorBrush.Color)"));

            Storyboard s = new Storyboard();
            s.Children.Add(ca);
            s.Begin();
        }
    }
}


5、緩動(Easing)
EasingDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.GraphicAndAnimation.EasingDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Name="root" Orientation="Vertical">

            <!--
                以 xaml 的方式為動畫增加緩動效果
                更多詳細內容請參見:
                    http://www.cnblogs.com/webabcd/archive/2009/08/20/1550334.html
            -->

            <!--給 Rectangle 的 Width 增加動畫效果,同時為此動畫增加緩動效果-->
            <StackPanel.Resources>
                <BeginStoryboard x:Name="beginStoryboard">
                    <Storyboard x:Name="storyboard">
                        <DoubleAnimation Storyboard.TargetName="rectangle" Storyboard.TargetProperty="Width" From="400" To="100" Duration="00:00:05" AutoReverse="True" RepeatBehavior="Forever">
                            <DoubleAnimation.EasingFunction>
                                <!--增加緩動效果-->
                                <BounceEase EasingMode="EaseInOut" />
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </StackPanel.Resources>

            <Rectangle x:Name="rectangle" Width="400" Height="100" Fill="Red" StrokeThickness="6" />

        </StackPanel>
    </Grid>
 
</phone:PhoneApplicationPage>

EasingDemo.xaml.cs

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 Demo.GraphicAndAnimation
{
    public partial class EasingDemo : PhoneApplicationPage
    {
        public EasingDemo()
        {
            InitializeComponent(); 
            
            this.Loaded += new RoutedEventHandler(AnimationDemo_Loaded);
        }

        void AnimationDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // 以 代碼 的方式增加動畫效果

            // 畫個圓
            Ellipse ellipse = new Ellipse();
            ellipse.Width = 100d;
            ellipse.Height = 100d;
            ellipse.Fill = new SolidColorBrush(Colors.Green);
            root.Children.Add(ellipse);


            // 為上面畫的圓增加顏色動畫效果
            ColorAnimation ca = new ColorAnimation();
            ca.Duration = TimeSpan.FromSeconds(5);
            ca.From = Colors.Green;
            ca.To = Colors.Blue;
            ca.AutoReverse = true;
            ca.RepeatBehavior = RepeatBehavior.Forever;

            // 為顏色動畫增加緩動效果
            EasingFunctionBase easing = new BounceEase();
            easing.EasingMode = EasingMode.EaseInOut;
            ca.EasingFunction = easing;

            Storyboard.SetTarget(ca, ellipse);
            Storyboard.SetTargetProperty(ca, new PropertyPath("(Ellipse.Fill).(SolidColorBrush.Color)"));

            Storyboard s = new Storyboard();
            s.Children.Add(ca);
            s.Begin();
        }
    }
}



OK
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM