当前位置:   article > 正文

QtQuick制作滚动呈现渐变动画_qt界面过渡动画

qt界面过渡动画

QtQuick强大的动画系统以及高度自由的特效制作功能,使得制作动画特效尤为简便。

首先我们需要的效果如下图:

 动画从左到右滚动呈现,且未呈现部分有一定的透明度过渡效果,使得滚动更加流畅自然。

制作这个动画需要用到OpenGL片段着色器,来处理最终到达屏幕的颜色,且使用到动画系统,控制又左到右的呈现。

原理图:

点o为当前动画所到达的呈现位置,若文字最左侧为0 最右侧为1,那么点o为0-1的一个浮点数。

最初点o为0点,文字不展示,点o到达最右侧文字完全显示。

点p为当前着色器处理的纹理坐标,渐变长度为gradient,需要考虑到线性渐变,则点p的透明度越靠近点o越低,如果距离点o大于gradient则完全不透明;如果点p距离点o小于gradient,那么点p的透明度计算公式为 t = (o-p)/gradient;如果点p距离;

下面是着色器代码:

  1. uniform float qt_Opacity;
  2. uniform float divider;
  3. uniform float gradient;
  4. uniform sampler2D source;
  5. varying vec2 qt_TexCoord0;
  6. void main(void)
  7. {
  8. float op = divider - qt_TexCoord0.x;
  9. if(op < 0){
  10. gl_FragColor = vec4(0);
  11. return;
  12. }
  13. if(op >= gradient || divider == 1.0) {
  14. gl_FragColor = texture2D(source, qt_TexCoord0) * qt_Opacity;
  15. return;
  16. }
  17. float t = op / gradient;
  18. vec4 rgba = texture2D(source, qt_TexCoord0);
  19. gl_FragColor = rgba * t * qt_Opacity;
  20. }

以下为qml框架代码:

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. width: 1024
  5. height: 480
  6. visible: true
  7. title: qsTr("Hello World")
  8. Rectangle{
  9. anchors.fill: parent
  10. color: "white"
  11. Text{
  12. id: text
  13. anchors.centerIn: parent
  14. font.pixelSize: 48
  15. font.italic: true
  16. color: "green"
  17. text: "Transiation Present Animation"
  18. }
  19. ShaderEffect{
  20. id: shaderEffect
  21. anchors.centerIn: parent
  22. width: text.implicitWidth
  23. height: text.implicitHeight
  24. fragmentShader: "qrc:/transationpresent.frag"
  25. property real divider: 0
  26. property real gradient: 0.2
  27. property variant source: shaderSource
  28. ShaderEffectSource{
  29. id: shaderSource
  30. sourceItem: text
  31. hideSource: true
  32. }
  33. }
  34. NumberAnimation{
  35. target: shaderEffect
  36. property: "divider"
  37. from: 0
  38. to: 1
  39. running: true
  40. duration: 1000
  41. loops: Animation.Infinite
  42. }
  43. }
  44. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/81525
推荐阅读
相关标签
  

闽ICP备14008679号