Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So after I thought about it, I realized I could just implement a similar algorithm as what Android itself uses to figure out the orientation. I do it using the onSenseorChanged callback</p> <pre><code>public static final int UPSIDE_DOWN = 3; public static final int LANDSCAPE_RIGHT = 4; public static final int PORTRAIT = 1; public static final int LANDSCAPE_LEFT = 2; public int mOrientationDeg; //last rotation in degrees public int mOrientationRounded; //last orientation int from above private static final int _DATA_X = 0; private static final int _DATA_Y = 1; private static final int _DATA_Z = 2; private int ORIENTATION_UNKNOWN = -1; @Override public void onSensorChanged(SensorEvent event) { Log.d(TAG, "Sensor Changed"); float[] values = event.values; int orientation = ORIENTATION_UNKNOWN; float X = -values[_DATA_X]; float Y = -values[_DATA_Y]; float Z = -values[_DATA_Z]; float magnitude = X*X + Y*Y; // Don't trust the angle if the magnitude is small compared to the y value if (magnitude * 4 &gt;= Z*Z) { float OneEightyOverPi = 57.29577957855f; float angle = (float)Math.atan2(-Y, X) * OneEightyOverPi; orientation = 90 - (int)Math.round(angle); // normalize to 0 - 359 range while (orientation &gt;= 360) { orientation -= 360; } while (orientation &lt; 0) { orientation += 360; } } //^^ thanks to google for that code //now we must figure out which orientation based on the degrees Log.d("Oreination", ""+orientation); if (orientation != mOrientationDeg) { mOrientationDeg = orientation; //figure out actual orientation if(orientation == -1){//basically flat } else if(orientation &lt;= 45 || orientation &gt; 315){//round to 0 tempOrientRounded = 1;//portrait } else if(orientation &gt; 45 &amp;&amp; orientation &lt;= 135){//round to 90 tempOrientRounded = 2; //lsleft } else if(orientation &gt; 135 &amp;&amp; orientation &lt;= 225){//round to 180 tempOrientRounded = 3; //upside down } else if(orientation &gt; 225 &amp;&amp; orientation &lt;= 315){//round to 270 tempOrientRounded = 4;//lsright } } if(mOrientationRounded != tempOrientRounded){ //Orientation changed, handle the change here mOrientationRounded = tempOrientRounded; } } </code></pre> <p>It looks more complecated than it is, but just know that it works(I'd say equally well as the system one works). Dont forget to register your sensor change event listener in onResume and onPause for accelerometer</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload