最近有点小忙,没有来得及续写,不好意思,现在完成把工作续完。
(备注此文章代码开发环境为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; }
}
}
另外:如果想下载源码实例的请到我的资源下载: