研究了一段時間siverlight,最近需要實現一個動畫,此動畫是循環滾動的動畫(2)

最近有點小忙,沒有來得及續寫,不好意思,現在完成把工作續完。

(備註此文章代碼開發環境爲vs2010,sl4)

先說說實現思路:

1.一個計時器:用來控制動畫的循環時間的間隔(也就是一條數據循環時間間隔)

2.用一個容器存放數據以及控制數據滾動的範圍(上下邊距)

主要就是以上兩步,下面就是實現動畫了。

 

下面把隨手寫的代碼貼上:(代碼有點亂,沒好好整理)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace sl_rollData
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 動畫控制計時器
        /// </summary>
        private DispatcherTimer rollTimer;
        /// <summary>
        /// 實體集
        /// </summary>
        List<CompEntity> brandlist = new List<CompEntity>();
        List<CompEntity> modellist = new List<CompEntity>();
        /// <summary>
        /// 加載
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //
            for (int i = 0; i < 5; i++)
            {
                CompEntity ce = new CompEntity();
                ce.ID = i;
                ce.Title = "tt" + i.ToString();
                ce.Sum = i + 10;
                ce.Prate = "20%";
                brandlist.Add(ce);
                modellist.Add(ce);
            }

            CreateRollDataTimer();

        }

        /// <summary>
        /// 數據索引
        /// </summary>
        int index = 0;

        /// <summary>
        /// 滾動動畫
        /// </summary>
        /// <param name="BrandTypeList"></param>
        private void BrandDataRoll(List<CompEntity> BrandTypeList)
        {


            if (brandlist == null || (brandlist != null && index > brandlist.Count - 1))
            {
                index = 0;
            }
            StackPanel sp = new StackPanel();
            if (brandlist != null && brandlist.Count > 0)
            {

                sp.Name = "spb" + index.ToString();
                sp.Orientation = Orientation.Horizontal;
                sp.Width = 290;
                sp.Height = 25;

                TextBlock tb_type = new TextBlock();
                tb_type.Height = 25;
                tb_type.Width = 96;
                tb_type.TextAlignment = TextAlignment.Center;
                tb_type.Text = brandlist[index].Title;
                tb_type.Foreground = new SolidColorBrush(Colors.White);

                Rectangle rec1 = new Rectangle();
                rec1.Height = 25;
                rec1.Width = 0.5;
                rec1.Fill = new SolidColorBrush(Colors.Blue);

                TextBlock tb_num = new TextBlock();
                tb_num.Height = 25;
                tb_num.Width = 96;
                tb_num.TextAlignment = TextAlignment.Center;
                tb_num.Text = brandlist[index].Sum.ToString();
                tb_num.Foreground = new SolidColorBrush(Colors.White);

                Rectangle rec2 = new Rectangle();
                rec2.Height = 25;
                rec2.Width = 0.5;
                rec2.Fill = new SolidColorBrush(Colors.Blue);

                TextBlock tb_rate = new TextBlock();
                tb_rate.Height = 25;
                tb_rate.Width = 96;
                tb_rate.TextAlignment = TextAlignment.Center;
                tb_rate.Text = brandlist[index].Prate;
                tb_rate.Foreground = new SolidColorBrush(Colors.White);

                sp.Children.Add(tb_type);
                //sp.Children.Add(rec1);
                sp.Children.Add(tb_num);
                //sp.Children.Add(rec2);
                sp.Children.Add(tb_rate);
                sp.Background = (Brush)this.Resources["spback"];
                if (brand_data.Children.Count > brandlist.Count - 1)
                    brand_data.Children.RemoveAt(0);
                //if (this.brand_data.Children.Count!=0)
                //RegisteredWaitHandle regi = new RegisteredWaitHandle();
                //regi.Unregister(sp);
                //brand_data.RegisterName(stp.Name, stp);

                this.brand_data.Children.Add(sp);
            }

            Storyboard _rollStoryBoard = new Storyboard();

            DoubleAnimation chartAnimation = new DoubleAnimation();
            chartAnimation.From = 90;
            chartAnimation.To = -30;
            chartAnimation.Duration = new Duration(TimeSpan.FromSeconds(7));
            Storyboard.SetTarget(chartAnimation, sp);
            Storyboard.SetTargetProperty(chartAnimation, new PropertyPath(Canvas.TopProperty));
            _rollStoryBoard.FillBehavior = FillBehavior.HoldEnd;
            _rollStoryBoard.Completed += new EventHandler(rollstory_Completed);
            //_rollStoryBoard.RepeatBehavior = RepeatBehavior.Forever;
            _rollStoryBoard.Children.Add(chartAnimation);

            _rollStoryBoard.Begin();


            index++;


        }


        /// <summary>
        /// 動畫執行完畢
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rollstory_Completed(object sender, EventArgs e)
        {
            if (brand_data.Children.Count > brandlist.Count - 1)
                brand_data.Children.RemoveAt(0);
        }

        /// <summary>
        /// 創建計時器
        /// </summary>
        private void CreateRollDataTimer()
        {
            rollTimer = new DispatcherTimer();
            rollTimer.Interval = TimeSpan.FromMilliseconds(1000);
            rollTimer.Tick += new EventHandler(rollTimer_Tick);
            rollTimer.Start();

        }

        void rollTimer_Tick(object sender, EventArgs e)
        {

            BrandDataRoll(brandlist);
            ModelDataRoll();

        }

        /// <summary>
        /// 數據索引
        /// </summary>
        int mindex = 0;
        /// <summary>
        /// 動畫
        /// </summary>
        private void ModelDataRoll()
        {

            if (modellist != null && mindex > modellist.Count - 1)
                mindex = 0;
            StackPanel sp1 = new StackPanel();
            if (modellist != null && modellist.Count > 0)
            {

                sp1.Name = "spm" + mindex.ToString();
                sp1.Orientation = Orientation.Horizontal;
                sp1.Width = 290;
                sp1.Height = 25;

                TextBlock tb_type = new TextBlock();
                tb_type.Height = 25;
                tb_type.Width = 96;
                tb_type.TextAlignment = TextAlignment.Center;
                tb_type.Text = modellist[mindex].Title;

                tb_type.Foreground = new SolidColorBrush(Colors.White);

                Rectangle rec1 = new Rectangle();
                rec1.Height = 25;
                rec1.Width = 0.5;
                rec1.Fill = new SolidColorBrush(Colors.Blue);

                TextBlock tb_num = new TextBlock();
                tb_num.Height = 25;
                tb_num.Width = 96;
                tb_num.TextAlignment = TextAlignment.Center;
                tb_num.Text = modellist[mindex].Sum.ToString();
                tb_num.Foreground = new SolidColorBrush(Colors.White);

                Rectangle rec2 = new Rectangle();
                rec2.Height = 25;
                rec2.Width = 0.5;
                rec2.Fill = new SolidColorBrush(Colors.Blue);

                TextBlock tb_rate = new TextBlock();
                tb_rate.Height = 25;
                tb_rate.Width = 96;
                tb_rate.TextAlignment = TextAlignment.Center;
                tb_rate.Text = modellist[mindex].Prate;
                tb_rate.Foreground = new SolidColorBrush(Colors.White);

                sp1.Children.Add(tb_type);
                //sp.Children.Add(rec1);
                sp1.Children.Add(tb_num);
                //sp.Children.Add(rec2);
                sp1.Children.Add(tb_rate);
                sp1.Background = (Brush)this.Resources["spback"];
                if (model_data.Children.Count > modellist.Count - 1)
                    model_data.Children.RemoveAt(0);
                //if(model_data.Children.Count!=0)
                //DependencyProperty dp = ((DependencyProperty)(model_data)).Register(sp1.Name, typeof(int), typeof(StackPanel), new PropertyMetadata(0, tt));
                this.model_data.Children.Add(sp1);

            }
            Storyboard _rollStoryBoard1 = new Storyboard();

            DoubleAnimation chartAnimation1 = new DoubleAnimation();
            chartAnimation1.From = 120;
            chartAnimation1.To = -50;
            chartAnimation1.Duration = new Duration(TimeSpan.FromSeconds(8));
            Storyboard.SetTarget(chartAnimation1, sp1);
            Storyboard.SetTargetProperty(chartAnimation1, new PropertyPath(Canvas.TopProperty));
            _rollStoryBoard1.FillBehavior = FillBehavior.HoldEnd;
            _rollStoryBoard1.Completed += new EventHandler(rollstory1_Completed);
            _rollStoryBoard1.Children.Add(chartAnimation1);

            _rollStoryBoard1.Begin();
            mindex++;

 

        }
        /// <summary>
        /// 動畫執行完畢
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rollstory1_Completed(object sender, EventArgs e)
        {
            if (model_data.Children.Count > modellist.Count - 1)
                model_data.Children.RemoveAt(0);
        }
    }
}

/// <summary>
/// 中間類,實體類
/// </summary>
public class CompEntity
{
    private int _id;
    private string _title;
    private int _num;
    private string _percentage;
    /// <summary>
    /// 序號
    /// </summary>
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }
    /// <summary>
    /// 種類
    /// </summary>
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
    /// <summary>
    /// 數量
    /// </summary>
    public int Sum
    {
        get { return _num; }
        set { _num = value; }
    }
    /// <summary>
    /// 百分比
    /// </summary>
    public string Prate
    {
        get { return _percentage; }
        set { _percentage = value; }
    }
}

另外:如果想下載源碼實例的請到我的資源下載:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章