Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫(huà)
本文實(shí)例為大家分享了Silverlight實(shí)現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
主要功能有以下幾點(diǎn):
1、使用動(dòng)畫(huà)屬性驅(qū)動(dòng)圖片運(yùn)動(dòng)動(dòng)畫(huà)
2、圖片循環(huán)到最后一張后會(huì)自動(dòng)循環(huán)
3、當(dāng)鼠標(biāo)放到圖片時(shí)運(yùn)動(dòng)的圖片會(huì)停止,當(dāng)鼠標(biāo)離開(kāi)時(shí)暫停的圖片會(huì)繼續(xù)運(yùn)動(dòng)
4、當(dāng)鼠標(biāo)點(diǎn)擊任何一個(gè)圖片時(shí),該圖片會(huì)顯示真正大小
XAML:
<Grid x:Name="Layout" Background="White"> <Canvas x:Name="canvas" Background="Black" Grid.Row="1" Height="280"> <!--隱藏矩形以外的其它部分--> <Canvas.Clip > <RectangleGeometry x:Name="rg" /> </Canvas.Clip> <StackPanel x:Name="sp" Orientation="Horizontal" ></StackPanel> </Canvas> <Image x:Name="img_Full" Width="640" Height="480" Visibility="Collapsed" MouseLeftButtonUp="img_Full_MouseLeftButtonUp" /> </Grid>
界面由Grid、Canvas、StackPanel和一個(gè)Image組成,Image用來(lái)顯示圖片的真實(shí)尺寸。
public partial class Demo : UserControl { //定義 private Storyboard storyboard; private const double photowidth = 320; private double totalwidth; public Demo() { InitializeComponent(); CreatePhoto(); } /// <summary> /// 創(chuàng)建圖片列表 /// </summary> private void CreatePhoto() { string[] picList = new string[] { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg" }; //創(chuàng)建多組圖片,保證圖片不會(huì)出現(xiàn)空白,因?yàn)镾tackPanel是橫向排列的,這樣就可以把圖片類似模擬的排成一圈 for (int i = 0; i < 3; i++) { //根據(jù)數(shù)組創(chuàng)建圖片 for (int j = 0; j < picList.Length; j++) { UC_pic pic = new UC_pic(); pic.ImageUrl = "../images/photo/" + picList[j]; pic.Width = photowidth; //綁定事件 pic.MouseEnter += new MouseEventHandler(pic_MouseEnter); pic.MouseLeave += new MouseEventHandler(pic_MouseLeave); pic.MouseLeftButtonUp += new MouseButtonEventHandler(pic_MouseLeftButtonUp); //添加對(duì)象到StackPanel中 sp.Children.Add(pic); } } //計(jì)算圖片的總寬度 totalwidth = -1.0 * photowidth * picList.Length; Canvas.SetLeft(sp, totalwidth); //調(diào)用初始化 方法 CreateStoryboard(); //播放動(dòng)畫(huà) storyboard.Begin(); //重新繪制區(qū)域 Resize(); } /// <summary> /// 創(chuàng)建故事面板 /// </summary> private void CreateStoryboard() { //創(chuàng)建故事面板 storyboard = new Storyboard(); DoubleAnimation animation = new DoubleAnimation(); //設(shè)置動(dòng)畫(huà)延時(shí) animation.Duration = new Duration(TimeSpan.FromSeconds(2.0)); //設(shè)置對(duì)象的作用屬性 Storyboard.SetTarget(animation, sp); Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Left)", new object[0])); //添加到動(dòng)畫(huà)故事板內(nèi) storyboard.Children.Add(animation); //動(dòng)畫(huà)自動(dòng)完成事件 storyboard.Completed += new EventHandler(storyboard_Completed); } //動(dòng)畫(huà)自動(dòng)完成事件,當(dāng)動(dòng)畫(huà)播放完成(結(jié)束)的時(shí)候。再次循環(huán)動(dòng)畫(huà) void storyboard_Completed(object sender, EventArgs e) { DoubleAnimation animation = (DoubleAnimation)storyboard.Children[0]; //取得圖片當(dāng)前位置 double left = Canvas.GetLeft(sp); //如果圖片已接近最后,就重新設(shè)置位置 if (left > (totalwidth - photowidth)) { animation.From = new double?(left); } //設(shè)置動(dòng)畫(huà)的起始值(From)所依據(jù)的總量(總長(zhǎng)度) animation.By = new double?(totalwidth); //循環(huán)動(dòng)畫(huà) storyboard.Begin(); } private void Resize() { //重新繪制顯示區(qū)域 rg.Rect = new Rect(0, 0, this.ActualWidth, 260); } void pic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //顯示放大圖片 UC_pic pic = sender as UC_pic; img_Full.Source = pic.photo.Source; img_Full.Visibility = Visibility.Visible; } void pic_MouseLeave(object sender, MouseEventArgs e) { //繼續(xù)動(dòng)畫(huà) storyboard.Resume(); } void pic_MouseEnter(object sender, MouseEventArgs e) { //暫停動(dòng)畫(huà) storyboard.Pause(); } private void img_Full_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //隱藏放大圖片 img_Full.Visibility = Visibility.Collapsed; } private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { //動(dòng)畫(huà)根據(jù)屏幕大小改變而改變 Resize(); } }
同時(shí)還有一個(gè)UserControl用來(lái)承載圖片代碼如下:
<Canvas x:Name="LayoutRoot" Background="White"> <Image x:Name="photo" Width="320" Height="240" Stretch="UniformToFill" Margin="10" /> </Canvas>
C#:
public partial class UC_pic : UserControl { public UC_pic() { InitializeComponent(); } private string _imgUrl; public string ImageUrl { get { return this._imgUrl; } set { //設(shè)置圖片資源屬性 this._imgUrl = value; Uri uri = new Uri(value, UriKind.Relative); BitmapImage bitimg = new BitmapImage(uri); this.photo.Source = bitimg; } } }
這樣就完成了跑馬燈的效果,如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04unity3d?對(duì)接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能
workerman?是一款開(kāi)源高性能?PHP?應(yīng)用容器,他除了用于互聯(lián)網(wǎng)、即時(shí)通訊、APP?開(kāi)發(fā)、硬件通訊、智能家居、物聯(lián)網(wǎng)等領(lǐng)域的開(kāi)發(fā)外,這篇文章主要介紹了unity3d?對(duì)接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲,需要的朋友可以參考下2022-10-10C#操作DataTable方法實(shí)現(xiàn)過(guò)濾、取前N條數(shù)據(jù)及獲取指定列數(shù)據(jù)列表的方法
這篇文章主要介紹了C#操作DataTable方法實(shí)現(xiàn)過(guò)濾、取前N條數(shù)據(jù)及獲取指定列數(shù)據(jù)列表的方法,實(shí)例分析了C#操作DataTable的各種常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫(kù),數(shù)據(jù)表的增刪改查的實(shí)現(xiàn)
本篇文章小編為大家介紹,Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫(kù),數(shù)據(jù)表的增刪改查的實(shí)現(xiàn)。需要的朋友參考下2013-04-04