本文用WPF的动画实现一个简单的跑马灯

xmal:

<Window x:Class="wpfstatusBar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpfstatusBar"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="200">
<Grid > <Canvas Name="canva1" ClipToBounds="True" Background="Aquamarine" >
<StackPanel Name="sp1" Background="Aqua" Orientation="Horizontal">
<Label Name="lab1" Content="1111111111111111111111111"/>
<Label Name="lab2" Content="2222222222222222222222222"/>
<Label Name="lab3" Content="3333333333333333333333333"/>
<Label Name="lab4" Content="1111111111111111111111111"/>
</StackPanel>
</Canvas>
</Grid>
</Window>

下面是一个最简单的例子,就让label动起来:

  public MainWindow()
{
InitializeComponent();
storyboard_imgs = new Storyboard();
daukf_img1 = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(daukf_img1, sp1);
Storyboard.SetTargetProperty(daukf_img1, new PropertyPath("(Canvas.Left)"));
LinearDoubleKeyFrame k1_img1 = new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0)));
LinearDoubleKeyFrame k2_img1 = new LinearDoubleKeyFrame(-600, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 3)));
daukf_img1.KeyFrames.Add(k1_img1);
daukf_img1.KeyFrames.Add(k2_img1);
storyboard_imgs.Children.Add(daukf_img1);
storyboard_imgs.Begin();
}

然后进入正题,利用定时器实现一个简单的循环滚动3个Label的跑马灯

关键点在于第三个lab滚动的时候怎么衔接第一个lab,其实很简单,就是多new了一个和第一个lab一样的lab放在最后,

在滚动第四个lab后,利用动画开始后会瞬间将lab定位到初始位置的特性即可完成,当然如果硬要只new三个lab也行,

通过add,remove这样的操作完成,但是会比较复杂,本文应该是实现该功能最简单的算法了!xmal和上面例子一样,cs代码如下:

 public partial class MainWindow : Window
{
DispatcherTimer _timer = new DispatcherTimer(); //UI定时器
Storyboard storyboard_imgs = null;
DoubleAnimationUsingKeyFrames daukf_img1 =null; public MainWindow()
{
InitializeComponent();
_timer.Interval = new TimeSpan(0, 0, 0, 2);
_timer.Tick += TimerElapsed;
storyboard_imgs = new Storyboard();
daukf_img1 = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(daukf_img1, sp1);
Storyboard.SetTargetProperty(daukf_img1, new PropertyPath("(Canvas.Left)"));
_timer.Start();
}
int index = 0;
void TimerElapsed(object o,EventArgs e)
{
double start_left = 0;
double end_left = 0;
index++;
if(index%3==1)
{
end_left = -lab1.ActualWidth;
}
else if(index%3==2)
{
start_left= -lab1.ActualWidth;
end_left = -lab1.ActualWidth-lab2.ActualWidth;
}
else if(index%3==0)
{
start_left = -lab1.ActualWidth - lab2.ActualWidth;
end_left = -lab1.ActualWidth - lab2.ActualWidth-lab3.ActualWidth;
}
daukf_img1.KeyFrames.Clear();
storyboard_imgs.Children.Clear();
LinearDoubleKeyFrame k1_img1 = new LinearDoubleKeyFrame(start_left, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0)));
LinearDoubleKeyFrame k2_img1 = new LinearDoubleKeyFrame(end_left, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 1)));
daukf_img1.KeyFrames.Add(k1_img1);
daukf_img1.KeyFrames.Add(k2_img1);
storyboard_imgs.Children.Add(daukf_img1);
storyboard_imgs.Begin();
}
}

WPF 实现简单的跑马灯的更多相关文章

  1. Android:TextView文字跑马灯的效果实现

    解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_wi ...

  2. Unity3D 文字滚动跑马灯效果

    需求 在日常游戏中,文字滚动效果是比较常用的.例如日常游戏顶部的新闻公告,聊天系统的文字滚动,都属于这个范围. 思路 由于使用的地方比较广泛,所以希望能够尽量独立的游戏之外,能够做到随处使用的功能.N ...

  3. javascript 跑马灯

    1.看了写跑马灯的教程案例,隔了段时间自己写了一个简单的跑马灯.将过程中遇到的问题特此记录下来 代码如下: <!DOCTYPE html> <html> <head> ...

  4. 纯css3跑马灯demo

    我们写跑马灯一般都是用js控制定时器不断循环产生,但是定时器消耗比较大,特别是程序中很多用到定时器的时候,感觉有的时候比较卡.但是css3样式一般不会.这里主要的思路就是用css3代替js定时器实现一 ...

  5. 【跑马灯】纯css3跑马灯demo

    我们写跑马灯一般都是用js控制定时器不断循环产生,但是定时器消耗比较大,特别是程序中很多用到定时器的时候,感觉有的时候比较卡.但是css3样式一般不会.这里主要的思路就是用css3代替js定时器实现一 ...

  6. Arduino 跑马灯

    参考: 1. https://blog.csdn.net/hunhun1122/article/details/70254606 2. http://www.51hei.com/arduino/392 ...

  7. js+css简单效果(幕布,跑马灯)

    2.js普通的盒子,css的优先级 css的优先级 !important >>>>>  style 行内样式  >>>>> #id选择器 # ...

  8. TextView中实现跑马灯的最简单方法

    几行代码实现跑马灯效果,效果如下: 因为很简单,所以就直接贴代码喽 <TextView android:id="@+id/item1_title_message" andro ...

  9. WPF 实现跑马灯效果的Label控件,数据绑定方式实现

    原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...

随机推荐

  1. css3 ---2 属性的选择器

    存在和值属性选择器1:[attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何. [name]{ background: pink; } <!DOCTYPE html& ...

  2. I-country

    I-country 在\(n\times m\)的网格图中,给出每个格子的权值,寻找有k个格子的凸联通块,使包含的权值最大,\(N,M≤15,K≤225\). 解 我们首先要知道凸联通块的定义 从任意 ...

  3. SQL数据表纵横转换

    SELECT DISTINCT '(select b.risk from rhwl_easy_genes_new_risk b where b.genes_id=a.id and b.disease= ...

  4. 容斥原理——hdu2841

    记得要开ll /* 莫比乌斯反演模板题,也可以直接算phi来做 容斥的解法 求x[1..m],在[1,n]中和其互质的数的个数即可 那么就是n-和x不互质的数个数即可 */ #include<b ...

  5. 为什么说 Python 是数据科学的发动机(一)发展历程(附视频中字)

    为什么说 Python 是数据科学的发动机(一)发展历程(附视频中字) 在PyData Seattle 2017中,Jake Vanderplas介绍了Python的发展历程以及最新动态.在这里我们把 ...

  6. PAT甲级——A1100 Mars Numbers

    People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...

  7. 04-6-queryselect

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Elasticsearch & Kibana with Shield

    Elasticsearch & Kibana with Shield   官方网站: https://www.elastic.co/guide/en/kibana/current/produc ...

  9. 理解JWT

    1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...

  10. 2014-VGG-《Very deep convolutional networks for large-scale image recognition》翻译

    2014-VGG-<Very deep convolutional networks for large-scale image recognition>翻译 原文:http://xues ...