WPF实现手风琴式轮播图切换效果
本文实例为大家分享了WPF实现轮播图切换效果的具体代码,供大家参考,具体内容如下
实现效果如下:
步骤:
1、自定义控件MyImageControl
实现图片的裁切和动画的赋值。
publicpartialclassMyImageControl:UserControl { publicstaticreadonlyDependencyPropertyShowImageProperty=DependencyProperty.Register("ShowImage",typeof(BitmapImage),typeof(MyImageControl),newPropertyMetadata(null)); publicBitmapImageShowImage { get{return(BitmapImage)GetValue(ShowImageProperty);} set{SetValue(ShowImageProperty,value);} } publicMyImageControl() { InitializeComponent(); } publicStoryboardstoryboard=newStoryboard(); privateconstintFlipCount=5; BitmapSource[]bitmap=newBitmapSource[FlipCount]; Image[]images=newImage[FlipCount]; publicvoidGetHorizontalFlip() { intpartImgWidth=(int)this.ShowImage.PixelWidth; intpartImgHeight=(int)(this.ShowImage.PixelHeight/FlipCount); for(inti=0;i2、自定义轮播控件
实现图片点击轮播和动画的启动。
publicpartialclassMyRollControl:UserControl { publicMyRollControl() { InitializeComponent(); } //////是否开始滚动 /// publicboolisBegin=false; //////本轮剩余滚动数 /// publicintrollNum=0; privateList_ls_images; /// ///滚动图片组 /// publicListls_images { set { if(rollNum>0) { //本轮滚动未结束 } else { //开始新的一轮滚动 _ls_images=value; rollNum=_ls_images.Count(); } } get{return_ls_images;} } privateintn_index=0;//滚动索引 /// ///启动 /// publicvoidBegin() { if(!isBegin) { isBegin=true; this.ResetStory(); this.controlFront.GetHorizontalFlip(); } } //////初始化图片 /// voidResetStory() { 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]; } } privatevoidmainGrid_MouseDown(objectsender,MouseButtonEventArgse) { 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(); } } elseif(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、主窗体调用后台逻辑
publicpartialclassMainWindow:Window { publicMainWindow() { InitializeComponent(); Listls_adv_img=newList (); List listAdv=GetUserImages(@"C:\Image"); foreach(stringainlistAdv) { BitmapImageimg=newBitmapImage(newUri(a)); ls_adv_img.Add(img); } this.rollImg.ls_images=ls_adv_img; this.rollImg.Begin(); } /// ///获取当前用户的图片文件夹中的图片路径列表(不包含子文件夹) /// privateListGetUserImages(stringpath) { List images=newList (); DirectoryInfodir=newDirectoryInfo(path); FileInfo[]files=GetPicFiles(path,"*.jpg,*.png,*.bmp,",SearchOption.TopDirectoryOnly); if(files!=null) { foreach(FileInfofileinfiles) { images.Add(file.FullName); } } returnimages; } privateFileInfo[]GetPicFiles(stringpicPath,stringsearchPattern,SearchOptionsearchOption) { List ltList=newList (); DirectoryInfodir=newDirectoryInfo(picPath); string[]sPattern=searchPattern.Replace(';',',').Split(','); for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。