用 QML 来实现一个呼吸动画效果的字体,非常酷炫,主要使用 Glow 组件来实现,然后通过控制透明度的改变来实现想要的效果。
mainText.qml
Item {
Rectangle {
anchors.fill: parent
color: "black"
}
Text {
id: text
anchors.fill: parent
text: qsTr("ADBASn你好")
font.bold: true
font.pixelSize: 50
color:"white"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
Glow {
anchors.fill: text
radius:9
samples: 13
color: "#ddd"
source: text
spread: 0.5
opacity: 0.8
NumberAnimation on opacity {
id:an1
to:0.8
duration: 2000
running: true
onStopped: {
an2.start()
}
}
NumberAnimation on opacity {
id:an2
to:0.2
duration: 2000
onStopped: {
an1.start()
}
}
}
}
QWidget实现:
UI上拉取QuickWidget控件
MainWindow.cpp
QUrl source("qrc:/mainText.qml");
ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView );
ui->quickWidget->setSource(source);
ui->quickWidget->setClearColor(QColor(Qt::transparent));
ui->quickWidget->show();