Android开发:调用华为手机X轴线性马达震动
因为使用默认的震动的反馈使用太差了,所以强烈建议除了闹钟的震动使用默认马达,如果有X轴或者Y轴线性马达的手机,尽量使用线性马达。
项目需要的环境/依赖:
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'
简单示例:
private static HapticsKit mHapkit; private static HapticsKitAdapter mHapticsAdapter; mHapkit = new HapticsKit(context); mHapticsAdapter = mHapkit.initialize(1); mHapticsAdapter.setParameter(HapticsKitConstant.CameraTypeEnum.MODE_SWITCH.getType());
这里的
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的介绍:https://developer.huawei.com/consumer/cn/codelab/HUAWEIHapticsKit/index.html#0
华为官方Github实例:https://github.com/huaweicodelabs/HapticsEngine