目录

线性马达振动命令<VibrateCommand>

功能概述

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

系统预置模式

自定义振动模式

应用场景

XML规范

<Button x="" y="" w="" h="">
<Triggers>
<Trigger action="">
<VibrateCommand id ="" type="" define = "" />
</Trigger>
</Triggers>
</Button>

<Button x="" y="" w="" h="">
<Triggers>
<Trigger action="">
<VibrateCommand id ="" define = "true" >
<Vibrate loop="">
<Wave>
<Slice time=""    duration="" type="2" intensity="" />
<Slice time="" duration="" type="2" intensity="" />
<Slice time="" duration="" type="2" intensity="" />
…………
</Wave>
</Vibrate>
</VibrateCommand>
</Trigger>
</Triggers>
</Button>

参数说明

参数

类型

选项

注释

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>为<VibrateCommand>的子标签,表示使用自定义振动模式。

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

参数

类型

选项

注释

loop

字符串

必填

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

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

自定义振动波形标签<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类型。

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

应用示例

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

<Image x="600" y="500" h="250" w = "260" src="bj.jpg"  />
<Button x="600" y="500" h="250" w="260">
	<Trigger action="down">         
		<RefreshWeatherCommand />
	</Trigger>
</Button>

<Image x="20" y="640" w="200" h="300" src="v1.png" />
<Button x="20" y="640" w="200" h="300">
        <Triggers>
	        <Trigger action="up">
                         <VibrateCommand type="haptic.common.success" define = "false" />
	        </Trigger>
        </Triggers>
</Button>

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

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

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

 <Image x="20" y="640" w="200" h="300" src="v1.png" />
  <Button x="20" y="640" w="200" h="300">
  <Triggers>
	<Trigger action="up">
	<VibrateCommand  define = "true" >
	<Vibrate loop="false">
	    <Wave>
	        <Slice time="0"    duration="800" type="2" intensity="0.2" />
	        <Slice time="1000" duration="500" type="2" intensity="0.5" />
	        <Slice time="1800" duration="800" type="2" intensity="0.8" />
	    </Wave>
	</Vibrate>
	</VibrateCommand>
	</Trigger>
  </Triggers>
  </Button>

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

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

  
  <!-- 是否亮屏振动 当解析到相关振动command中如果有 defPlayVibrateId的值和 VibrateCommand中 id相同时则亮屏会振动-->
  <Var name="defPlayVibrateId" expression="'b'" type="string" persist="true"/>

  <Image x="107" y="748" w="176" w="217" src="v1.png" />
  <Image x="107" y="1012" w="176" w="217" src="v2.png"/>
  <Image x="107" y="1285" w="176" w="217" src="v3.png" />

  <Button x="107" y="748" w="176" h="217">
	<Triggers>
		<Trigger action="up">
			  <VariableCommand name="defPlayVibrateId" expression="'a'" type="string" />  <!-- 振动1 -->
			  <VibrateCommand id ="a"  define = "true" >
				<Vibrate loop="false">
					<Wave>
						<Slice time="0"    duration="800" type="2" intensity="0.2" />
						<Slice time="1000" duration="500" type="2" intensity="0.5" />
						<Slice time="1800" duration="800" type="2" intensity="0.8" />
					</Wave>
				</Vibrate>
			 </VibrateCommand>
		</Trigger>
	</Triggers>
  </Button>

  <Button x="107" y="1012" w="176" h="217">
	<Triggers>
		<Trigger action="up">
			<VariableCommand name="defPlayVibrateId" expression="'b'" type="string" />  <!-- 振动2 -->
			<VibrateCommand id="b"  define = "true" >
				<Vibrate loop="false">
					<Wave>
						<Slice time="0" duration="800" type="1" intensity="0.5" />
						<Slice time="1000" duration="1500" type="2" intensity="0.5" />
					</Wave>
				</Vibrate>
			 </VibrateCommand>
		</Trigger>
	</Triggers>
  </Button>

  <Button x="107" y="1285" w="176" h="217">
	<Triggers>
		<Trigger action="up">
			<VariableCommand name="defPlayVibrateId" expression="'c'" type="string" />  <!-- 振动3 -->
			<VibrateCommand id="c"  define = "true" >
				<Vibrate loop="false">
					<Wave>
						<Slice time="200" duration="1200" type="2" intensity="0.8"/>
					</Wave>
				</Vibrate>
			</VibrateCommand>
		</Trigger>
	</Triggers>
  </Button>

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

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

 <Image x="300" y="100" w="200" h="200" src="v1.png"/>
  <Image x="300" y="400" w="200" h="200" src="v2.png"/>
  <Image x="300" y="800" w="200" h="200" src="v3.png"/>
  <Slider name="vibrate" visibility="1"> 
	<StartPoint x="300" y="100" w="200" h="200"> 
		<PressedState> 
			<Image x="300" y="100" w="200" h="200" src="v1.png"/>	
			<Trigger> 
				<VibrateCommand define = "true" >
				<Vibrate loop="false">
					<Wave>
						<Slice time="0" duration="500" type="1" intensity="0.5" /> 
					</Wave>
				</Vibrate>
				</VibrateCommand>
			</Trigger> 
		</PressedState> 
	</StartPoint>  

	<EndPoint x="300" y="400" w="200" h="200"> 
		<ReachedState> 		
			<Trigger> 
				<VibrateCommand define = "true" >
				<Vibrate loop="false">
					<Wave>
						<Slice time="0"    duration="200" type="1" intensity="0.5"/> 
						<Slice time="500"    duration="300" type="1" intensity="0.5"/> 
					</Wave>
				</Vibrate>
				</VibrateCommand>
			</Trigger> 
		</ReachedState>  
	</EndPoint> 

	<EndPoint x="300" y="800" w="200" h="200"> 
		<ReachedState> 
			<Trigger> 
				<VibrateCommand define = "true" >
				<Vibrate loop="false">
				<Wave>
					<Slice time="0"    duration="600" type="1" intensity="0.5"/> 
				</Wave>
				</Vibrate>
				</VibrateCommand>
			</Trigger> 
		</ReachedState>  
	</EndPoint> 
  </Slider>

效果和脚本展示

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

<?xml version="1.0" encoding="utf-8"?>
<Lockscreen version="1" frameRate="60"  displayDesktop="true" screenWidth="1080"  id="201706169895">
	<Var name="w" expression="#screen_width" persist="true" const="true" />
	<Var name="h" expression="#screen_height" persist="true" const="true" />
	<Var name="qh" expression="(#screen_height-2400)/2" persist="true" const="true" />
	<Var name="pos" expression="ifelse(gt(#screen_height,1920),300,200)" />

	<!--初始化-->
	<ExternalCommands>
		<Trigger action="resume">
			<VariableCommand name="index" expression="0"/>
			<VariableCommand name="first" expression="0"/>
			<VariableCommand name="video" expression="0"/>
			<VariableCommand name="group" expression="0" />
			<VariableCommand name="group" expression="1" delay="2000"/>
		</Trigger>
		<Trigger action="pause">
		</Trigger>
	</ExternalCommands>

	<Rectangle x="0" y="0" w="#w" h="#h" fillColor="#000000"/>
	<!--背景-->
	<Image name="bg" x="0" y="0" src="bg.jpg" />
	<Video name="js" src="video1.mp4" defaultBitmap="bg.jpg" play="true" sound="0" looping="false" scaleType="fit_width" visibility="eq(#group,0)" />

	<Image  x="0" y="0"  src="bg/houbanduan.jpg" srcid="int(#index)" visibility="eq(#group,1)"/>

	<Var name="timeEvent" expression="#time" threshold="1">
		<Trigger>
			<VariableCommand name="index"   expression="ifelse(gt(#index,84),37,#index+#ratio)" condition="eq(#video,1)" />
			<VariableCommand name="index"   expression="ifelse(gt(#index,0),#index-1,0)" condition="eq(#video,2)" />
			<VariableCommand name="video"   expression="0" condition="eq(#index,0)" />
		</Trigger>
	</Var>

	<Image  x="939"  y="#h-175"  w="80"  h="130" src="taban.png" />
	<Image  x="60"  y="#h-175"  w="80"  h="130" src="music.png" srcid="#music"/>

	<Button x="739" y="#h-400" w="400" h="400" visibility="eq(#group,1)*eq(#video,0)">
		<Triggers>
			<Trigger action="down">
				<VariableCommand name="ratio" expression="1.5" condition="eq(#first,0)"/>
				<VariableCommand name="ratio" expression="0.5" delay="2000"/>

				<VariableCommand name="first" expression="1" condition="eq(#first,0)"/>

				<VariableCommand name="video" expression="1"/>
				<SoundCommand sound="m2.mp3" volume="1" loop="false"   keepCur="false"  condition="eq(#music,0)"/>
				<SoundCommand sound="m3.mp3" volume="1" loop="true"    keepCur="true"  condition="eq(#music,0)" delay="20"/>
				<!--				<SoundCommand sound="m3.mp3" volume="1" loop="true"   keepCur="false" delay="2000"  delayCondition="eq(#video,1)"/>-->

				<VibrateCommand id ="a" type="haptic.volume.trigger" define = "false" >
				</VibrateCommand>
			</Trigger>

			<Trigger action="up">
				<SoundCommand sound="1.mp3" volume="0" loop="false"  keepCur="false" />
				<VibrateCommand id ="a" define="true">
					<Vibrate loop="false">
						<Wave>
							<Slice time="0"  duration="10" type="1" intensity="0" />
						</Wave>
					</Vibrate>
				</VibrateCommand>

				<VariableCommand name="video" expression="2" />
			</Trigger>
		</Triggers>
	</Button>


	<Button  x="0"  y="#h-400" w="400" h="400" >
		<Trigger action="down">
			<VariableCommand name="music" expression="1-#music"/>
		</Trigger>
	</Button>

	<!--时间日期-->
	<Group x="#w/2-112" y="#pos">
		<Image x="0" y="0"   src="time/number.png" srcid="#hour/10"/>
		<Image x="112" y="0" src="time/number.png" srcid="#hour%10"/>

		<AlphaAnimation>
			<Alpha a="0"   time="0"/>
			<Alpha a="255" time="300"/>
			<Alpha a="255" time="10000000"/>
		</AlphaAnimation>

		<PositionAnimation repeat="1">
			<Position x="0"   y="300"   time="0" varSpeedFlag="SineFun_Out"/>
			<Position x="0"   y="0" time="300" />
		</PositionAnimation>

	</Group>

	<Group x="#w/2-112" y="#pos">
		<Image x="0" y="182"   src="time/number.png" srcid="#minute/10"/>
		<Image x="112" y="182" src="time/number.png" srcid="#minute%10"/>

		<AlphaAnimation delay="200">
			<Alpha a="0"   time="0"/>
			<Alpha a="255" time="300"/>
			<Alpha a="255" time="10000000"/>
		</AlphaAnimation>

		<PositionAnimation delay="200"  repeat="1">
			<Position x="0"   y="300"   time="0" varSpeedFlag="SineFun_Out"/>
			<Position x="0"   y="0" time="300" />
		</PositionAnimation>
	</Group>

	<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"  >
		<Image name="m1"  x="0"    y="0" src="time/date.png" srcid="(#month+1)/10" />
		<Image name="m2"  x="#m1.bmp_width" y="0" src="time/date.png" srcid="(#month+1)%10"/>
		<Image name="ms"  x="#m1.bmp_width+#m2.bmp_width" y="0" src="time/date_dot.png"/>
		<Image name="d1"  x="#m1.bmp_width+#m2.bmp_width+#ms.bmp_width" y="0" src="time/date.png" srcid="#date/10" />
		<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"/>
		<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" />
		<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"/>

		<AlphaAnimation delay="400">
			<Alpha a="0"   time="0"/>
			<Alpha a="255" time="300"/>
			<Alpha a="255" time="10000000"/>
		</AlphaAnimation>

		<PositionAnimation delay="400"  repeat="1">
			<Position x="0"   y="300"   time="0" varSpeedFlag="SineFun_Out"/>
			<Position x="0"   y="0" time="300" />
		</PositionAnimation>
	</Group>

	<!--上滑解锁-->
	<Button x="0" y="0" w="1080" h="#screen_height">
		<Triggers>
			<Trigger action="down">
			</Trigger>
			<Trigger action="up">
				<ExternCommand command="unlock" condition="lt(#touch_y-#touch_begin_y,-300)" />
			</Trigger>
		</Triggers>
	</Button>
</Lockscreen>