下图是我设计的示例效果:
图 1 渐变图形效果
如图所示,第一行为线性渐变,第二行为锥形渐变,第三行为径向渐变。
渐变元素与其他图形效果元素不同之处在于:渐变元素既可以用来改变一个已有的元素(如Image),也有可以独立使用。如你在示例效果图中看到的那样,每一行前两个是独立使用渐变元素的效果,后两个是讲渐变效果应用到其它元素上的效果。
渐变效果较为简单,所有的示例都放在一个 qml 文档—— GradientExample.qml ——中,内容如下:
import QtQuick 2.2 import QtGraphicalEffects 1.0 import QtQuick.Controls 1.2 Rectangle { id: example; anchors.fill: parent; signal back(); Button { anchors.right: parent.right; anchors.top: parent.top; anchors.margins: 4; text: "Back"; onClicked: example.back(); } Text { id: linearLabel; anchors.left: parent.left; anchors.top: parent.top; anchors.margins: 4; text: "LinearGradient"; font.bold: true; } Row { id: linears; anchors.left: parent.left; anchors.top: linearLabel.bottom; anchors.margins: 4; spacing: 8; LinearGradient { width: 100; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#80222222";} GradientStop{ position: 0.9; color: "#FFFFFFFF";} } start: Qt.point(0, 0); end: Qt.point(0, 180); } LinearGradient { width: 100; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#FFFF0000";} GradientStop{ position: 0.3; color: "#FFFFA000";} GradientStop{ position: 0.5; color: "#A0FF4000";} GradientStop{ position: 1.0; color: "#FF800FFF";} } start: Qt.point(0, 0); end: Qt.point(80, 180); } Image { id: butterfly; source: "butterfly.png"; width: 180; height: 180; smooth: true; sourceSize: Qt.size(180, 180); } LinearGradient { source: butterfly; width: 180; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#222222";} GradientStop{ position: 0.9; color: "#F8884F";} } start: Qt.point(0, 0); end: Qt.point(180, 180); } } Text { id: conicalLabel; anchors.left: parent.left; anchors.top: linears.bottom; anchors.margins: 4; text: "ConicalGradient"; font.bold: true; } Row { id: conicals; anchors.left: parent.left; anchors.top: conicalLabel.bottom; anchors.margins: 4; spacing: 8; ConicalGradient { width: 100; height: 180; angle: 45; gradient: Gradient { GradientStop{ position: 0.0; color: "#80222222";} GradientStop{ position: 0.9; color: "#FFFFFFFF";} } } ConicalGradient { width: 100; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#FFFF0000";} GradientStop{ position: 0.3; color: "#FFFFA000";} GradientStop{ position: 0.5; color: "#A0FF4000";} GradientStop{ position: 1.0; color: "#FF800FFF";} } horizontalOffset: 20; verticalOffset: 40; } Image { id: conicalOrig; source: "butterfly.png"; width: 180; height: 180; smooth: true; sourceSize: Qt.size(180, 180); } ConicalGradient { source: conicalOrig; width: 180; height: 180; angle: 60; gradient: Gradient { GradientStop{ position: 0.0; color: "#A22222";} GradientStop{ position: 0.9; color: "#2888F4";} } } } Text { id: radialLabel; anchors.left: parent.left; anchors.top: conicals.bottom; anchors.margins: 4; text: "RadialGradient"; font.bold: true; } Row { id: radials; anchors.left: parent.left; anchors.top: radialLabel.bottom; anchors.margins: 4; spacing: 8; RadialGradient { width: 100; height: 180; angle: 60; horizontalRadius: 50; verticalRadius: 90; gradient: Gradient { GradientStop{ position: 0.0; color: "#222222";} GradientStop{ position: 0.9; color: "#00FF0F";} } } RadialGradient { width: 100; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#FFFF0000";} GradientStop{ position: 0.3; color: "#FFFFA000";} GradientStop{ position: 0.5; color: "#A0FF4000";} GradientStop{ position: 1.0; color: "#FF800FFF";} } horizontalOffset: 20; horizontalRadius: 40; verticalRadius: 40; verticalOffset: 40; } Image { id: radialOrig; source: "butterfly.png"; width: 180; height: 180; smooth: true; sourceSize: Qt.size(180, 180); } RadialGradient { source: radialOrig; width: 180; height: 180; angle: 120; horizontalRadius: 40; verticalRadius: 80; gradient: Gradient { GradientStop{ position: 0.0; color: "#A22222";} GradientStop{ position: 1.0; color: "#2888F4";} } } } }
线性渐变元素(LinearGradient)有下列属性:
有了上面的介绍,再来看示例:
LinearGradient { width: 100; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#80222222";} GradientStop{ position: 0.9; color: "#FFFFFFFF";} } start: Qt.point(0, 0); end: Qt.point(0, 180) ; }
下面的代码则是为 source 属性指定了一个 Image 对象:
Image { id: butterfly; source: "butterfly.png"; width: 180; height: 180; smooth: true; sourceSize: Qt.size(180, 180); } LinearGradient { source: butterfly; width: 180; height: 180; gradient: Gradient { GradientStop{ position: 0.0; color: "#222222";} GradientStop{ position: 0.9; color: "#F8884F";} } start: Qt.point(0, 0); end: Qt.point(180, 180); }
锥形渐变(ConicalGradient)有下列属性:
RadialGradient 是渐变元素里最复杂的了,有下列属性:
好吧,结合前面的完整示例代码来看吧,不说了。
--------
回顾一下我的Qt Quick系列文章:
原文地址:http://blog.csdn.net/foruok/article/details/43225263