WPF test animation robot(simulate blink,mouse talk)
WPF 动画,模拟机器人眨眼,说话。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfApplication9 { /// <summary> /// Interaction logic for RobotWindow.xaml /// </summary> public partial class RobotWindow : Window { public RobotWindow() { InitializeComponent(); sbTalk = Resources["sbTalk"] as Storyboard; aTalk = sbTalk.Children[0] as DoubleAnimation; sbEyeBlink = Resources["sbEyeBlink"] as Storyboard; TestDisplay(); } Storyboard sbTalk; DoubleAnimation aTalk; Storyboard sbEyeBlink; Random rnd = new Random(); void TestDisplay() { Talk(); EyeBlink(); } /// <summary> /// 随机眨眼 /// </summary> void EyeBlink() { Task.Run(() => { while (true) { Thread.Sleep(1000* rnd.Next(1,5)); Dispatcher.Invoke(() => { sbEyeBlink.Begin(); }); } }); } /// <summary> /// 说话 /// </summary> void Talk() { Task.Run(() => { while (true) { Thread.Sleep(600); Dispatcher.Invoke(() => { aTalk.From = 0; aTalk.To = rnd.NextDouble(); sbTalk.Begin(); }); } }); } } } UI: <Window x:Class="WpfApplication9.RobotWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RobotWindow" Height="377" Width="535"> <Window.Resources> <Storyboard x:Key="sbTalk"> <DoubleAnimation AutoReverse="True" Duration="0:0:0.3" To="1" Storyboard.TargetName="t1" Storyboard.TargetProperty="ScaleY" /> </Storyboard> <Storyboard x:Key="sbEyeBlink"> <DoubleAnimation AutoReverse="True" Duration="0:0:0.3" To="0" Storyboard.TargetName="eye1" Storyboard.TargetProperty="ScaleY" /> <DoubleAnimation AutoReverse="True" Duration="0:0:0.3" To="0" Storyboard.TargetName="eye2" Storyboard.TargetProperty="ScaleY" /> </Storyboard> </Window.Resources> <Grid> <Grid Margin="0,32,74,0" HorizontalAlignment="Right" Width="388" Height="270" VerticalAlignment="Top"> <Ellipse Name="Head" Width="245" Height="235" Stroke="#FFCDCDC2" StrokeThickness="4" Fill="#FFFBEDF3" HorizontalAlignment="Center" Margin="22,10,25,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" > </Ellipse> <Ellipse Name="lEye" Width="64" Stroke="#FFCDCDC2" StrokeThickness="4" Fill="#FFCDCDC2" HorizontalAlignment="Center" Margin="97,100,227,0" RenderTransformOrigin="0.5,0.5" Height="44" VerticalAlignment="Top" > <Ellipse.RenderTransform> <TransformGroup> <ScaleTransform x:Name="eye1" ScaleY="1"/> <SkewTransform/> <TranslateTransform/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> <Ellipse Name="rEye" Width="64" Stroke="#FFCDCDC2" StrokeThickness="4" Fill="#FFCDCDC2" HorizontalAlignment="Center" Margin="220,100,104,0" RenderTransformOrigin="0.5,0.5" Height="44" VerticalAlignment="Top" > <Ellipse.RenderTransform> <TransformGroup> <ScaleTransform x:Name="eye2" ScaleY="1"/> <SkewTransform/> <TranslateTransform/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> <Ellipse Name="mouse" Width="100" Height="58" Stroke="Yellow" StrokeThickness="4" Fill="#FFE2055F" HorizontalAlignment="Center" Margin="96,151,96,61" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" > <Ellipse.RenderTransform> <TransformGroup> <ScaleTransform x:Name="t1" ScaleY="0.1"/> <SkewTransform/> <TranslateTransform/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> </Grid> </Grid> </Window>
fffffffffffffffff
test red font.