Blog de Márgori

Desde mi ser para el mundo.

Archivo de Noviembre 2008

Animación perpetua en Silverlight

Publicado por margori en Sab 1 dUTC Noviembre dUTC 2008

Estimados amigos:

Si bien me he demorado en realizar una nueva entrada en este blog, es un exelente momento para que comparta con ustedes los resultados de una nueva investigación que realicé sobre animaciones en Silverlight: como hacer una animación armada por código con continuidad perpetua.

He aquí el codigo:

- Page.xaml

<UserControl x:Class=”Circle.Page”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
Width=”640″ Height=”480″>
<Grid x:Name=”LayoutRoot” Background=”White”>
<Path Fill=”Blue”>
<Path.Data>
<EllipseGeometry x:Name=”ellipse”
Center=”320,240″ RadiusX=”5″ RadiusY=”5″ />
</Path.Data>
</Path>
</Grid>
</UserControl>

- Page.xaml.cs

using …;

namespace Circle
{
public partial class Page : UserControl
{
const int rpm = 2;
const int frameTime = 1000/30; // in milliseconds;
Duration frameDuration = new Duration(TimeSpan.FromMilliseconds(frameTime));
double angle = 0.0;
double radius = 200;
double angleDelta = 0.1;
Point beforePoint = new Point();
Storyboard storyBoard = new Storyboard();
PointAnimation animation = new PointAnimation();

public Page()
{
InitializeComponent();

Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)
{
InitStoryBoard();
}

private void InitStoryBoard()
{
animation.BeginTime = new TimeSpan(0);
animation.Duration = frameDuration;
storyBoard.Children.Add(animation);

Storyboard.SetTarget(animation, ellipse);
Storyboard.SetTargetProperty(animation, new PropertyPath(“Center”));

storyBoard.Duration = frameDuration;
LayoutRoot.Resources.Add(“story”, storyBoard);
storyBoard.Completed += new EventHandler(storyBoard_Completed);

NextSequence();
storyBoard.Begin();
}

void storyBoard_Completed(object sender, EventArgs e)
{
storyBoard.Stop();
NextSequence();
storyBoard.Begin();
}

private void NextSequence()
{
double newX;
double newY;

angle += angleDelta;
newX = this.Width / 2 + radius * Math.Cos(angle);
newY = this.Height / 2 + radius * Math.Sin(angle);

animation.From = beforePoint;
animation.To = new Point(newX, newY);
beforePoint = (Point)animation.To;
}

}
}

Este código fue probado en Windows XP, Visual Studio 2008, Silverlight Tool Kit 2.0.

Espero les sirva.

Saludos. Marcelo.

Publicado en Software | Etiquetado: , | Deja un Comentario »