最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫

 更新時(shí)間:2018年07月04日 14:18:55   作者:拉斐爾-Raphael  
這篇文章主要為大家詳細(xì)介紹了Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評論

南雄市| 双鸭山市| 措美县| 赣州市| 枣庄市| 乾安县| 饶河县| 宝清县| 和静县| 屏东县| 西畴县| 犍为县| 门头沟区| 祥云县| 文登市| 兴和县| 易门县| 潮安县| 杨浦区| 广南县| 饶河县| 曲松县| 乌兰浩特市| 雅江县| 左权县| 郸城县| 定边县| 滨海县| 日土县| 东城区| 淅川县| 曲靖市| 铜陵市| 合阳县| 辽宁省| 日照市| 南川市| 高碑店市| 云霄县| 蓬溪县| 水城县|