WPF實(shí)現(xiàn)手風(fēng)琴式輪播圖切換效果
本文實(shí)例為大家分享了WPF實(shí)現(xiàn)輪播圖切換效果的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果如下:
步驟:
1、自定義控件MyImageControl
實(shí)現(xiàn)圖片的裁切和動(dòng)畫(huà)的賦值。
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、自定義輪播控件
實(shí)現(xiàn)圖片點(diǎn)擊輪播和動(dòng)畫(huà)的啟動(dòng)。
public partial class MyRollControl : UserControl { public MyRollControl() { InitializeComponent(); } /// <summary> /// 是否開(kāi)始滾動(dòng) /// </summary> public bool isBegin = false; /// <summary> /// 本輪剩余滾動(dòng)數(shù) /// </summary> public int rollNum = 0; private List<BitmapImage> _ls_images; /// <summary> /// 滾動(dòng)圖片組 /// </summary> public List<BitmapImage> ls_images { set { if (rollNum > 0) { // 本輪滾動(dòng)未結(jié)束 } else { // 開(kāi)始新的一輪滾動(dòng) _ls_images = value; rollNum = _ls_images.Count(); } } get { return _ls_images; } } private int n_index = 0;// 滾動(dòng)索引 /// <summary> /// 啟動(dòng) /// </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、主窗體調(diào)用后臺(tái)邏輯
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> /// 獲取當(dāng)前用戶的圖片文件夾中的圖片路徑列表(不包含子文件夾) /// </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(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解析美國(guó)東部時(shí)間與北京時(shí)間相互轉(zhuǎn)換的實(shí)現(xiàn)代碼
本篇文章是對(duì)美國(guó)東部時(shí)間與北京時(shí)間相互轉(zhuǎn)換的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable
這篇文章介紹了C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法
這篇文章主要介紹了VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法,需要的朋友可以參考下2017-02-02.NET實(shí)現(xiàn)定時(shí)發(fā)送郵件代碼(兩種方式)
經(jīng)常發(fā)郵件的朋友都知道,郵箱有個(gè)特殊功能,可以設(shè)定郵件發(fā)送時(shí)間,定時(shí)發(fā)送,這個(gè)功能是怎么實(shí)現(xiàn)的呢?接下來(lái),小編給大家分享.NET實(shí)現(xiàn)定時(shí)發(fā)送郵件的代碼,有需要的朋友可以參考下2015-08-08