使用qml创建动画与交互界面

16 篇文章 1 订阅

效果:

 

1,创建一个QtQuickApplication工程,建立如下用户界面

2,代码如下

import QtQuick 2.3
import QtQuick.Window 2.2

Window{
     visible:true;
     width:480;
     height:320;

     Rectangle{
           id:background;
          anchors.fill:parent;
          color:"blue";
        }
     Text{
          text:qsTr("Hello world");
          anchors.centerIn:parent;
          color:"white";
          font.pointSize:15;
           }
}

 3,想矩形对象添加颜色动画

Rectangle{
  id:background;
  anchors.fill:parent;
  color:"blue";
  SequentialAnimation on color
 {
   ColorAnimation{to:"yellow";duration:1000}
   ColorAnimation{to:"red";duration:1000}
   ColorAnimation{to:"blue";duration:1000}
   loops:Animation.Infinite;
 }
}

4,text object添加透明度动画

Text{
    text:qsTr("Hello World");
    anchors.centerIn:parent;
    color:"white";
    font.pointSize:15;
    SequentialAnimation on opacity{
    
     NumberAnimation{to:0.0;duration:200}
     NumberAnimation{to:1.0;duration:200}
     loops:Animation.Infinite;

    }
}

5,添加旋转动画

sequentialanimation on rotation
{
  NumberAnimation{from:0.0;to:360;duration:200}
  loops:Animation.Inifite;
}

6,添加状态,一个是按下状态一个是释放状态。设置默认的状态是释放

state:"RELEASED";
states:[
   State{
         name:"PRESSED"
         PropertyChanges{target:background;color:"blue"}
        },
   State{
          name:"RELEASED"
          PropertyChanges{target:background;color:"red"}
        }
]

7,创建鼠标响应区域。

MouseArea{
    anchors.fill:parent;
    onPressed:background.state="PRESSED";
    onReleased:background.state="RELEASED";
  }

8,添加一些过度

transitions:
[
     Transition
     {
       from:"PRESSED"
       to:"RELEASED"
       ColorAnimation{target:background; duration:200}       
     },
     Transition
     {
      from:"RELEASED"
      to:"PRESSED"
      ColorAnimation{target:background;duration:200
      }
     }



]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值