WPF实现手风琴式轮播图切换效果
更新时间:2020年11月3日 15:19 点击:1950
本文实例为大家分享了WPF实现轮播图切换效果的具体代码,供大家参考,具体内容如下
实现效果如下:
步骤:
1、自定义控件MyImageControl
实现图片的裁切和动画的赋值。
public partial class MyImageControl : UserControl { public static readonly DependencyProperty ShowImageProperty = DependencyProperty.Register("ShowImage", typeof(BitmapImage), typeof(MyImageControl), new PropertyMetadata(null)); public BitmapImage ShowImage { get { return (BitmapImage)GetValue(ShowImageProperty); } set { SetValue(ShowImageProperty, value); } } public MyImageControl() { InitializeComponent(); } public Storyboard storyboard = new Storyboard(); private const int FlipCount = 5; BitmapSource[] bitmap = new BitmapSource[FlipCount]; Image[] images = new Image[FlipCount]; public void GetHorizontalFlip() { int partImgWidth = (int)this.ShowImage.PixelWidth; int partImgHeight = (int)(this.ShowImage.PixelHeight / FlipCount); for (int i = 0; i < FlipCount; i++) { bitmap[i] = GetPartImage(this.ShowImage, 0, i * partImgHeight, partImgWidth, partImgHeight); images[i] = new Image() { Width = partImgWidth, Height = partImgHeight, Source = bitmap[i], }; Canvas.SetTop(images[i], i * partImgHeight); this.mainCanvas.Children.Add(images[i]); DoubleAnimation da = new DoubleAnimation(0, (int)this.ShowImage.PixelWidth, new Duration(TimeSpan.FromMilliseconds((i + 1) * 250)), FillBehavior.HoldEnd); storyboard.Children.Add(da); Storyboard.SetTarget(da, images[i]); Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)")); } storyboard.FillBehavior = FillBehavior.HoldEnd; storyboard.Completed += new EventHandler(Storyboard_Completed); } private void Storyboard_Completed(object sender, EventArgs e) { this.mainCanvas.Children.Clear(); storyboard.Children.Clear(); } private BitmapSource GetPartImage(BitmapImage img, int XCoordinate, int YCoordinate, int Width, int Height) { return new CroppedBitmap(img, new Int32Rect(XCoordinate, YCoordinate, Width, Height)); } }
2、自定义轮播控件
实现图片点击轮播和动画的启动。
public partial class MyRollControl : UserControl { public MyRollControl() { InitializeComponent(); } /// <summary> /// 是否开始滚动 /// </summary> public bool isBegin = false; /// <summary> /// 本轮剩余滚动数 /// </summary> public int rollNum = 0; private List<BitmapImage> _ls_images; /// <summary> /// 滚动图片组 /// </summary> public List<BitmapImage> ls_images { set { if (rollNum > 0) { // 本轮滚动未结束 } else { // 开始新的一轮滚动 _ls_images = value; rollNum = _ls_images.Count(); } } get { return _ls_images; } } private int n_index = 0;// 滚动索引 /// <summary> /// 启动 /// </summary> public void Begin() { if (!isBegin) { isBegin = true; this.ResetStory(); this.controlFront.GetHorizontalFlip(); } } /// <summary> /// 初始化图片 /// </summary> void ResetStory() { if (this.ls_images.Count > 0) { this.controlFront.ShowImage = this.ls_images[this.n_index++ % this.ls_images.Count]; this.controlBack.ShowImage = this.ls_images[this.n_index % this.ls_images.Count]; } } private void mainGrid_MouseDown(object sender, MouseButtonEventArgs e) { if (this.controlFront.storyboard.Children.Count > 0) { if(this.controlBack.storyboard.Children.Count <= 0) { Canvas.SetZIndex(this.controlFront, 0); this.controlFront.storyboard.Begin(); this.controlBack.GetHorizontalFlip(); rollNum--; this.ResetStory(); } } else if(this.controlFront.storyboard.Children.Count <= 0) { if(this.controlBack.storyboard.Children.Count > 0) { this.controlBack.storyboard.Begin(); rollNum--; this.ResetStory(); Canvas.SetZIndex(this.controlFront, -1); this.controlFront.GetHorizontalFlip(); } } } }
3、主窗体调用后台逻辑
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<BitmapImage> ls_adv_img = new List<BitmapImage>(); List<string> listAdv = GetUserImages(@"C:\Image"); foreach (string a in listAdv) { BitmapImage img = new BitmapImage(new Uri(a)); ls_adv_img.Add(img); } this.rollImg.ls_images = ls_adv_img; this.rollImg.Begin(); } /// <summary> /// 获取当前用户的图片文件夹中的图片路径列表(不包含子文件夹) /// </summary> private List<string> GetUserImages(string path) { List<string> images = new List<string>(); DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] files = GetPicFiles(path, "*.jpg,*.png,*.bmp,", SearchOption.TopDirectoryOnly); if (files != null) { foreach (FileInfo file in files) { images.Add(file.FullName); } } return images; } private FileInfo[] GetPicFiles(string picPath, string searchPattern, SearchOption searchOption) { List<FileInfo> ltList = new List<FileInfo>(); DirectoryInfo dir = new DirectoryInfo(picPath); string[] sPattern = searchPattern.Replace(';', ',').Split(','); for (int i = 0; i < sPattern.Length; i++) { FileInfo[] files = null; try { files = dir.GetFiles(sPattern[i], searchOption); } catch (System.Exception ex) { files = new FileInfo[] { }; } ltList.AddRange(files); } return ltList.ToArray(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: WPF实现半圆形导航菜单
下一篇: C#实现拼手气红包算法
相关文章
- 这篇文章主要介绍了如何使用JavaScript实现“无缝滚动 自动播放”轮播图效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-20
c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)
这篇文章主要介绍了c# WPF中通过双击编辑DataGrid中Cell的示例(附源码),帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-03- jQuery实现带玻璃流光质感的手风琴特效是一款基于jQuery+CSS3实现的带玻璃流光质感的手风琴特效,分享给大家,具体如下效果图:具体代码如下:html代码: <section class="strips"> <article class="strips__strip"> <di...2015-11-24
- 这篇文章主要为大家详细介绍了jQuery实现手风琴小案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-11
- 最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟着小编学习WPF实现类似360安全卫士界面的程序源码分享...2020-06-25
- 这篇文章主要为大家详细介绍了vue+rem自定义轮播图效果,手指触摸左滑和右滑,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-29
- 这篇文章主要为大家详细介绍了基于jquery实现轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
- 这篇文章主要为大家详细介绍了使用jquery实现轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-01-02
- 这篇文章主要为大家详细介绍了JS轮播图的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-25
- 这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了C# WPF 通过委托实现多窗口间的传值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了原生Js 实现的简单无缝滚动轮播图的示例代码,帮助大家更好的理解喝学习使用js,感兴趣的朋友可以了解下...2021-05-11
- 这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家详细介绍了原生javascript+CSS实现轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-18
- 这篇文章主要介绍了C#中WPF使用多线程调用窗体组件的方法,涉及C#中多线程的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了WPF InkCanvas绘制矩形和椭圆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家详细介绍了基于JavaScript实现轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-01-02
- 这篇文章主要给大家介绍了关于WPF基础教程之形状画刷与变换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了jQuery插件实现手风琴二级菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-12
- 这篇文章主要给大家介绍了关于WPF如何自定义TabControl控件样式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。...2020-06-25