DoubleAnimation 创建动画问题

adonis_net 2011-05-11 03:21:53
步骤1:创建一个Rectangle.
代码:
//初始化bullet
private void bulletInit(string bulletType)
{

rect = new Rectangle();
var imageBrush = new ImageBrush();
imageBrush.Stretch = Stretch.None;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.AlignmentY = AlignmentY.Top;
switch (bulletType)
{
case "green":
imageBrush.ImageSource = new BitmapImage(new Uri("/SLADdemo;component/" + bullet[0], UriKind.Relative));
break;
case "ice":
imageBrush.ImageSource = new BitmapImage(new Uri("/SLADdemo;component/" + bullet[1], UriKind.Relative));
break;
case "fire":
imageBrush.ImageSource = new BitmapImage(new Uri("/SLADdemo;component/" + bullet[2], UriKind.Relative));
break;
}

rect.Fill = imageBrush;
rect.Width = 47;
rect.Height = 47;
LayoutRoot.Children.Add(rect);
Canvas.SetLeft(rect, 150);
Canvas.SetTop(rect, 118);
rect.Visibility = System.Windows.Visibility.Collapsed;

}

步骤2:给矩形创建动画
代码:

//显示
rect.Visibility = System.Windows.Visibility.Visible;

//新建动画
storyboard = new Storyboard();




//创建X轴方向动画

DoubleAnimation da = new DoubleAnimation();


Duration dur = new Duration(TimeSpan.FromMilliseconds(1000));

da.Duration = dur;
da.From = Canvas.GetLeft(rect);
da.To = 800;

Storyboard.SetTarget(da, rect);

Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)"));

storyboard.Children.Add(da);


//将动画动态加载进资源内

if (!Resources.Contains("rectAnimation"))
{

Resources.Add("rectAnimation", storyboard);


}

//动画播放

storyboard.Begin();


bulletInit("ice");



问题:动画能正常运行,但是运行过程中图像回时不时往回弹,不是很平滑,请问这是什么原因?
...全文
118 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
Storyboard.SetTarget(da, rect);

Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)"));
你这样设置了,动画可以运行,那你这个rect图像肯定也会随动画而播放啊!

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧