我对如何使用测量 - MotionEvent的getPressure有疑问。我想测量用户触摸屏幕时施加的压力。任何人都可以给我一些样品。
还有一个问题:
getPressure:1.0
的返回由最强的媒体或设备定义。
答案 0 :(得分:1)
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);// Get an instance of the sensor service, and use that to get an instance of a particular sensor.
public final void onSensorChanged(SensorEvent event) {
float millibars_of_pressure = event.values[0];//use the pressure value
}
答案 1 :(得分:0)
public class MySensorActivity extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mPressure;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
float millibars_of_pressure = event.values[0];
Log.e("sensorValues",String.valueOf(millibars_of_pressure));
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}