- Começa-mos por criar a nossa aplicação Silverlight ,adiciona-se um controlo de utilizador para criamos a nossa bolha ,de seguida adicionamos uma imagem com a nossa bolha e que no meu caso saiu algo assim:
- De seguida criamos o nosso movimento para a nossa bolha usando a função de animação Quadratic Out ;
- Voltamos de volta a nosso controlo principal adicionamos a imagem da nossa bebida e uma “slider” para poder-mos controlar a intensidade das bolhas
- Agora vamos ao código no qual criamos um “timer” que nos prometerá criar de tempo em tempo novas bolhas essas bolhas serão posicionados inicialmente de forma aleatória bem como terão uma escala diferente para finalizar é invocando o nosso movimento e é associado um método para que quando esta finaliza o movimento a remover o nosso objecto da aplicação.
private Random NextDouble = new Random(); private Storyboard sb = new Storyboard(); Dictionary<string, Bubble> objectos = new Dictionary<string, Bubble>(); private int count = 1; public MainPage() { InitializeComponent(); DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(0.5); timer.Tick += new EventHandler(timer_Tick); timer.Start(); } private void createBubles() { for (int i = 0; i < bubblesCount.Value; i++) { count++; Bubble bubbleUP = new Bubble(); double scale = .20 + NextDouble.NextDouble() ; double alpha = .1 + NextDouble.NextDouble(); ScaleTransform scaleT = new ScaleTransform() { ScaleX = scale, ScaleY = scale }; bubbleUP.RenderTransform = scaleT; bubbleUP.Opacity = alpha; LayoutRoot.Children.Add(bubbleUP); double y = NextDouble.Next(410); double x = NextDouble.Next(455); Canvas.SetLeft(bubbleUP,y); Canvas.SetTop(bubbleUP, x); if ((i % 2) == 1) { bubbleUP.up.SetValue(Storyboard.TargetNameProperty, "ID_" + count); bubbleUP.up.Begin(); bubbleUP.up.Completed += new EventHandler(up_Completed); } else { bubbleUP.up2.SetValue(Storyboard.TargetNameProperty, "ID_" + count); bubbleUP.up2.Begin(); bubbleUP.up2.Completed += new EventHandler(up_Completed); } objectos.Add("ID_" + count, bubbleUP); } } void up2_Completed(object sender, EventArgs e) { if (sender != null && typeof(Bubble) == sender.GetType()) { Bubble b = (Bubble)sender; b.up.Stop(); b.IsEnabled = false; b.Visibility = Visibility.Collapsed; LayoutRoot.Children.Remove(b); this.Resources.Remove(b); Application.Current.RootVisual.UpdateLayout(); sender = null; b = null; } else { if (typeof(Storyboard) == sender.GetType()) { Storyboard s = (Storyboard)sender; if (s == null) return; string spriteName = s.GetValue(Storyboard.TargetNameProperty).ToString(); if (objectos.ContainsKey(spriteName)) { Bubble b = objectos[spriteName]; b.up2.Stop(); b.IsEnabled = false; b.Visibility = Visibility.Collapsed; LayoutRoot.Children.Remove(b); Application.Current.RootVisual.UpdateLayout(); sender = null; b = null; } else { FrameworkElement sprite = (FrameworkElement)LayoutRoot.FindName(spriteName); LayoutRoot.Children.Remove(sprite); LayoutRoot.Resources.Remove(spriteName); } } } } void timer_Tick(object sender, EventArgs e) { createBubles(); } void sb_Completed(object sender, EventArgs e) { Bubble bubbleUP = new Bubble(); double scale = .01 + NextDouble.NextDouble() * 2; double alpha = .1 + NextDouble.NextDouble(); bubbleUP.Opacity = alpha; LayoutRoot.Children.Add(bubbleUP); Canvas.SetLeft(bubbleUP, NextDouble.Next(410)); Canvas.SetTop(bubbleUP, NextDouble.Next(455)); bubbleUP.up.Begin(); bubbleUP.up.Completed += new EventHandler(up_Completed); sb.Begin(); } void up_Completed(object sender, EventArgs e) { if (sender != null && typeof(Bubble) == sender.GetType()) { Bubble b = (Bubble)sender; b.up.Stop(); b.IsEnabled = false; b.Visibility = Visibility.Collapsed; LayoutRoot.Children.Remove(b); this.Resources.Remove(b); Application.Current.RootVisual.UpdateLayout(); sender = null; b = null; } else { if (typeof(Storyboard) == sender.GetType()) { Storyboard s = (Storyboard)sender; if (s == null) return; string spriteName = s.GetValue(Storyboard.TargetNameProperty).ToString(); if (objectos.ContainsKey(spriteName)) { Bubble b = objectos[spriteName]; b.up.Stop(); b.IsEnabled = false; b.Visibility = Visibility.Collapsed; LayoutRoot.Children.Remove(b); Application.Current.RootVisual.UpdateLayout(); sender = null; b = null; } else { FrameworkElement sprite = (FrameworkElement)LayoutRoot.FindName(spriteName); LayoutRoot.Children.Remove(sprite); LayoutRoot.Resources.Remove(spriteName); } } } } }
Download de Código Fonte:
Sem comentários:
Enviar um comentário