這篇文章運用簡單易懂的例子給大家介紹使用WPF制作一個手風琴式輪播圖效果,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

實現(xiàn)效果如下:

步驟:
1、自定義控件MyImageControl
實現(xiàn)圖片的裁切和動畫的賦值。
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));
}
}
本文標題:使用WPF制作一個手風琴式輪播圖效果-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://chinadenli.net/article48/shgep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、微信小程序、域名注冊、面包屑導航、虛擬主機、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容