java 控制安卓手机的振动强度?是否可以?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2888043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
controlling vibration intensity in android phones? is it possible?
提问by Mithraa
I am developing a game. In which, i want do set different vibration intensities for different events. I just want know if its really possible to control the vibration intensity and duration. Any advice or reference links, could be very helpful. Thanks in advance.
我正在开发一个游戏。其中,我想为不同的事件设置不同的振动强度。我只是想知道是否真的可以控制振动强度和持续时间。任何建议或参考链接,都可能非常有帮助。提前致谢。
回答by Josh
I think it depends on what you mean by intensity. You can control the pattern and length of the vibration, but I don't think you can make it vibrate "stronger".
我认为这取决于你所说的强度是什么意思。你可以控制振动的模式和长度,但我认为你不能让它振动“更强”。
http://developer.android.com/reference/android/os/Vibrator.html
http://developer.android.com/reference/android/os/Vibrator.html
回答by mateusz
I've made a simple trick to somehow reduce the intensity of vibration. My idea is to interleave vibration intervals with silent intervals. If you have one millisecond of vibration and then one second of silence and so on it seems like it's one constant vibration but weaker than normal. You can try to increase the silence intervals to make the vibration even weaker. Here goes the code example:
我做了一个简单的技巧来以某种方式降低振动强度。我的想法是将振动间隔与静音间隔交错。如果您有 1 毫秒的振动,然后是 1 秒的静音,依此类推,这似乎是一种持续的振动,但比正常情况要弱。您可以尝试增加静音间隔,使振动更弱。下面是代码示例:
int strong_vibration = 30; //vibrate with a full power for 30 secs
int interval = 1000;
int dot = 1; //one millisecond of vibration
int short_gap = 1; //one millisecond of break - could be more to weaken the vibration
long[] pattern = {
0, // Start immediately
strong_vibration,
interval,
// 15 vibrations and 15 gaps = 30millis
dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, //yeah I know it doesn't look good, but it's just an example. you can write some code to generate such pattern.
};
回答by paulscode
PWMcan be used to produce a vibration pattern of various pulse widths, resulting in lower average voltage to the vibrator motor (and thus weaker vibration output).
PWM可用于产生各种脉冲宽度的振动模式,从而导致振动器电机的平均电压较低(因此振动输出较弱)。
I've posted a simple proof of concept method here. This method will generate a pattern with the specified intensity and duration. The transition in that method isn't quite linear, so I have posted a bounty to hopefully get some alternate suggestions. Will update when I have an even better algorithm.
我已经发布的概念方法的一个简单证明这里。此方法将生成具有指定强度和持续时间的图案。该方法中的转换不是完全线性的,所以我发布了一个悬赏,希望能得到一些替代建议。当我有更好的算法时会更新。
回答by jayanthsaikiran
This could help you but It only works for API level 26 or above.
这可以帮助您,但它仅适用于 API 级别 26 或更高级别。
public void vibrate(View view) {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
long[] wave_time = {0, 100, 0, 100, 0, 100, 0, 100, 0, 100};
int[] wave_ampl = {0, 50, 0, 100, 0, 150, 0, 200, 0, 255};
VibrationEffect vibrationEffect = null;
vibrationEffect = VibrationEffect.createWaveform(wave_time, wave_ampl, -1);
vibrator.vibrate(vibrationEffect);
}
}
Here wave_timearray represents two types of times:
这里的wave_time数组代表两种类型的时间:
- Time in which it should be idle (0th index, 2nd index, .etc)
- Time In which it should vibrate (1st index, 3rd index, .etc)
- 应该空闲的时间(第 0 个索引、第 2 个索引等)
- 它应该振动的时间(第一个索引,第三个索引,等)
wave_amplarray represents the strength of the vibration w.r.t wave_timearray.
wave_amplarray 表示振动 wrtwave_time阵列的强度。
Explanation:
解释:
Phone waits for 0ms (0th index of wave_time) and starts vibrating for 100ms (1st index of wave_time) with intensity of 50(1st index of wave_ampl).
手机等待0毫秒(第 0 个索引wave_time)并开始以50(第一个索引)的强度振动100毫秒(第一个索引)。wave_timewave_ampl
Similarly for phone vibrates for 100ms (3rd index of wave_time) with intensity of 100(3rd index of wave_ampl).
类似地,手机振动100毫秒(第 3 个索引wave_time),强度为100(第 3 个索引wave_ampl)。
The maximum intensity is 255in android.
android 中的最大强度为255。

