MainWindow.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Linq;
  5. using System.Reactive.Linq;
  6. using Avalonia;
  7. using Avalonia.Animation;
  8. using Avalonia.Controls;
  9. using Avalonia.Controls.Shapes;
  10. using Avalonia.Data;
  11. using Avalonia.Input;
  12. using Avalonia.Layout;
  13. using Avalonia.Markup.Xaml;
  14. using Avalonia.Media;
  15. using Avalonia.Rendering;
  16. using Avalonia.VisualTree;
  17. namespace RenderTest
  18. {
  19. public class MainWindow : Window
  20. {
  21. public MainWindow()
  22. {
  23. this.InitializeComponent();
  24. this.CreateAnimations();
  25. this.AttachDevTools();
  26. Renderer.DrawFps = true;
  27. }
  28. private void InitializeComponent()
  29. {
  30. AvaloniaXamlLoader.Load(this);
  31. }
  32. private void CreateAnimations()
  33. {
  34. const int Count = 100;
  35. var panel = new WrapPanel();
  36. for (var i = 0; i < Count; ++i)
  37. {
  38. Ellipse ellipse;
  39. var element = new Panel
  40. {
  41. Children =
  42. {
  43. (ellipse = new Ellipse
  44. {
  45. Name = $"ellipse{i}",
  46. Width = 100,
  47. Height = 100,
  48. Fill = Brushes.Blue,
  49. }),
  50. new Path
  51. {
  52. Data = StreamGeometry.Parse(
  53. "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"),
  54. Fill = Brushes.Green,
  55. HorizontalAlignment = HorizontalAlignment.Center,
  56. VerticalAlignment = VerticalAlignment.Center,
  57. RenderTransform = new ScaleTransform(2, 2),
  58. }
  59. },
  60. Margin = new Thickness(4),
  61. RenderTransform = new ScaleTransform(),
  62. };
  63. var start = Animate.Stopwatch.Elapsed;
  64. var index = i % (Count / 2);
  65. var degrees = Animate.Timer
  66. .Select(x => (x - start).TotalSeconds)
  67. .Where(x => (x % Count) >= index && (x % Count) < index + 1)
  68. .Select(x => (x % 1) / 1);
  69. element.RenderTransform.Bind(
  70. ScaleTransform.ScaleXProperty,
  71. degrees,
  72. BindingPriority.Animation);
  73. ellipse.PointerEnter += Ellipse_PointerEnter;
  74. ellipse.PointerLeave += Ellipse_PointerLeave;
  75. panel.Children.Add(element);
  76. }
  77. Content = panel;
  78. }
  79. private void Ellipse_PointerEnter(object sender, PointerEventArgs e)
  80. {
  81. ((Ellipse)sender).Fill = Brushes.Red;
  82. }
  83. private void Ellipse_PointerLeave(object sender, PointerEventArgs e)
  84. {
  85. ((Ellipse)sender).Fill = Brushes.Blue;
  86. }
  87. }
  88. }