Silverlight實現(xiàn)跑馬燈動畫
更新時間:2018年07月04日 14:18:55 作者:拉斐爾-Raphael
這篇文章主要為大家詳細介紹了Silverlight實現(xiàn)跑馬燈動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Silverlight實現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內容如下
主要功能有以下幾點:
1、使用動畫屬性驅動圖片運動動畫
2、圖片循環(huán)到最后一張后會自動循環(huán)
3、當鼠標放到圖片時運動的圖片會停止,當鼠標離開時暫停的圖片會繼續(xù)運動
4、當鼠標點擊任何一個圖片時,該圖片會顯示真正大小
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和一個Image組成,Image用來顯示圖片的真實尺寸。
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)空白,因為StackPanel是橫向排列的,這樣就可以把圖片類似模擬的排成一圈
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);
}
}
//計算圖片的總寬度
totalwidth = -1.0 * photowidth * picList.Length;
Canvas.SetLeft(sp, totalwidth);
//調用初始化 方法
CreateStoryboard();
//播放動畫
storyboard.Begin();
//重新繪制區(qū)域
Resize();
}
/// <summary>
/// 創(chuàng)建故事面板
/// </summary>
private void CreateStoryboard()
{
//創(chuàng)建故事面板
storyboard = new Storyboard();
DoubleAnimation animation = new DoubleAnimation();
//設置動畫延時
animation.Duration = new Duration(TimeSpan.FromSeconds(2.0));
//設置對象的作用屬性
Storyboard.SetTarget(animation, sp);
Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Left)", new object[0]));
//添加到動畫故事板內
storyboard.Children.Add(animation);
//動畫自動完成事件
storyboard.Completed += new EventHandler(storyboard_Completed);
}
//動畫自動完成事件,當動畫播放完成(結束)的時候。再次循環(huán)動畫
void storyboard_Completed(object sender, EventArgs e)
{
DoubleAnimation animation = (DoubleAnimation)storyboard.Children[0];
//取得圖片當前位置
double left = Canvas.GetLeft(sp);
//如果圖片已接近最后,就重新設置位置
if (left > (totalwidth - photowidth))
{
animation.From = new double?(left);
}
//設置動畫的起始值(From)所依據(jù)的總量(總長度)
animation.By = new double?(totalwidth);
//循環(huán)動畫
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ù)動畫
storyboard.Resume();
}
void pic_MouseEnter(object sender, MouseEventArgs e)
{
//暫停動畫
storyboard.Pause();
}
private void img_Full_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//隱藏放大圖片
img_Full.Visibility = Visibility.Collapsed;
}
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
//動畫根據(jù)屏幕大小改變而改變
Resize();
}
}
同時還有一個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 {
//設置圖片資源屬性
this._imgUrl = value;
Uri uri = new Uri(value, UriKind.Relative);
BitmapImage bitimg = new BitmapImage(uri);
this.photo.Source = bitimg;
}
}
}
這樣就完成了跑馬燈的效果,如圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
c#使用htmlagilitypack解析html格式字符串
這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下2014-03-03

