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#中DateTime函数的详细用法

    C#中DateTime函数的详细用法

    这篇文章介绍了C#中DateTime函数的详细用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#中参数数组、引用参数和输出参数示例详解

    C#中参数数组、引用参数和输出参数示例详解

    这篇文章主要给大家介绍了关于C#中参数数组、引用参数和输出参数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • C# 10个常用特性汇总

    C# 10个常用特性汇总

    这篇文章主要介绍了C# 10个常用特性,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • Unity实现通用的信息提示框

    Unity实现通用的信息提示框

    这篇文章主要为大家详细介绍了Unity实现通用的信息提示框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • C# Lambda 知识回顾

    C# Lambda 知识回顾

    本文主要介绍了C#中Lambda的相关知识。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C# 循环判断会进来几次的实现代码

    C# 循环判断会进来几次的实现代码

    这篇文章主要介绍了C# 循环判断会进来几次的实现代码,代码中就一个循环,循环的判断是从一个函数获取值,需要的朋友可以参考下
    2018-06-06
  • PowerShell 定时执行.Net(C#)程序的方法

    PowerShell 定时执行.Net(C#)程序的方法

    利用PowerShell可以调用动态页面,然后再用 .bat 执行 PowerShell 脚本,最后把 .bat 添加到服务器的任务计划里面。OK,所有操作都做好了,.Net 定时执行了,是不是呢,有木有呢。
    2013-04-04
  • Unity实现手机摇一摇震动

    Unity实现手机摇一摇震动

    这篇文章主要为大家详细介绍了untiy实现手机摇一摇震动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C#利用Refit实现JWT自动续期详解

    C#利用Refit实现JWT自动续期详解

    Refit 是一个受到Square的Retrofit库(Java)启发的自动类型安全REST库,这篇文章主要为大家介绍了C#如何利用Refit实现JWT自动续期,感兴趣的可以了解下
    2023-08-08
  • C#编程实现简易图片浏览器的方法

    C#编程实现简易图片浏览器的方法

    这篇文章主要介绍了C#编程实现简易图片浏览器的方法,涉及C#基于WinForm操作图片实现预览功能的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11

最新评论