文档管理中心
内容分发主题中心开发教程主题引擎HarmonyOS 4.X及以下版本主题引擎规范基础功能命令线性马达振动命令<VibrateCommand>

线性马达振动命令<VibrateCommand>

功能概述

震动设置<Vibrate>相比,线性马达振动命令<VibrateCommand>支持触发振动的组件更多,支持实现的振动效果更丰富,搭配酷炫动效或音乐,给用户带来无限“振”撼。

  • 线性马达振动命令,有标签和多个子标签,集成在<Trigger>标签下。
  • 支持触发线性马达振动命令的组件无限制,常用场景为点击按钮/滑动/亮屏触发线性马达振动。
  • 支持系统预自定义振动两种模式,系统预置模式下可切换震动的类型;自定义振动模式下可自定义设置震动的时长、强弱和间隔时间。
  • 线性马达振动命令有机型限制。

系统预置模式

  • 系统预置41种振动类型,可直接调用。每种振动类型的波形时长、强度和间隔时间是固定的,不可修改。
  • 仅Mate30、Mate40、P40、P50系列手机支持。

自定义振动模式

  • 可自定义设置振动波形的时长、强度和间隔时间,非常灵活。
  • 仅P50系列手机支持。

应用场景

  • 持续按压汽车油门,伴随振动和酷炫音效,带来汽车启动的无限振感。
  • 点击杯子破碎、按压气球爆炸触发不同振感。
  • 在屏幕上滑动不倒翁,根据不同的方位滑动触发不同的线性马达振动类型,带来不同的振感。

XML规范

  • 系统预置模式
收起
自动换行
深色代码主题
复制
  1. <Button x="" y="" w="" h="">
  2. <Triggers>
  3. <Trigger action="">
  4. <VibrateCommand id ="" type="" define = "" />
  5. </Trigger>
  6. </Triggers>
  7. </Button>

  • 自定义振动模式
收起
自动换行
深色代码主题
复制
  1. <Button x="" y="" w="" h="">
  2. <Triggers>
  3. <Trigger action="">
  4. <VibrateCommand id ="" define = "true" >
  5. <Vibrate loop="">
  6. <Wave>
  7. <Slice time="" duration="" type="2" intensity="" />
  8. <Slice time="" duration="" type="2" intensity="" />
  9. <Slice time="" duration="" type="2" intensity="" />
  10. …………
  11. </Wave>
  12. </Vibrate>
  13. </VibrateCommand>
  14. </Trigger>
  15. </Triggers>
  16. </Button>

参数说明

  • <VibrateCommand>
展开

参数

类型

选项

注释

id

字符串

选填

点击屏幕/滑动触发振动时,可不填。

亮屏触发振动时,该id为必填。亮屏时变量defPlayVibrateId值与id值对应,值相等则触发对应的振动(参考示例3)。

define

字符串

必填

define为是否启用自定义模式,false为执行系统预置,true为自定义模式,默认为false。

当define="true"时,自定义振动标签<Vibrate>及其子标签 <Wave>、<Slice> 为必填。

type

字符串

选填

当define="true"时,可不填。

当define="false"时,为必填,表示启用系统预置模式下的振动类型,其为固定的枚举值:

haptic.grade.strength1,

haptic.grade.strength2,

haptic.grade.strength3,

haptic.grade.strength4,

haptic.grade.strength5,

haptic.camera.focus,

haptic.camera.gear_slip,

haptic.camera.portrait_switch,

haptic.camera.mode_switch,

haptic.camera.long_press,

haptic.common.button,

haptic.common.charging,

haptic.common.delete_long_press,

haptic.common.fail_pattern1,

haptic.common.fail_pattern2,

haptic.common.long_press,

haptic.common.long_press1,

haptic.common.long_press2,

haptic.common.notice,

haptic.common.pinch,

haptic.common.success,

haptic.common.switch,

haptic.common.threshold,

haptic.common.upglide,

haptic.mode.change,

haptic.poweroff,

haptic.slide.type1,

haptic.slide.type2,

haptic.slide.type3,

haptic.slide.type4,

haptic.slide.type5,

haptic.slide.type6,

haptic.common.long_press3,

haptic.common.click_up,

haptic.common.tick,

haptic.common.double_click,

haptic.common.click,

haptic.volume.max,

haptic.volume.min,

haptic.notice.Arrow,

haptic.clock.stopwatch。

  • 自定义振动标签<Vibrate>

自定义振动标签<Vibrate>为<VibrateCommand>的子标签,表示使用自定义振动模式。

当<VibrateCommand>中的define参数赋值为true时,必须使用<Vibrate>标签。

展开

参数

类型

选项

注释

loop

字符串

必填

loop属性表示是否循环播放,true循环,false表示非循环。

  • 自定义振动波形标签 <Wave>

自定义振动波形标签<Wave>为<Vibrate>的子标签,表示自定义振动模式下的振动波形。

  • 自定义振动波形标签<Slice>

自定义振动波形标签<Slice>为<Wave>的子标签,表示每个振动波形。

一个<Wave>中可以有多个<Slice>,<Slice>的最大数目为64。

展开

参数

类型

选项

注释

time

数值

必填

不支持表达式,表示振动波形的时间间隔,类型为int类型,取值范围[0,1800000],单位为ms。

考虑到用户友好性,建议取值在1000以内。当前slice的time要大于等于上个slice的time+duration的值且time的值必须唯一。

duration

数值

必填

不支持表达式,表示振动波形的时长,单位为ms,取值范围[0,1800000]。

考虑到马达振动的持续时间以及自身器件特性,建议取值为5秒。

intensity

数值

必填

不支持表达式,表示振动波形强度,属性值为浮点类型,取值范围[0,1]。0代表振动强度为零,即不振动,数值越大则振动强度越大。

type

数值

必填

不支持表达式,表示该波段振动波类型。只能填1(transient)和2(continuous),当前仅支持2类型。

  • 控制亮屏自动执行振动的变量名 defPlayVibrateId

manifest.xml中如果有声明var且name 为 defPlayVibrateId的变量,则每次亮屏会检测是否有VibrateCommand振动id与变量defPlayVibrateId值相等,如有相等则会自动执行对应的振动命令。参照示例3。

应用示例

示例1:点击按钮,得到系统预置的振动反馈

收起
自动换行
深色代码主题
复制
  1. <Image x="600" y="500" h="250" w = "260" src="bj.jpg" />
  2. <Button x="600" y="500" h="250" w="260">
  3. <Trigger action="down">
  4. <RefreshWeatherCommand />
  5. </Trigger>
  6. </Button>
  7. <Image x="20" y="640" w="200" h="300" src="v1.png" />
  8. <Button x="20" y="640" w="200" h="300">
  9. <Triggers>
  10. <Trigger action="up">
  11. <VibrateCommand type="haptic.common.success" define = "false" />
  12. </Trigger>
  13. </Triggers>
  14. </Button>

示例2:点击按钮,得到自定义模式的振动反馈

该自定义振动为非循环振动,分为三段振动波:第一段振动开始时间为0,振动时长800ms,振动强度为0.2;第二段振动开始时间为1000,振动时长500ms,振动强度为0.5;第三段振动开始时间为1800,振动时长800ms,振动强度为0.8。

注意

三段振动的振动时间区间不能有交集。

收起
自动换行
深色代码主题
复制
  1. <Image x="20" y="640" w="200" h="300" src="v1.png" />
  2. <Button x="20" y="640" w="200" h="300">
  3. <Triggers>
  4. <Trigger action="up">
  5. <VibrateCommand define = "true" >
  6. <Vibrate loop="false">
  7. <Wave>
  8. <Slice time="0" duration="800" type="2" intensity="0.2" />
  9. <Slice time="1000" duration="500" type="2" intensity="0.5" />
  10. <Slice time="1800" duration="800" type="2" intensity="0.8" />
  11. </Wave>
  12. </Vibrate>
  13. </VibrateCommand>
  14. </Trigger>
  15. </Triggers>
  16. </Button>

示例3:每次亮屏有设置的默认振动效果,且点击不同按钮得到与之对应的自定义模式振动反馈,点击不同按钮后再次亮屏将振动最后一次点击的振动模式

亮屏默认执行振动2及VibrateCommand的id为b的振动。点击Image src 为 v1、v2、v3按钮分别执行三种不同的自定义振动模式。点击每个按钮都会对变量defPlayVibrateId重新赋值,则下次亮屏会执行最后一次点击的振动模式。

收起
自动换行
深色代码主题
复制
  1. <!-- 是否亮屏振动 当解析到相关振动command中如果有 defPlayVibrateId的值和 VibrateCommand中 id相同时则亮屏会振动-->
  2. <Var name="defPlayVibrateId" expression="'b'" type="string" persist="true"/>
  3. <Image x="107" y="748" w="176" w="217" src="v1.png" />
  4. <Image x="107" y="1012" w="176" w="217" src="v2.png"/>
  5. <Image x="107" y="1285" w="176" w="217" src="v3.png" />
  6. <Button x="107" y="748" w="176" h="217">
  7. <Triggers>
  8. <Trigger action="up">
  9. <VariableCommand name="defPlayVibrateId" expression="'a'" type="string" /> <!-- 振动1 -->
  10. <VibrateCommand id ="a" define = "true" >
  11. <Vibrate loop="false">
  12. <Wave>
  13. <Slice time="0" duration="800" type="2" intensity="0.2" />
  14. <Slice time="1000" duration="500" type="2" intensity="0.5" />
  15. <Slice time="1800" duration="800" type="2" intensity="0.8" />
  16. </Wave>
  17. </Vibrate>
  18. </VibrateCommand>
  19. </Trigger>
  20. </Triggers>
  21. </Button>
  22. <Button x="107" y="1012" w="176" h="217">
  23. <Triggers>
  24. <Trigger action="up">
  25. <VariableCommand name="defPlayVibrateId" expression="'b'" type="string" /> <!-- 振动2 -->
  26. <VibrateCommand id="b" define = "true" >
  27. <Vibrate loop="false">
  28. <Wave>
  29. <Slice time="0" duration="800" type="1" intensity="0.5" />
  30. <Slice time="1000" duration="1500" type="2" intensity="0.5" />
  31. </Wave>
  32. </Vibrate>
  33. </VibrateCommand>
  34. </Trigger>
  35. </Triggers>
  36. </Button>
  37. <Button x="107" y="1285" w="176" h="217">
  38. <Triggers>
  39. <Trigger action="up">
  40. <VariableCommand name="defPlayVibrateId" expression="'c'" type="string" /> <!-- 振动3 -->
  41. <VibrateCommand id="c" define = "true" >
  42. <Vibrate loop="false">
  43. <Wave>
  44. <Slice time="200" duration="1200" type="2" intensity="0.8"/>
  45. </Wave>
  46. </Vibrate>
  47. </VibrateCommand>
  48. </Trigger>
  49. </Triggers>
  50. </Button>

示例4:结合Slider滑块触发振动

可实现滑动过程种改变振动效果的体验,注意每段的振动时长设置的控制,如果上一个触发的振动未执行完,则停止上个震动触发当前位置的振动。控制好滑动节奏,则本例可以实现按下v1位置及滑动到v2、v3位值都会有不同振动效果。

收起
自动换行
深色代码主题
复制
  1. <Image x="300" y="100" w="200" h="200" src="v1.png"/>
  2. <Image x="300" y="400" w="200" h="200" src="v2.png"/>
  3. <Image x="300" y="800" w="200" h="200" src="v3.png"/>
  4. <Slider name="vibrate" visibility="1">
  5. <StartPoint x="300" y="100" w="200" h="200">
  6. <PressedState>
  7. <Image x="300" y="100" w="200" h="200" src="v1.png"/>
  8. <Trigger>
  9. <VibrateCommand define = "true" >
  10. <Vibrate loop="false">
  11. <Wave>
  12. <Slice time="0" duration="500" type="1" intensity="0.5" />
  13. </Wave>
  14. </Vibrate>
  15. </VibrateCommand>
  16. </Trigger>
  17. </PressedState>
  18. </StartPoint>
  19. <EndPoint x="300" y="400" w="200" h="200">
  20. <ReachedState>
  21. <Trigger>
  22. <VibrateCommand define = "true" >
  23. <Vibrate loop="false">
  24. <Wave>
  25. <Slice time="0" duration="200" type="1" intensity="0.5"/>
  26. <Slice time="500" duration="300" type="1" intensity="0.5"/>
  27. </Wave>
  28. </Vibrate>
  29. </VibrateCommand>
  30. </Trigger>
  31. </ReachedState>
  32. </EndPoint>
  33. <EndPoint x="300" y="800" w="200" h="200">
  34. <ReachedState>
  35. <Trigger>
  36. <VibrateCommand define = "true" >
  37. <Vibrate loop="false">
  38. <Wave>
  39. <Slice time="0" duration="600" type="1" intensity="0.5"/>
  40. </Wave>
  41. </Vibrate>
  42. </VibrateCommand>
  43. </Trigger>
  44. </ReachedState>
  45. </EndPoint>
  46. </Slider>

效果和脚本展示

长按右下角油门,启动跑车,持续按压油门,伴随振动和酷炫音效,给用户带来无限震撼(振动效果需在手机上感受)。

收起
自动换行
深色代码主题
复制
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Lockscreen version="1" frameRate="60" displayDesktop="true" screenWidth="1080" id="201706169895">
  3. <Var name="w" expression="#screen_width" persist="true" const="true" />
  4. <Var name="h" expression="#screen_height" persist="true" const="true" />
  5. <Var name="qh" expression="(#screen_height-2400)/2" persist="true" const="true" />
  6. <Var name="pos" expression="ifelse(gt(#screen_height,1920),300,200)" />
  7. <!--初始化-->
  8. <ExternalCommands>
  9. <Trigger action="resume">
  10. <VariableCommand name="index" expression="0"/>
  11. <VariableCommand name="first" expression="0"/>
  12. <VariableCommand name="video" expression="0"/>
  13. <VariableCommand name="group" expression="0" />
  14. <VariableCommand name="group" expression="1" delay="2000"/>
  15. </Trigger>
  16. <Trigger action="pause">
  17. </Trigger>
  18. </ExternalCommands>
  19. <Rectangle x="0" y="0" w="#w" h="#h" fillColor="#000000"/>
  20. <!--背景-->
  21. <Image name="bg" x="0" y="0" src="bg.jpg" />
  22. <Video name="js" src="video1.mp4" defaultBitmap="bg.jpg" play="true" sound="0" looping="false" scaleType="fit_width" visibility="eq(#group,0)" />
  23. <Image x="0" y="0" src="bg/houbanduan.jpg" srcid="int(#index)" visibility="eq(#group,1)"/>
  24. <Var name="timeEvent" expression="#time" threshold="1">
  25. <Trigger>
  26. <VariableCommand name="index" expression="ifelse(gt(#index,84),37,#index+#ratio)" condition="eq(#video,1)" />
  27. <VariableCommand name="index" expression="ifelse(gt(#index,0),#index-1,0)" condition="eq(#video,2)" />
  28. <VariableCommand name="video" expression="0" condition="eq(#index,0)" />
  29. </Trigger>
  30. </Var>
  31. <Image x="939" y="#h-175" w="80" h="130" src="taban.png" />
  32. <Image x="60" y="#h-175" w="80" h="130" src="music.png" srcid="#music"/>
  33. <Button x="739" y="#h-400" w="400" h="400" visibility="eq(#group,1)*eq(#video,0)">
  34. <Triggers>
  35. <Trigger action="down">
  36. <VariableCommand name="ratio" expression="1.5" condition="eq(#first,0)"/>
  37. <VariableCommand name="ratio" expression="0.5" delay="2000"/>
  38. <VariableCommand name="first" expression="1" condition="eq(#first,0)"/>
  39. <VariableCommand name="video" expression="1"/>
  40. <SoundCommand sound="m2.mp3" volume="1" loop="false" keepCur="false" condition="eq(#music,0)"/>
  41. <SoundCommand sound="m3.mp3" volume="1" loop="true" keepCur="true" condition="eq(#music,0)" delay="20"/>
  42. <!-- <SoundCommand sound="m3.mp3" volume="1" loop="true" keepCur="false" delay="2000" delayCondition="eq(#video,1)"/>-->
  43. <VibrateCommand id ="a" type="haptic.volume.trigger" define = "false" >
  44. </VibrateCommand>
  45. </Trigger>
  46. <Trigger action="up">
  47. <SoundCommand sound="1.mp3" volume="0" loop="false" keepCur="false" />
  48. <VibrateCommand id ="a" define="true">
  49. <Vibrate loop="false">
  50. <Wave>
  51. <Slice time="0" duration="10" type="1" intensity="0" />
  52. </Wave>
  53. </Vibrate>
  54. </VibrateCommand>
  55. <VariableCommand name="video" expression="2" />
  56. </Trigger>
  57. </Triggers>
  58. </Button>
  59. <Button x="0" y="#h-400" w="400" h="400" >
  60. <Trigger action="down">
  61. <VariableCommand name="music" expression="1-#music"/>
  62. </Trigger>
  63. </Button>
  64. <!--时间日期-->
  65. <Group x="#w/2-112" y="#pos">
  66. <Image x="0" y="0" src="time/number.png" srcid="#hour/10"/>
  67. <Image x="112" y="0" src="time/number.png" srcid="#hour%10"/>
  68. <AlphaAnimation>
  69. <Alpha a="0" time="0"/>
  70. <Alpha a="255" time="300"/>
  71. <Alpha a="255" time="10000000"/>
  72. </AlphaAnimation>
  73. <PositionAnimation repeat="1">
  74. <Position x="0" y="300" time="0" varSpeedFlag="SineFun_Out"/>
  75. <Position x="0" y="0" time="300" />
  76. </PositionAnimation>
  77. </Group>
  78. <Group x="#w/2-112" y="#pos">
  79. <Image x="0" y="182" src="time/number.png" srcid="#minute/10"/>
  80. <Image x="112" y="182" src="time/number.png" srcid="#minute%10"/>
  81. <AlphaAnimation delay="200">
  82. <Alpha a="0" time="0"/>
  83. <Alpha a="255" time="300"/>
  84. <Alpha a="255" time="10000000"/>
  85. </AlphaAnimation>
  86. <PositionAnimation delay="200" repeat="1">
  87. <Position x="0" y="300" time="0" varSpeedFlag="SineFun_Out"/>
  88. <Position x="0" y="0" time="300" />
  89. </PositionAnimation>
  90. </Group>
  91. <Group x="#w/2-(#m1.bmp_width+#m2.bmp_width+#ms.bmp_width+#d1.bmp_width+#d2.bmp_width+#w1.bmp_width+#wk.bmp_width)/2" y="#pos+182*2" >
  92. <Image name="m1" x="0" y="0" src="time/date.png" srcid="(#month+1)/10" />
  93. <Image name="m2" x="#m1.bmp_width" y="0" src="time/date.png" srcid="(#month+1)%10"/>
  94. <Image name="ms" x="#m1.bmp_width+#m2.bmp_width" y="0" src="time/date_dot.png"/>
  95. <Image name="d1" x="#m1.bmp_width+#m2.bmp_width+#ms.bmp_width" y="0" src="time/date.png" srcid="#date/10" />
  96. <Image name="d2" x="#m1.bmp_width+#m2.bmp_width+#ms.bmp_width+#d1.bmp_width" y="0" src="time/date.png" srcid="#date%10"/>
  97. <Image name="w1" x="#m1.bmp_width+#m2.bmp_width+#ms.bmp_width+#d1.bmp_width+#d2.bmp_width" y="0" src="time/week0.png" />
  98. <Image name="wk" x="#m1.bmp_width+#m2.bmp_width+#ms.bmp_width+#d1.bmp_width+#d2.bmp_width+#w1.bmp_width" y="0" src="time/week.png" srcid="#day_of_week"/>
  99. <AlphaAnimation delay="400">
  100. <Alpha a="0" time="0"/>
  101. <Alpha a="255" time="300"/>
  102. <Alpha a="255" time="10000000"/>
  103. </AlphaAnimation>
  104. <PositionAnimation delay="400" repeat="1">
  105. <Position x="0" y="300" time="0" varSpeedFlag="SineFun_Out"/>
  106. <Position x="0" y="0" time="300" />
  107. </PositionAnimation>
  108. </Group>
  109. <!--上滑解锁-->
  110. <Button x="0" y="0" w="1080" h="#screen_height">
  111. <Triggers>
  112. <Trigger action="down">
  113. </Trigger>
  114. <Trigger action="up">
  115. <ExternCommand command="unlock" condition="lt(#touch_y-#touch_begin_y,-300)" />
  116. </Trigger>
  117. </Triggers>
  118. </Button>
  119. </Lockscreen>
在 内容分发 中进行搜索
请输入您想要搜索的关键词