0

我只想获得 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之间的设备角度

在此处输入图像描述

4

1 回答 1

0

这是使用传感器获取角度的示例代码

private float[] mGravity;
private float[] mMagnetic;
private float angle_value;

private float getDirection()
{

    float[] temp = new float[9];
    float[] R = new float[9];
    //Load rotation matrix into R
    SensorManager.getRotationMatrix(temp, null,
            mGravity, mMagnetic);

    //Remap to camera's point-of-view
    SensorManager.remapCoordinateSystem(temp,
            SensorManager.AXIS_X,
            SensorManager.AXIS_Z, R);

    //Return the orientation values
    float[] values = new float[3];
    SensorManager.getOrientation(R, values);

    //Convert to degrees
    for (int i=0; i < values.length; i++) {
        Double degrees = (values[i] * 180) / Math.PI;
        values[i] = degrees.floatValue();
    }

    return values[0];

}

@Override
public void onSensorChanged(SensorEvent event) {
    switch(event.sensor.getType()) {

    case Sensor.TYPE_ACCELEROMETER:
        mGravity = event.values.clone();
        break;
    case Sensor.TYPE_MAGNETIC_FIELD:
        mMagnetic = event.values.clone();
        break;
    default:
        return;
    }
    if(mGravity != null && mMagnetic != null) {
        angle_value = getDirection();
    }
}

如果您只想使用这样的逻辑,angle_value则必须这样做。0-1800-90

if(angle_value>90)
{
   angle_value=angle_value-90;
}
于 2012-11-05T06:30:25.510 回答