在传感器更改时,设备角度在0到105度之间

时间:2012-11-05 05:16:34

标签: android

我想获得0到90度之间的角度,当我的设备处于90度然后我获得105度时,我在SensorChnaged上获得了错误的学位 所以请帮助我获得正确的角度 我已经在设备三星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之间的设备角度

enter image description here

1 个答案:

答案 0 :(得分: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-180必须是0-90,然后才能使用这样的逻辑。

if(angle_value>90)
{
   angle_value=angle_value-90;
}