我们在有些的时候,需要在触屏的时候感知到触觉。那么我们怎么在QML应用是实现这个功能呢?
在Ubuntu 15.04的Ubuntu.Component 1.2模块中,我们有如下的一个API:
https://developer.ubuntu.com/api/apps/qml/sdk-15.04/Ubuntu.Components.Haptics/
具体的描述可以参阅相应的API文档。这里我们只简单地把我们的测试程序列出来:
import QtQuick 2.0
import Ubuntu.Components 1.2
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "haptics.liu-xiao-guo"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
//useDeprecatedToolbar: false
width: units.gu(100)
height: units.gu(75)
Page {
title: i18n.tr("Haptics")
Column {
Item {
implicitWidth: units.gu(20)
implicitHeight: units.gu(5)
Label {
text: "Press me"
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
MouseArea {
anchors.fill: parent
onClicked: Haptics.play()
}
}
Item {
implicitWidth: units.gu(25)
implicitHeight: units.gu(5)
Label {
text: "Press me again"
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
MouseArea {
anchors.fill: parent
onClicked: Haptics.play({duration: 25, attackIntensity: 0.7})
}
}
}
}
}
这里我们使用了Haptics.play()来完成相应的功能。当我们触控“Press me”时,我们会感觉到屏幕的触动。当然这也依赖于我们在“系统设置”中的设置。我们也可以通过改变Haptics.play()其中的参数来完成不同时间长度及震动大小的触觉。
整个项目的源码在: git clone https://gitcafe.com/ubuntu/haptics.git
如何在qmake项目中在QML语言中调用C++代码:http://www.linuxdiyf.com/linux/12105.html