Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫
本文實(shí)例為大家分享了Silverlight實(shí)現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
主要功能有以下幾點(diǎn):
1、使用動(dòng)畫屬性驅(qū)動(dòng)圖片運(yùn)動(dòng)動(dòng)畫
2、圖片循環(huán)到最后一張后會自動(dòng)循環(huán)
3、當(dāng)鼠標(biāo)放到圖片時(shí)運(yùn)動(dòng)的圖片會停止,當(dāng)鼠標(biāo)離開時(shí)暫停的圖片會繼續(xù)運(yùn)動(dòng)
4、當(dāng)鼠標(biāo)點(diǎn)擊任何一個(gè)圖片時(shí),該圖片會顯示真正大小
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用來顯示圖片的真實(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)建多組圖片,保證圖片不會出現(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);
//添加對象到StackPanel中
sp.Children.Add(pic);
}
}
//計(jì)算圖片的總寬度
totalwidth = -1.0 * photowidth * picList.Length;
Canvas.SetLeft(sp, totalwidth);
//調(diào)用初始化 方法
CreateStoryboard();
//播放動(dòng)畫
storyboard.Begin();
//重新繪制區(qū)域
Resize();
}
/// <summary>
/// 創(chuàng)建故事面板
/// </summary>
private void CreateStoryboard()
{
//創(chuàng)建故事面板
storyboard = new Storyboard();
DoubleAnimation animation = new DoubleAnimation();
//設(shè)置動(dòng)畫延時(shí)
animation.Duration = new Duration(TimeSpan.FromSeconds(2.0));
//設(shè)置對象的作用屬性
Storyboard.SetTarget(animation, sp);
Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Left)", new object[0]));
//添加到動(dòng)畫故事板內(nèi)
storyboard.Children.Add(animation);
//動(dòng)畫自動(dòng)完成事件
storyboard.Completed += new EventHandler(storyboard_Completed);
}
//動(dòng)畫自動(dòng)完成事件,當(dāng)動(dòng)畫播放完成(結(jié)束)的時(shí)候。再次循環(huán)動(dòng)畫
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)畫的起始值(From)所依據(jù)的總量(總長度)
animation.By = new double?(totalwidth);
//循環(huán)動(dòng)畫
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)畫
storyboard.Resume();
}
void pic_MouseEnter(object sender, MouseEventArgs e)
{
//暫停動(dòng)畫
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)畫根據(jù)屏幕大小改變而改變
Resize();
}
}
同時(shí)還有一個(gè)UserControl用來承載圖片代碼如下:
<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;
}
}
}
這樣就完成了跑馬燈的效果,如圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)調(diào)用迅雷下載的方法
這篇文章主要介紹了C#實(shí)現(xiàn)調(diào)用迅雷下載的方法,非常實(shí)用的一個(gè)技巧,對于進(jìn)行C#程序設(shè)計(jì)有很好的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
C#?實(shí)現(xiàn)Ping遠(yuǎn)程主機(jī)功能及代碼演示
這篇文章主要介紹了C#?實(shí)現(xiàn)Ping遠(yuǎn)程主機(jī)功能,本教程將演示1.0.2版本更新功能,以及實(shí)現(xiàn)的具體代碼演示,需要的朋友可以參考下2024-05-05
c#使用htmlagilitypack解析html格式字符串
這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下2014-03-03
C#匿名方法與Delegate類型轉(zhuǎn)換錯(cuò)誤分析
這篇文章主要介紹了C#匿名方法與Delegate類型轉(zhuǎn)換錯(cuò)誤,較為詳細(xì)的分析了C#匿名方法的用法及Delegate類型轉(zhuǎn)換錯(cuò)誤問題解決方法,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11

