WPF实现手风琴式轮播图切换效果
作者:RunnerDNA 发布时间:2022-01-24 13:49:26
标签:WPF,手风琴,轮播图
本文实例为大家分享了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


猜你喜欢
- 本文实例为大家分享了使用aop实现全局异常处理的具体代码,供大家参考,具体内容如下日常业务中存在的问题使用大量的try/catch来捕获异常
- 前言最近在阅读 .NET Threadpool starvation, and how queuing makes it worse 这篇博
- Mybatis条件if test使用枚举值1.正确package com.weather.weatherexpert.common.util
- 前言平时做一些统计数据,经常从数据库或者是从接口获取出来的数据,单位是跟业务需求不一致的。比如, 我们拿出来的 分, 实际上要是元又比如,我
- The error simply says, “you've made changes in files in your works
- 不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录action,就可以实现登录到其他系统。ssh框架项
- springboot 中各种配置项纪录1. @Value最早获取配置文件中的配置的时候,使用的就是这个注解,SpEL表达式语言。// 使用起
- disruptor不过多介绍了,描述下当前的业务场景,两个应用A,B,应用 A 向应用 B 传递数据 . 数据传送比较快,如果用http直接
- 在 Android 系统中,一般使用 AudioRecord 或者 MediaRecord 来采集音频。AudioRecord 是一个比较偏
- 示例1项目结构代码controller中 UserController.javapackage com.example.demo1110.c
- 这篇文章主要介绍了spring boot 2整合swagger-ui过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 我们知道,Maven 是通过仓库对依赖进行管理的,当 Maven 项目需要某个依赖时,只要其 POM 中声明了依赖的坐标信息,Maven 就
- 本文实例为大家分享了OpenCV+Qt实现图像处理操作的具体代码,供大家参考,具体内容如下一、目标Qt界面实现 雪花屏 高斯模糊 中值滤波
- 前言本篇文章 中写到的是 flutter 调用了Android 原生的 TextView 案例添加原生组件的流程基本上可以描述为:1 and
- 方法一:递归算法/// <summary>/// 一列数的规则如下: 1、1、2、3、5、8、13、21、34求第30位数是多少
- 在说struts2的线程安全之前,先说一下,什么是线程安全?这是一个网友讲的。如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会
- 1.微信配置信息 global.properties2.方法wxpay用于生成预支付订单信息方法notifyWeiXinPay用于微信支付成
- 预加载bean在springBoot启动过程中就完成创建加载在AbstractApplicationContext的refresh方法中//
- foreach嵌套使用if标签对象取值问题最近做项目过程中,涉及到需要在 Mybatis 中 使用 foreach 进行循环读取传入的查询条
- 本文实例为大家分享了springboot实现异步任务的具体代码,供大家参考,具体内容如下1.什么异步任务同步:一定要等任务执行完了,得到结果