Android Compass Accelerometer Magnetic sensor-android learning Journey (67)

Source: Internet
Author: User
<span id="Label3"></p>Since Andorid does not recommend the use of conventional directional sensors, it is recommended to use accelerometers and geomagnetic sensors to construct the data of the direction sensor, in fact the rotation angle of the z axis is the code example code as follows<pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>ImageView compass;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Sensormanager sensormanager;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Compass = (ImageView) Findviewbyid (r.id.compass); Sensormanager = (sensormanager) Getsystemservice (context.sensor_service); Sensor accelerometersensor = Sensormanager.getdefaultsensor (sensor.type_accelerometer); Sensor magneticsensor = Sensormanager.getdefaultsensor (sensor.type_magnetic_field); Sensormanager.registerlistener (sensoreventlistener,magneticsensor,sensormanager.sensor_delay_game); Sensormanager.registerlistener (sensoreventlistener,accelerometersensor,sensormanager.sensor_delay_game); }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Sensoreventlistener Sensoreventlistener =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Sensoreventlistener () {<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[] accelerometervalues =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[<span class="hljs-number"><span class="hljs-number">3</span></span>];<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[] maneticvalues =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[<span class="hljs-number"><span class="hljs-number">3</span></span>];<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>lastdegree;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onsensorchanged</span></span>(sensorevent Sensorevent) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(sensorEvent.sensor.getType () = = Sensor.type_accelerometer) {accelerometervalues = SensorEvent.values.clone (); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(sensorEvent.sensor.getType () = = Sensor.type_magnetic_field) {maneticvalues = SensorEvent.values.clone (); }<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[] R =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[<span class="hljs-number"><span class="hljs-number">9</span></span>];<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[] values =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[<span class="hljs-number"><span class="hljs-number">3</span></span>]; Sensormanager.getrotationmatrix (R,<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>, accelerometervalues,maneticvalues); Sensormanager.getorientation (r,values);<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Rotatedegree =-(<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>) Math.todegrees (values[<span class="hljs-number"><span class="hljs-number">0</span></span>]);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(math.abs (rotatedegree-lastdegree) ><span class="hljs-number"><span class="hljs-number">1</span></span>) {rotateanimation animation =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Rotateanimation (lastdegree,rotatedegree, animation.relative_to_self,<span class="hljs-number"><span class="hljs-number">0.5</span></span>f,animation.relative_to_self,<span class="hljs-number"><span class="hljs-number">0.5</span></span>f); Animation.setfillafter (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>); Compass.startanimation (animation); Lastdegree = rotatedegree; } }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onaccuracychanged</span></span>(sensor sensor,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>I) {}};<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDestroy</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(sensormanager! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {sensormanager.unregisterlistener (sensoreventlistener); }<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnDestroy (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">Oncreateoptionsmenu</span></span>(menu Menu) {<span class="hljs-comment"><span class="hljs-comment">//inflate The menu; this adds items to the action bar if it is Present.</span></span>Getmenuinflater (). Inflate (r.menu.menu_main, menu);<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">onoptionsitemselected</span></span>(MenuItem Item) {<span class="hljs-comment"><span class="hljs-comment">//Handle Action Bar Item clicks Here. The Action Bar</span> would</span> <span class="hljs-comment"><span class="hljs-comment">//automatically handle clicks on the Home/up button, so long</span></span> <span class="hljs-comment"><span class="hljs-comment">//as You specify a parent activity in Androidmanifest.xml.</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>id = item.getitemid ();<span class="hljs-comment"><span class="hljs-comment">//noinspection simplifiableifstatement</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(id = = R.id.action_settings) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onoptionsitemselected (item); }}</code></pre><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Compass Accelerometer Magnetic sensor-android learning Journey (67)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.