4.1的震子变成一个服务,调用方式有所变化:

private static final int VIBRATE_DURATION = 15;
private Vibrator mVibrator;
mVibrator = (Vibrator) launcher.getSystemService(Context.VIBRATOR_SERVICE);
mVibrator.vibrate(VIBRATE_DURATION);
//以前的用法如下:
private Vibrator mVibrator = new Vibrator();
mVibrator.vibrate(VIBRATE_DURATION);
//END

一般也可以利用view的HapticFeedback来实现触摸点击反馈:

v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
                        HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); 

该参数在设置的声音选项触摸时振动设置并存储为系统数据,view在刷新绘制时会获取该值然后进行振动回馈。

有时候需要定制一个view的振动强度这时候我们可以两者搭配使用:

public void performVibrate(int adjustment){	
		int val = new Settings.System().getInt(getContentResolver(),Settings.System.HAPTIC_FEEDBACK_
			ENABLED,0);	
       if (DEBUG_WIDGETS) {	
           Log.d(TAG,"-----performVibrate for onLongClick----" + val);
       }	
       if (val != 0){
           mVibrator.vibrate(VIBRATE_DURATION + adjustment);	
       }	
    }
定制化view的振动时需要关闭它自身的HapticFeedback,不然会出现振动时间和设值不一致的情况:

launcher.setupViews:mWorkspace.setHapticFeedbackEnabled(false);
AppsCustomizePagedView.syncAppsPageItems:icon.setHapticFeedbackEnabled(false);



相关文章:

  • 2021-08-23
  • 2021-06-28
  • 2021-12-24
  • 2021-06-20
  • 2021-06-08
  • 2021-08-02
  • 2021-07-20
  • 2021-06-22
猜你喜欢
  • 2021-10-30
  • 2022-01-05
  • 2022-12-23
  • 2021-09-09
  • 2021-05-24
  • 2022-12-23
  • 2021-05-05
相关资源
相似解决方案