Android:麻烦在循环中改变振动强度

时间:2015-09-09 19:22:22

标签: android pwm android-vibration

我试图根据使用PWM的模式改变手机的振动,以便改变它的速度。我能够让它发挥作用:

vibrator.vibrate(patternA, 0);

但当我把振动器调用放在这样的循环中时:

final int[] rampIntensity = {30, 40, 50, 60, 70, 80, 90, 100};
final int[] rampTime = {3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000};
for(int i=0; i <= rampIntensity.length-1; i++) {
    long endTime = System.currentTimeMillis() + rampTime[i]*60; //fetch starting time
    while((System.currentTimeMillis()) < endTime){
       vibrator.vibrate(patternFollower(rampIntensity, i), 0);
    }
}

振动最多是粗略的,并且最终会在通过预定模式时强度增加的地方失败。我觉得好像有一些关于让它离开这个循环运行的东西,但它并没有找到我。

所以我的问题是,如何让这个方法让Vibrator类使用PWM振动到一个模式。

编辑:这里是patternFollower和getVibratePattern方法。 Vibrator.vibrate似乎想要你作为决赛提供给它的模式。

private long[] patternFollower(int[] intensity, int 
long[] pattern;
    pattern =  getVibratePattern(intensity[i]);

    return pattern;
}

private long[] getVibratePattern(int intensity) {
    long[] pattern = null;
    final long[] smooth90 = {2, 30, 2, 30}; // Smooth - 90%
    final long[] smooth80 = {3, 30, 3, 30}; // Smooth - 80%
    final long[] smooth70 = {5, 30, 5, 30}; // Smooth - 75%
    final long[] smooth60 = {5, 25, 5, 25}; // Smooth - 60%
    final long[] smooth50 = {7, 25, 7, 25}; // Smooth - 50%
    final long[] smooth40 = {3, 15, 3, 15}; // Smooth - 40%
    final long[] smooth30 = {7, 15, 7, 15}; // Smooth - 30%
    switch (intensity){
        case 90: return smooth90;
        case 80: return smooth80;
        case 70: return smooth70;
        case 60: return smooth60;
        case 50: return smooth50;
        case 40: return smooth40;
        case 30: return smooth30;
    }
    return pattern;
}

0 个答案:

没有答案