Evgenii Legotckoi
Jan. 4, 2016, 9:41 p.m.

QML - Lesson 022. Animation click on an item in the list Material Design style

QML does not provide animation interactions clicks Material Design Style for Android by default, but is easily adjusted with the Rectangle primitive. Animation is to one parent, Rectangle, when clicked, you need a second child Rectangle object to stretch the entire area of the parent. At the same time the child object will stretch for a certain time and will appear as an expanding circle, but it will not go beyond its parent.

For clarity, create a list of items, which will produce clicks. To track clicks is used area MouseArea, which will be monitored by the interaction of several signals:

  • onClicked - the signal will stop the animation and the result of the interaction performed with the list;
  • onPressed - when the signal is pressing need to run an animation preset coordinates animated object Rectangle.
  • onReleased - you need to stop the animation when you release the list item;
  • onPositionChanged - when you change the position of the field also need to stop the animation.

To make the animation used PropertyAnimation object. This site is chosen goal of animation, and a list of properties that will be subject to change. In the case of animatable object Rectangl, it is necessary to expand the circle, for that increase the properties width, height and radius with the same value. In order to completely fill the parent object properties will exhibit a finite amount three times greater than the width of the parent element.

Another important property is the parent element:

clip - activating this property (true), we cut the child elements of the object, so that they do not go beyond the boundaries of the parent element.


Click Animation - Implementation of the code

  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3.  
  4. ApplicationWindow {
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9.  
  10. /* Create a list with several items, which will be animated click
  11. * */
  12. ListView {
  13. anchors.fill: parent
  14.  
  15. // In the object ...
  16. delegate: Item {
  17. height: 48
  18. anchors.left: parent.left
  19. anchors.right: parent.right
  20.  
  21. // ... rectangular background locates,
  22. Rectangle {
  23. id: body
  24. anchors.fill: parent
  25. color: "white"
  26. // child objects will be cut over the area of the parent element
  27. clip: true
  28.  
  29. /* ... which is a different background, which will be animated circle clique
  30. * */
  31. Rectangle {
  32. id: colorRect
  33. height: 0
  34. width: 0
  35. color: "#e8e8e8"
  36.  
  37. /* Property transformation, which will be recalculated starting position,
  38.   * that there was a circle in the center of the click
  39. * */
  40. transform: Translate {
  41. x: -colorRect.width / 2
  42. y: -colorRect.height / 2
  43. }
  44. }
  45.  
  46. Text {
  47. text: fragment
  48. anchors.left: parent.left
  49. anchors.leftMargin: 72
  50. anchors.top: parent.top
  51. anchors.bottom: parent.bottom
  52. font.pixelSize: 14
  53.  
  54. renderType: Text.NativeRendering
  55. horizontalAlignment: Text.AlignLeft
  56. verticalAlignment: Text.AlignVCenter
  57. }
  58.  
  59. // Area click on the item
  60. MouseArea {
  61. anchors.fill: parent
  62.  
  63. onClicked: {
  64. circleAnimation.stop()
  65. }
  66. onPressed: {
  67. /* When you click on an element exhibiting the starting coordinates
  68.   * of the background for the animation range in element
  69. * */
  70. colorRect.x = mouseX
  71. colorRect.y = mouseY
  72. // Запускаем анимацию
  73. circleAnimation.start()
  74. }
  75. onReleased: circleAnimation.stop()
  76. onPositionChanged: circleAnimation.stop()
  77. }
  78.  
  79. PropertyAnimation {
  80. id: circleAnimation
  81. target: colorRect // The aim Asking circular background
  82. properties: "width,height,radius" // In animation, change the height, width and radius
  83. from: 0 // Change the settings from 0 pixels ...
  84. to: body.width*3 // ... to triple the size of the width of the item in the list
  85. duration: 300 // within 300 milliseconds
  86.  
  87. // By stopping the animation zero out the height and width of an animated background
  88. onStopped: {
  89. colorRect.width = 0
  90. colorRect.height = 0
  91. }
  92. }
  93. }
  94. }
  95. model: listModel
  96. }
  97.  
  98. ListModel {
  99. id: listModel
  100.  
  101. ListElement {fragment: qsTr("1-й Фрагмент")}
  102. ListElement {fragment: qsTr("2-й Фрагмент")}
  103. ListElement {fragment: qsTr("3-й Фрагмент")}
  104. ListElement {fragment: qsTr("4-й Фрагмент")}
  105. ListElement {fragment: qsTr("5-й Фрагмент")}
  106. ListElement {fragment: qsTr("6-й Фрагмент")}
  107. ListElement {fragment: qsTr("7-й Фрагмент")}
  108. }
  109. }

Video

Result Material Design animations in the style is not completely identical to what should be, but it shows the basic idea. Demonstration of work you can see in the video tutorial.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup