WPF实现手风琴式轮播图切换效果

 更新时间:2020年09月01日 13:44:07   作者:RunnerDNA  
这篇文章主要为大家详细介绍了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();
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。 

相关文章

  • C#中的Image控件用法详解与实际应用示例

    C#中的Image控件用法详解与实际应用示例

    在C#应用程序开发中,图像显示是一个常见的需求,无论是创建图形界面还是处理图像数据,System.Windows.Controls.Image控件都是实现这一目标的重要工具,本文将详细介绍Image控件的功能、用法、优化技巧以及一些实际应用示例,需要的朋友可以参考下
    2024-06-06
  • C# 串口接收数据中serialPort.close()死锁的实例

    C# 串口接收数据中serialPort.close()死锁的实例

    下面小编就为大家分享一篇C# 串口接收数据中serialPort.close()死锁的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • Unity使用ScrollRect制作翻页

    Unity使用ScrollRect制作翻页

    这篇文章主要为大家详细介绍了Unity使用ScrollRect制作翻页,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C#实现IDisposable接口释放非托管资源

    C#实现IDisposable接口释放非托管资源

    这篇文章主要为大家介绍了C#实现IDisposable接口释放非托管资源,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • C#实现获取电脑中的端口号和硬件信息

    C#实现获取电脑中的端口号和硬件信息

    这篇文章主要为大家详细介绍了C#实现获取电脑中的端口号和硬件信息的相关方法,文中的示例代码讲解详细,有需要的小伙伴可以参考一下
    2025-01-01
  • C# 中使用Stopwatch计时器实现暂停计时继续计时功能

    C# 中使用Stopwatch计时器实现暂停计时继续计时功能

    这篇文章主要介绍了C# 中使用Stopwatch计时器可暂停计时继续计时,主要介绍stopwatch的实例代码详解,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#实现在服务器端裁剪图片的方法

    C#实现在服务器端裁剪图片的方法

    这篇文章主要介绍了C#实现在服务器端裁剪图片的方法,涉及C#操作图片的相关技巧,需要的朋友可以参考下
    2015-04-04
  • C#编写SqlHelper类

    C#编写SqlHelper类

    在C#中使用ADO.NET连接数据库的时候,每次连接都要编写连接,打开,执行SQL语句的代码,很麻烦,编写一个SqlHelper类,把每次连接都要写的代码封装成方法,把要执行的SQL语句通过参数传进去,可以大大简化编码
    2017-09-09
  • WPF利用TabControl控件实现拖拽排序功能

    WPF利用TabControl控件实现拖拽排序功能

    在UI交互中,拖拽操作是一种非常简单友好的交互,这篇文章主要为大家介绍了WPF如何利用TabControl控件实现拖拽排序功能,需要的小伙伴可以参考一下
    2023-10-10
  • C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法

    C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法

    这篇文章主要介绍了C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法,结合实例形式分析了GetDesktopWindow函数用于获取窗口句柄的具体使用方法与相关注意事项,需要的朋友可以参考下
    2016-08-08

最新评论