AnimationsPage.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Reactive.Linq;
  2. using Avalonia;
  3. using Avalonia.Animation;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Shapes;
  6. using Avalonia.Data;
  7. using Avalonia.Input;
  8. using Avalonia.Markup.Xaml;
  9. using Avalonia.Media;
  10. namespace RenderTest.Pages
  11. {
  12. public class AnimationsPage : UserControl
  13. {
  14. public AnimationsPage()
  15. {
  16. this.InitializeComponent();
  17. this.CreateAnimations();
  18. }
  19. private void InitializeComponent()
  20. {
  21. AvaloniaXamlLoader.Load(this);
  22. }
  23. private void CreateAnimations()
  24. {
  25. const int Count = 100;
  26. var panel = new WrapPanel();
  27. for (var i = 0; i < Count; ++i)
  28. {
  29. Ellipse ellipse;
  30. var element = new Panel
  31. {
  32. Children =
  33. {
  34. (ellipse = new Ellipse
  35. {
  36. Name = $"ellipse{i}",
  37. Width = 100,
  38. Height = 100,
  39. Fill = Brushes.Blue,
  40. }),
  41. new Path
  42. {
  43. Data = StreamGeometry.Parse(
  44. "F1 M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z"),
  45. Fill = Brushes.Green,
  46. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
  47. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center,
  48. RenderTransform = new ScaleTransform(2, 2),
  49. }
  50. },
  51. Margin = new Thickness(4),
  52. RenderTransform = new ScaleTransform(),
  53. };
  54. var start = Animate.Stopwatch.Elapsed;
  55. var index = i % (Count / 2);
  56. var degrees = Animate.Timer
  57. .Select(x => (x - start).TotalSeconds)
  58. .Where(x => (x % Count) >= index && (x % Count) < index + 1)
  59. .Select(x => (x % 1) / 1);
  60. element.RenderTransform.Bind(
  61. ScaleTransform.ScaleXProperty,
  62. degrees,
  63. BindingPriority.Animation);
  64. ellipse.PointerEnter += Ellipse_PointerEnter;
  65. ellipse.PointerLeave += Ellipse_PointerLeave;
  66. panel.Children.Add(element);
  67. }
  68. Content = panel;
  69. }
  70. private void Ellipse_PointerEnter(object sender, PointerEventArgs e)
  71. {
  72. ((Ellipse)sender).Fill = Brushes.Red;
  73. }
  74. private void Ellipse_PointerLeave(object sender, PointerEventArgs e)
  75. {
  76. ((Ellipse)sender).Fill = Brushes.Blue;
  77. }
  78. }
  79. }