WPF实现手风琴式轮播图切换效果
作者:RunnerDNA 时间:2022-01-24 13:49:26
本文实例为大家分享了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();
}
}
来源:https://blog.csdn.net/dnazhd/article/details/107413269
标签:WPF,手风琴,轮播图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java设置session过期时间的实现方法
2022-02-18 20:25:29
C#中List<T>存放元素的工作机制
2022-09-29 16:31:23
Java 深拷贝与浅拷贝的分析
2023-07-30 14:13:13
java中MultipartFile互转File的方法
2022-12-14 16:52:09
Django之多对多查询与操作方法详解
2021-08-03 03:21:58
![](https://img.aspxhome.com/file/2023/0/96060_0s.jpg)
Java synchronized重量级锁实现过程浅析
2023-10-25 14:10:17
![](https://img.aspxhome.com/file/2023/7/79187_0s.png)
Maven学习----Maven安装与环境变量配置教程
2021-12-04 08:20:25
![](https://img.aspxhome.com/file/2023/8/60678_0s.png)
总结Java对象被序列化的两种方法
2023-05-11 09:46:52
详解JAVA 弱引用
2022-03-12 01:30:29
java自定义封装StringUtils常用工具类
2022-09-01 05:11:13
深入理解SpringMVC中央调度器DispatcherServlet
2023-03-11 08:54:48
![](https://img.aspxhome.com/file/2023/4/119184_0s.jpg)
Java Property类使用详解
2023-11-06 21:52:57
![](https://img.aspxhome.com/file/2023/7/58667_0s.png)
详解Java后端优雅验证参数合法性
2021-09-06 16:07:22
![](https://img.aspxhome.com/file/2023/9/60159_0s.jpg)
Java执行cmd命令的举例与注意事项
2023-11-03 10:21:05
Java接口默认方法带来的问题分析【二义性问题】
2023-11-27 20:32:55
![](https://img.aspxhome.com/file/2023/7/60117_0s.png)
一文带你了解如何正确使用Java中的字符串常量池
2022-07-13 01:35:30
![](https://img.aspxhome.com/file/2023/3/66933_0s.png)
Java控制台实现猜拳游戏
2022-12-15 09:54:46
如何解决springboot读取配置文件的中文乱码问题
2022-09-13 22:26:42
![](https://img.aspxhome.com/file/2023/7/64487_0s.png)
使用HTTPclient保持长连接
2023-10-17 12:29:34
![](https://img.aspxhome.com/file/2023/9/64109_0s.png)
全面了解Java中Native关键字的作用
2022-07-13 02:01:09
![](https://img.aspxhome.com/file/2023/9/88199_0s.png)