這篇文章運用簡單易懂的例子給大家介紹使用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(); } }
關于使用WPF制作一個手風琴式輪播圖效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。