Orientation of Android devices is a landscape, but sensors behave like orientation - portrait

I have a problem with orientation in android. The device orientation is the landscape that I set in the AndroidManifest.xml file.

android:screenOrientation="landscape" 

I also hold the device in the landscape, so everything (layouts, views, etc.) is in landscape mode as I wanted. But only when I hold the device in the portrait does it give me the values ​​that I need.

I’ve modified some code from the Internet and it works for HTC 3D EVO, Nexus 7 and Samsung Galaxy S3, but not on the Galaxy Tablet 10 ".

I found this post. Is Android Orientation Sensor different for different devices? The accepted answer suggests getRotation () and remapCoordinateSystem () methods, but no mention of how.

This is how I ended up.

  SensorManager.getRotationMatrix(Rmat, Imat, gData, mData); SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2); // Orientation isn't as useful as a rotation matrix, but // we'll show it here anyway. SensorManager.getOrientation(R2, orientation); float incl = SensorManager.getInclination(Imat); FontTextView pitchText = (FontTextView)findViewById(R.id.pitch_text); if((int)(orientation[1]*90) <= -110 && !newQuestionIsActive) { newQuestionIsActive = true; pitchText.setText("Bring new question"); } else if(Math.abs((int)(orientation[2]*90)) <= 200 && (int)(orientation[1]*90) >= -30 && newQuestionIsActive) { newQuestionIsActive = false; pitchText.setText("Not Correct!"); } else if(Math.abs((int)(orientation[2]*90)) > 200 && (int)(orientation[1]*90) >= -30 && newQuestionIsActive) { newQuestionIsActive = false; pitchText.setText("Congratulations!"); } 

Now, how should I use getRotation () in this code? And there may be a brief explanation, it would be useful to understand why the 3 devices mentioned are working fine, but not the Galaxy Tablet.

This issue is described in the Android documentation.

Finally, if your application maps sensor data to an on-screen display, you need to use the getRotation () method to determine screen rotation, and then use remapCoordinateSystem () to map the sensor coordinates to screen coordinates. You need to do this even if your manifest specifies only portrait display.

http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html

+4
source share
2 answers

Found a solution.

The problem is caused by the default orientation for newer devices, and the portrait of others is outdated. Thus, the sensor manager behaves accordingly. To get the sensor manager working, you expect that you need to define the default DisplayRotation of the device and change the remapCoordinateSystem () parameter logic.

I changed

 SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2); 

to

 int rotation = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); if(rotation == 0) // Default display rotation is portrait SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, R2); else // Default display rotation is landscape SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2); 

now it works ... I hope it will be useful for someone else!

+12
source

I do not know about your path. But the following method works for me.

 int orientation = getResources().getConfiguration().orientation; 

Here orientation is 1 if the device is in landscape and 2 if the device is in portrait mode.

Hope this helps you.

0
source

Source: https://habr.com/ru/post/1488293/


All Articles