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();
}
}
/// <summary>
/// 初始化图⽚
/// </summary>
void ResetStory()
{
if (this.ls_images.Count > 0)
{
}
}
private void mainGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
if (lFront.storyboard.Children.Count > 0)
{
lBack.storyboard.Children.Count <= 0)
{
Canvas.lFront, 0);
rollNum--;
this.ResetStory();
}
}
else lFront.storyboard.Children.Count <= 0)
{
lBack.storyboard.Children.Count > 0)
{
rollNum--;
this.ResetStory();
Canvas.lFront, -1);
}
}
}
}
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);
}
}
/
// <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);
canvas动画
}
return ltList.ToArray();
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论