我只想获得 0 到 90 度之间的角度,当我的设备处于 90 度时,我的传感器角度错误,然后我得到 105 度,所以请帮助我获得正确的角度,我已经在设备 samsung galaxy s duos 和micromax 并且还会冻结 UI。
private void init() {
oldBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.clock1);
w = oldBitmap.getWidth();
h = oldBitmap.getHeight();
mtx = new Matrix();
degrees = Integer.parseInt(x.toString());
mtx.postRotate(-degrees);
oldBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, w, h, mtx, true);
img.setScaleType(ScaleType.CENTER);
img.setImageBitmap(oldBitmap);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//x = (int) Math.pow(event.values[0], 2); // is for the right to left or left to right side angle
//x = (int) Math.pow(event.values[2], 2); // this is for the bottom to top or top to bottom side angle
x = (int) Math.pow(event.values[1], 2); // this is for both side angle
try {
Log.i("my x is..", "my degree is..."+x);
txtanglex.setText(x.toString());
init();
} catch (Exception e) {
e.printStackTrace();
}
}
}
从上面的代码我得到0到105之间的设备角度