前言:
本日志是记录在开发我们的基于安卓平台的文字冒险游戏《UP住模拟器》过程中遇到的一些技术上的问题。在游戏开发过程中,我们需要设计一个“”闹钟app:让玩家实现控制游戏角色的作息和查看数据统计等功能。其中需要对设置时间的滚轮震动体验进行调试,以达到准APP级别的是用体验,给用户身临其境的游戏体验。为此,我特地查阅了华为官方对于震动马达的介绍文档,以进行游戏实机体验的优化。
项目需要的环境/依赖:
build.gradle(项目的)
buildscript {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}
build.gradle(app):
implementation 'com.huawei.devices:hapticskit:1.0.0.400'
这里的HapticsKitConstant.CameraTypeEnum.MODE_SWITCH.getType()种类有很多,下列代码中第二个参数都是种类,可以选择自己喜欢的震动模式:
addData("Camera_enum.CLICK", HapticsKitConstant.CameraTypeEnum.CLICK.getType());
addData("Camera_enum.CLICK_UP", HapticsKitConstant.CameraTypeEnum.CLICK_UP.getType());
addData("Camera_enum.FOCUS", HapticsKitConstant.CameraTypeEnum.FOCUS.getType());
addData("Camera_enum.GEAR_SLIP", HapticsKitConstant.CameraTypeEnum.GEAR_SLIP.getType());
addData("Camera_enum.LONG_PRESS", HapticsKitConstant.CameraTypeEnum.LONG_PRESS.getType());
addData("Camera_enum.MODE_SWITCH", HapticsKitConstant.CameraTypeEnum.MODE_SWITCH.getType());
addData("Camera_enum.PORTAIT_SWITH", HapticsKitConstant.CameraTypeEnum.PORTAIT_SWITH.getType());
addData("Battery.CHARGING", HapticsKitConstant.BatteryTypeEnum.CHARGING.getType());
addData("Calculator.DELETE", HapticsKitConstant.CalculatorTypeEnum.DELETE_LONG_PRESS.getType());
addData("Calculator.VITUAL_TASK", HapticsKitConstant.CalculatorTypeEnum.VITUAL_TASK.getType());
addData("Clock.STOPWATCH", HapticsKitConstant.ClockTypeEnum.STOPWATCH.getType());
addData("Clock.Timer", HapticsKitConstant.ClockTypeEnum.TIMER.getType());
addData("Contact.DELTE", HapticsKitConstant.DiallerTypeEnum.DELTE.getType());
addData("Contact.LETTERS_INDEX", HapticsKitConstant.DiallerTypeEnum.LETTERS_INDEX.getType());
addData("Contact.LONGPRESS", HapticsKitConstant.DiallerTypeEnum.LONGPRESS.getType());
addData("Contact.CLICK", HapticsKitConstant.DiallerTypeEnum.CLICK.getType());
addData("Control.SEARCH_LONG_PRESS", HapticsKitConstant.ControlTypeEnum.SEARCH_LONG_PRESS.getType());
addData("Control.TEXT_CHOSE", HapticsKitConstant.ControlTypeEnum.TEXT_CHOSE_CURSOR_MOVE.getType());
addData("Control.TEXT_EDIT", HapticsKitConstant.ControlTypeEnum.TEXT_EDIT.getType());
addData("Control.WIDGET", HapticsKitConstant.ControlTypeEnum.WIDGET_OPERATION.getType());
addData("Desktop.LONG_PRESS", HapticsKitConstant.DesktopTypeEnum.LONG_PRESS.getType());
addData("FingerPrint.INPUT", HapticsKitConstant.FingerPrintTypeEnum.INPUT_LONG_PRESS.getType());
addData("FingerPrint.UNLOCK_FAILE", HapticsKitConstant.FingerPrintTypeEnum.UNLOCK_FAILE.getType());
addData("Gallery.ALBUMS", HapticsKitConstant.GalleryTypeEnum.ALBUMS_LONG_PRESS.getType());
addData("Gallery.PHOTO", HapticsKitConstant.GalleryTypeEnum.PHOTOS_LONG_PRESS.getType());
addData("Gallery.UPGLIDES", HapticsKitConstant.GalleryTypeEnum.UPGLIDE_RELATED.getType());
addData("Home.CLICK_BACK.", HapticsKitConstant.NavigationTypeEnum.CLICK_BACK.getType());
addData("Home.CLICK_HOME.", HapticsKitConstant.NavigationTypeEnum.CLICK_HOME.getType());
addData("Home.CLICK_MULTITASK.", HapticsKitConstant.NavigationTypeEnum.CLICK_MULTITASK.getType());
addData("LockScreen.FACE_UNLOCK_FAIL.", HapticsKitConstant.LockScreenTypeEnum.FACE_UNLOCK_FAIL.getType());
addData("LockScreen.FACE_UNLOCK_RETRY.", HapticsKitConstant.LockScreenTypeEnum.FACE_UNLOCK_RETRY.getType());
addData("LockScreen.NUMBER_UNLOCK_FAIL.", HapticsKitConstant.LockScreenTypeEnum.NUMBER_UNLOCK_FAIL.getType());
addData("LockScreen.UNLOCK_CLICK.", HapticsKitConstant.LockScreenTypeEnum.UNLOCK_CLICK.getType());
addData("LockScreen.ONEHAND.", HapticsKitConstant.LockScreenTypeEnum.ONEHAND_KEYBOARD_SWITCH.getType());
addData("LockScreen.UNLOCK_SLIP.", HapticsKitConstant.LockScreenTypeEnum.UNLOCK_SLIP.getType());
addData("LockScreen.UPGLIDE_SWITCH.", HapticsKitConstant.LockScreenTypeEnum.UPGLIDE_SWITCH.getType());
addData("SystemUI.EXPAND.", HapticsKitConstant.SystemuiTypeEnum.EXPAND.getType());
addData("SystemUI.MOVE.", HapticsKitConstant.SystemuiTypeEnum.MOVE.getType());
addData("SystemUI.NO_LONG_PRESS.", HapticsKitConstant.SystemuiTypeEnum.NOTIFICATIONS_LONG_PRESS.getType());
addData("SystemUI.SCREEN_RECORD.", HapticsKitConstant.SystemuiTypeEnum.SCREEN_RECORD_STOP.getType());
addData("SystemUI.SWITCH_PRESS.", HapticsKitConstant.SystemuiTypeEnum.SWITCH_LONG_PRESS.getType());
addData("SystemUI.SWITCH_SORT_PRESS.", HapticsKitConstant.SystemuiTypeEnum.SWITCH_SORT_LONG_PRESS.getType());
addData("Wallet.TIME_SCROLL.", HapticsKitConstant.WalletTypeEnum.TIME_SCROLL.getType());
addData("Keyboard input strength 1", HapticsKitConstant.HW_HAPTIC_TYPE_GRADE_STRENGTH1);
addData("Keyboard input strength 2", HapticsKitConstant.HW_HAPTIC_TYPE_GRADE_STRENGTH2);
addData("Keyboard input strength 3", HapticsKitConstant.HW_HAPTIC_TYPE_GRADE_STRENGTH3);
addData("Keyboard input strength 4", HapticsKitConstant.HW_HAPTIC_TYPE_GRADE_STRENGTH4);
addData("Keyboard input strength 5", HapticsKitConstant.HW_HAPTIC_TYPE_GRADE_STRENGTH5);
华为官方对于Haptics Engine的介绍:
Haptics Engine –线性马达能力
华为官方Github实例:
https://github.com/huaweicodelabs/HapticsEngine
对于其他的安卓手机,可以直接调用vibrator类来实现马达的震动,但是马达的震动效果完全取决于手机本身的马达性能以及手机厂商对于手机的底层优化策略。以下是一个利用vibrator类实现手机马达调用的示例:
震动需要获得权限的,在使用之前在AndroidManifest.xml文件中添加:
<uses-permission android:name="android.permission.VIBRATE"/>
示例的代码是使用点击按钮的时候可以实现马达的震动,在按钮的点击事件的函数中添加这样几行代码:
Vibrator vibrator = (Vibrator)this.getSystemService(this.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
这里的this代表的当前的Context,然后最后一行代码设置的是震动的时间,这里的单位是ms。
作者:王竞浛