【demo练习三】:图片水平滚动、点击按钮变更图片动画
要求:四张图片水平滚动,每隔5秒进行一次循环,点击按钮随机变更图片。
XAML前台代码:
1 <Window x:Class="图片滚动.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Loaded="Window_Loaded" 5 Title="MainWindow" 6 Height="350" 7 Width="525"> 8 <Viewbox> 9 <Canvas Width="1241" Height="768" Name="ImageCanvas" Background="SkyBlue"> 10 <Canvas x:Name="canvas_photo" Width="1242" Height="200"> 11 <Image x:Name="image1" Width="200" Height="200" 12 Canvas.Left="0" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\4.jpg"> 13 </Image> 14 <Image x:Name="image3" Width="200" Height="200" 15 Canvas.Left="420" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\6.jpg" Canvas.Top="0"> 16 </Image> 17 <Image x:Name="image4" Width="200" Height="200" 18 Canvas.Left="620" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\7.jpg" Canvas.Top="0"> 19 </Image> 20 <Image x:Name="image2" Width="200" Height="200" Canvas.Left="218" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\5.jpg" Canvas.Top="0"></Image> 21 </Canvas> 22 <Button Canvas.Left="303" Canvas.Top="474" Content="Button" Height="77" 23 Name="button1" Width="184" Click="button1_Click" /> 24 </Canvas> 25 </Viewbox> 26 </Window>
C#后台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.Windows.Media.Animation; 15 using System.Windows.Threading; 16 using System.IO; 17 18 namespace 图片滚动 19 { 20 /// <summary> 21 /// MainWindow.xaml 的交互逻辑 22 /// </summary> 23 public partial class MainWindow : Window 24 { 25 private System.Windows.Threading.DispatcherTimer dTimer = new DispatcherTimer(); 26 List<string> listPath = new List<string>(); 27 //设定图片集合 28 List<Image> listImg = new List<Image>(); 29 30 double position = 0.0; 31 32 public MainWindow() 33 { 34 InitializeComponent(); 35 } 36 37 private void Window_Loaded(object sender, RoutedEventArgs e) 38 { 39 40 listImg.Add(image1); 41 listImg.Add(image2); 42 listImg.Add(image3); 43 listImg.Add(image4); 44 45 DoubleAnimation animation = new DoubleAnimation(); 46 //animation.From = 0; 47 //animation.To = 250; 48 animation.RepeatBehavior = RepeatBehavior.Forever; 49 animation.Duration = TimeSpan.FromSeconds(5); 50 image1.BeginAnimation(Canvas.LeftProperty, animation); 51 //image1.BeginAnimation(Canvas.TopProperty, animation); 52 53 loadInfo(); 54 55 //dTimer.Tick += new EventHandler(dTimer_Tick); 56 //dTimer.Interval = new TimeSpan(0, 0, 5); 57 //dTimer.Start(); 58 59 //每30毫秒触发一次定时器 60 dTimer.Tick += new EventHandler(dTimer_Tick); 61 dTimer.Interval = TimeSpan.FromMilliseconds(30); 62 dTimer.Start(); 63 64 } 65 66 //遍历加载指定文件下的文件 67 public void loadInfo() 68 { 69 string floderPath = System.Environment.CurrentDirectory + "\\Images\\"; 70 DirectoryInfo TheFolder = new DirectoryInfo(floderPath); 71 foreach (FileInfo NextFile in TheFolder.GetFiles()) 72 { 73 listPath.Add(NextFile.FullName); 74 } 75 } 76 77 private string Window_Loaded(int index) 78 { 79 return System.Environment.CurrentDirectory + "\\Images\\" + index.ToString() + ".jpg"; 80 } 81 82 //int i; 83 private void dTimer_Tick(object sender, EventArgs e) 84 { 85 //if(Mouse) 86 foreach (var item in listImg) 87 { 88 position = Canvas.GetLeft(item); 89 //图片进行两个位置的移动 90 position = position + 2; 91 //判断如果越界了 92 if (position > this.canvas_photo.Width + 200) 93 { 94 //则回到起点图片零起点 95 Canvas.SetLeft(item, 0); 96 } 97 else 98 { 99 //没有越界则继续移动 100 Canvas.SetLeft(item, position); 101 } 102 } 103 //for (i = 0; i < listImg.Count; i++) 104 //{ 105 // position = Canvas.GetLeft(listImg[i]); 106 // position = position + 2; 107 // if (position > this.canvas_photo.Width + 200) 108 // { 109 // Canvas.SetLeft(listImg[i], 0); 110 // } 111 // else 112 // { 113 // Canvas.SetLeft(listImg[i], position); 114 // } 115 //} 116 } 117 118 //int imageIndex = 0; 119 private void button1_Click(object sender, RoutedEventArgs e) 120 { 121 //随机事件 122 Random ran = new Random(); 123 int RandKey = ran.Next(100, 999) % listPath.Count; 124 listImg[(new Random().Next(0, 1000)) % listImg.Count].Source = new BitmapImage(new Uri(listPath[RandKey])); 125 } 126 } 127 }
不积跬步,无以致千里;不集小流,无以成江海。
如转载本文,请还多关注一下我的博客:https://www.cnblogs.com/Owen-ET/;
我的Github地址:https://github.com/Owen-ET————————————
无善无恶心之体, 有善有恶意之动, 知善知恶是良知, 为善去恶是格物。