demo_view_animation.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/view.h>
  4. #include "demo_view.h"
  5. /*
  6. * 直接在DC上绘图以实现动画效果
  7. *
  8. * 动画是依赖于定时器驱动的,会上下翻滚显示文字
  9. * "飞线乱飞"
  10. */
  11. static rt_int8_t dx = 1, dy = 1;
  12. static rtgui_rect_t text_rect;
  13. static rtgui_timer_t *timer;
  14. void timeout(struct rtgui_timer* timer, void* parameter)
  15. {
  16. struct rtgui_dc* dc;
  17. rtgui_rect_t rect;
  18. rtgui_widget_t *widget;
  19. /* 控件(view)通过parameter参数传递给定时器 */
  20. widget = (rtgui_widget_t*)parameter;
  21. /* 获得控件所属的DC */
  22. dc = rtgui_dc_begin_drawing(widget);
  23. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  24. return ;
  25. /* 获得demo view允许绘图的区域,主要用于判断边界 */
  26. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  27. rect.y2 -= 5;
  28. /* 判断是否是第一次绘图 */
  29. if ((text_rect.x1 == 0) && (text_rect.y1 == 0))
  30. {
  31. rtgui_rect_moveto(&text_rect, rect.x1, rect.y1);
  32. }
  33. else
  34. {
  35. /* 擦除老的文字 */
  36. rtgui_dc_fill_rect(dc, &text_rect);
  37. }
  38. /* 设置dx和dy */
  39. if (text_rect.x2 >= rect.x2) dx = -1;
  40. if (text_rect.x1 < rect.x1) dx = 1;
  41. if (text_rect.y2 >= rect.y2) dy = -1;
  42. if (text_rect.y1 < rect.y1) dy = 1;
  43. /* 移动文本框的位置 */
  44. text_rect.x1 += dx; text_rect.x2 += dx;
  45. text_rect.y1 += dy; text_rect.y2 += dy;
  46. /* 绘图 */
  47. rtgui_dc_draw_text(dc, "飞线乱飞", &text_rect);
  48. /* 绘图完成 */
  49. rtgui_dc_end_drawing(dc);
  50. }
  51. rt_bool_t animation_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  52. {
  53. if (event->type == RTGUI_EVENT_PAINT)
  54. {
  55. struct rtgui_dc* dc;
  56. rtgui_rect_t rect;
  57. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  58. rtgui_view_event_handler(widget, event);
  59. /* 获得控件所属的DC */
  60. dc = rtgui_dc_begin_drawing(widget);
  61. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  62. return RT_FALSE;
  63. /* 获得demo view允许绘图的区域 */
  64. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  65. /* 擦除所有 */
  66. rtgui_dc_fill_rect(dc, &rect);
  67. /* 绘图 */
  68. rtgui_dc_draw_text(dc, "飞线乱飞", &text_rect);
  69. /* 绘图完成 */
  70. rtgui_dc_end_drawing(dc);
  71. }
  72. else
  73. {
  74. /* 调用默认的事件处理函数 */
  75. return rtgui_view_event_handler(widget, event);
  76. }
  77. return RT_FALSE;
  78. }
  79. rtgui_view_t *demo_view_animation(rtgui_workbench_t* workbench)
  80. {
  81. rtgui_view_t *view;
  82. view = demo_view(workbench, "DC 动画");
  83. if (view != RT_NULL)
  84. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), animation_event_handler);
  85. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(view)), "飞线乱飞", &text_rect);
  86. rtgui_rect_moveto(&text_rect, 0, 45);
  87. /* 启动定时器以触发动画 */
  88. timer = rtgui_timer_create(2, RT_TIMER_FLAG_PERIODIC, timeout, (void*)view);
  89. rtgui_timer_start(timer);
  90. return view;
  91. }