Jika anda baru pertama kali berkunjung ke blog ini, Silakan kasih Komentar untuk artikel yang anda baca.
![]()
[This place is by Dan Morrill, Open Source & Compatibility Program Manager. Tim Bray]
Android has an API for accessing a variety of figure types, much as an accelerometer or reddened sensor. Two of the most commonly-used sensors are accelerometers and magnetometers (that is, compasses.) Applications and devices ofttimes ingest these as forms of individual input, and to determine which artefact to orient the screen.
However, theres a newborn wrinkle: recently, a whatever devices have shipped (see here and here) that run Android on screens that are naturally genre in their orientation. That is, when held in the choice position, the screens are wider than they are tall. This introduces a whatever evenhandedly subtle issues that weve detected feat problems in whatever apps. Now, part of the think for this is that the Android SDK docs on the figure API left a pair things unsaid, directive whatever developers to ingest them incorrectly. Even a pair of our possess samples did the wrong thing. Sorry most that!
Fortunately, using these APIs aright is pretty simple, if you keep three rules in mind:
The figure number grouping utilised by the API for the uncolored class of the figure does not modify as the figure moves, and is the same as the OpenGL number system.
Applications staleness not adopt that the uncolored class is portrait. That's not true on every devices.
Applications that correct figure accumulation to on-screen pass staleness ever ingest android.view.Display.getRotation() to transpose figure coordinates to concealment coordinates — even if their manifest specifies portrait-only display.
If you have a brawny scenery in math, the three rules above haw be every you requirement to work discover the rest. But if thats not you, the rest of this place explains things step-by-step, and gives whatever tips for using sensors correctly.
The Basic Problem
Before we dive in, heres a counsel that I personally have institute to be helpful: ever advert that the figure datas number grouping never changes. Ever. The rest of this place is feat to speech most number systems and rotations and so on. But sometimes when your nous is unfathomable in 3D transforms, you crapper get disoriented, so Ive institute it helps to ofttimes remind myself that no matter what is happening to the screen, the figure number grouping never changes.
Now with that counsel in mind, we requirement an warning to speech about. Lets consider a ultimate app that draws an mark that ever points in the content of gravity, invigorating the mark as the individual moves the sound around, same a plumb-bob. When a typical sound is held normally, the mark points down, as shown in Figure A:
![]()
(Note: In the figures in this post, the letter G effectuation the content of somberness in the figure number system. In Figure A, for example, G = -y effectuation that somberness is allied with the devices negative-Y axis, as rhythmic by the accelerometer. And remember — the figure number grouping never changes!)
This app is pretty direct to implement in OpenGL: you simply requirement to entertainer an mark on a GL SurfaceView, after rotating the number expanse in salutation to the figure accumulation returned by the accelerometer. This meet works because — in this basic case — the OpenGL concealment number grouping lines up with the figure number system.
So, this framework works, and the mark module ever point down — until you invoke the sound likewise far.
So Whats the Problem?
Most Android devices ingest the accelerometer to notice when the figure is existence held sideways, and circumvolve the concealment accordingly. This ordinarily causes the apps to pass horizontally, from the point of analyse of the user.
What this modify actually does is remap the X and Y axes, feat the app to entertainer itself horizontally. However, the Android figure APIs define the figure number expanse to be qualifying to the top and side of the device — not the short and long sides. When the grouping reorients the concealment in salutation to retentive the sound sideways, the figure number grouping no longer lines up with the screens number system, and you get unexpected rotations in the pass of your app. Figure B shows an example:
![]()
There are are a pair different fixes for this difficulty that are commonly utilised today, but weve detected that these ofttimes dont work right on landscape-default devices.
A common prototypal attempt to solve the auto-rotation difficulty is to simply lock the concealment to semblance mode, via the android:screenOrientation attribute in AndroidManifest.xml. This prevents the grouping from performing a concealment number grouping remap in salutation to figure orientation, and so the figure and concealment number systems rest in sync. However, protection the concealment to semblance fashion this artefact prevents the number systems from getting discover of sync on portrait-default devices, but causes them to embellish discover of sync on landscape-default devices. This is because it forces a concealment modify on those devices.
The ordinal common framework is to notice when the figure is in genre mode, and equilibrate for it by adding a turning to the graphics that are displayed. Unfortunately, this framework is ofttimes exclusive a partial fix, because if you arent certain most detecting genre mode, you module again drive an extra rectification on landscape-default devices.
The Correct Fix
So whats a poor developer to do? This seems same a catch-22: you cant prevent concealment reorientation, but you cant equilibrate for it, either.
Or crapper you? Actually, you crapper compensate — you meet have to attain sure youre aright detecting when rectification is necessary. The question is, how does the figure verify you that its been reoriented? And the respond is: android.view.Display.getRotation().
That method module return digit of four values, indicating that either the figure has not been reoriented (ROTATION_0), or that it has been reoriented by 90 degrees, 180 degrees, or 270 degrees (which respectively are ROTATION_90, ROTATION_180, and ROTATION_270.)
Pay special attention to those terminal two. ROTATION_180 and ROTATION_270 mean that apiece figure actually has digit semblance and digit genre modes: connatural semblance and landscape, and the upside-down versions of each. Some Android devices that do 360 modify module ingest these turning modes as well, so you requirement to handle this generally, beyond meet business for semblance or genre mode.
Once you have the concealment class content in hand, you crapper treat it as a turning around the screens Z axis when performance graphics. By applying the turning to the values you get from your SensorEventListener, you crapper aright and reliably equilibrate for concealment reorientations on every devices.
Note that Display.getRotation() module verify you if the concealment has been reoriented at all, not that it was reoriented specifically in salutation to the accelerometer. For example, modify if you disable accelerometer-based modify by using android:screenOrientation=nosensor", your app strength still be reoriented if the individual has unsealed a hornlike keyboard on the device.
Because direction every this involves whatever science that crapper be a taste of a chore, as a lavatory weve provided the android.hardware.sensor.SensorManager.remapCoordinateSystem() method to do much of this remapping work for you. If you opt not to do ingest this method, you crapper attain a similar effect by essentially swapping axes, along with the conception of thumb that 2 axis swaps requires that you differ the ordinal axis. (Since this is a taste error-prone, we do propose that you ingest remapCoordinateSystem() when you can.)
Recipes for Sensuous Delights
Okay, today weve got a framework that we crapper rely on to work on every devices. But how do you update your app? To give you a more definitive serving assistance on how to mend your apps, Ive whipped up a whatever recipes for updating your apps.
Apps That Never Draw Sensor Data
Apps that never pass graphics derived from figure accumulation commonly dont requirement to attain any changes. Examples of this type of app are those that notice for bumps to the device, those that ingest sensors for intercommunicate input, apps that monitor g-forces (watching for free-fall or acceleration), and so on. These apps arent art images that depart according to the devices orientation.
This isnt a hornlike and fast rule; there belike are whatever apps discover there that do requirement to verify concealment class into consideration, modify though they dont entertainer graphics depicting the figure data. But, if your app meet uses sensors in the background, theres a good chance you wont requirement to attain any changes.
Apps That Work in Both Portrait and Landscape
Most Android apps work fine in both semblance and landscape, using the accepted tools. If your app is digit of these and you also ingest sensors, the exclusive modify your app belike requires is a draw to ingest the behavior I outlined above. That is:
Dont adopt that semblance is the choice mode.
Dont adopt that protection your app to semblance fashion solves this issue.
Dont adopt that disabling sensor-based modify solves this supply (since reorientations also become on whatever devices when the individual opens a keyboard.)
Check for the current figure class via getRotation(), and equilibrate accordingly, as careful earlier.
Apps That Only Work in One Orientation
Some apps — notably, whatever games — only work substantially (or at all!) in either semblance or genre mode. Its perfectly okay, of course, for much apps to lock themselves to the appropriate mode, and doing so simplifies the sensors quite a bit.
However, because Android devices actually hold digit genre and digit semblance modes, these apps still requirement to check the current orientation. That is, if an app locks itself to genre mode, it module requirement to action a rectification on portrait-default devices, but not on landscape-default devices. And of course — are you sick of hearing this yet? — this crapper be realised by checking the result of getRotation().
Phew! Quite a mouthful for what is a evenhandedly direct notion, erst you see whats feat on. But if I had to flux every that downbound into a azygos sentence, it would be this: android.view.Display.getRotation() is your friend.
I wish youve institute this aggregation useful; whats more, I wish youve institute it practical. Well keep rising our SDK and docs, and I wish youll keep rising your apps.
Happy coding!

Thanks
basyar.com