如何在Ubuntu QML应用中震动(vibration)

来源:互联网 发布:unity3d 跳跃代码 编辑:程序博客网 时间:2024/05/15 04:17

对于有些QML应用来说,震动是非常重要的一个功能。特别是对一下游戏来说。那么我们怎么在QML应用中震动呢?


我们官方有一个API HapticsEffect,这个API的功能就是让我们的应用来震动的。使用这个API非常容易:


import QtQuick 2.0import Ubuntu.Components 1.1import QtFeedback 5.0/*!    \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: "vibration.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("vibration")        HapticsEffect {            id: rumbleEffect            attackIntensity: 0.0            attackTime: 250            intensity: 1.0            duration: 100            fadeTime: 250            fadeIntensity: 0.0        }        Column {            spacing: units.gu(1)            anchors {                margins: units.gu(2)                fill: parent            }            Button {                objectName: "button"                width: parent.width                text: i18n.tr("Vibrate me!")                onClicked: {                    rumbleEffect.start();  // plays a rumble effect                }            }        }    }}

这里我们import了我们需要的库:

import QtFeedback 5.0

然后,实例化我们的HapticsEffect:

  HapticsEffect {            id: rumbleEffect            attackIntensity: 0.0            attackTime: 250            intensity: 1.0            duration: 100            fadeTime: 250            fadeIntensity: 0.0        }

当我们按下我们的按钮时,我们及开始震动了。当然这个必须是在手机上才可以测试到。


整个项目的源码在:https://github.com/liu-xiao-guo/vibration

0 0
原创粉丝点击