2017/07/13

[C#] WPF Transform Animation

針對一些WPF的物件會常用到的變形寫法
使用的套件是using System.Windows.Media;



source code:
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Threading;
  7.  
  8. namespace TransformSample
  9. {
  10. public partial class MainWindow : Window
  11. {
  12. private int frame_count = 0;
  13. private double fps = 30.0;
  14.  
  15. private DispatcherTimer timer_main;
  16.  
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20.  
  21. timer_main = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1.0 / fps) };
  22. timer_main.Tick += TimerMainTick;
  23. timer_main.Start();
  24. }
  25.  
  26. private void TimerMainTick(object sender, EventArgs e)
  27. {
  28. frame_count++;
  29. label_count.Content = frame_count;
  30.  
  31. TranslateObjectFunc(image_translate, frame_count);
  32. ScaleObjectFunc(image_sacle, frame_count);
  33. RotateObjectFunc(image_rotate, frame_count);
  34. }
  35.  
  36. private void TranslateObjectFunc(Image image, double parament)
  37. {
  38. parament = 5.0 * parament % 50.0;
  39. TranslateTransform tt = new TranslateTransform(0, parament);
  40. image.RenderTransform = tt;
  41. }
  42.  
  43. private void ScaleObjectFunc(Image image, double parament)
  44. {
  45. double ratio = 20.0;
  46. parament = (parament % ratio) / ratio;
  47. ScaleTransform st = new ScaleTransform(parament, parament);
  48. image.RenderTransformOrigin = new Point(0.5, 0.5); // object center
  49. image.RenderTransform = st;
  50. }
  51.  
  52. private void RotateObjectFunc(Image image, double parament)
  53. {
  54. parament = parament % 360;
  55. RotateTransform rt = new RotateTransform(parament);
  56. image.RenderTransformOrigin = new Point(0.5, 0.5); // object center
  57. image.RenderTransform = rt;
  58. }
  59.  
  60. private void Window_KeyDown(object sender, KeyEventArgs e)
  61. {
  62. if(e.Key == Key.Escape)
  63. {
  64. Application.Current.Shutdown();
  65. }
  66. }
  67. }
  68. }

end

沒有留言:

張貼留言