您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Android如何實(shí)現(xiàn)電子羅盤(指南針)方向傳感器的應(yīng)用的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
簡(jiǎn)介
現(xiàn)在每部Android手機(jī)里邊都會(huì)內(nèi)置有許多傳感器,如光照傳感器、加速度傳感器、地磁傳感器、壓力傳感器、溫度傳感器等,它們能夠監(jiān)測(cè)到各種發(fā)生在手機(jī)撒花姑娘的物理事件。當(dāng)然Android系統(tǒng)只是負(fù)責(zé)將這些傳感器所輸出的信息傳遞給我們,然后我們可以利用這些信息去開(kāi)發(fā)一些好玩的應(yīng)用。
圖片神馬的在網(wǎng)上搜個(gè)指南針圖片就好了,方便學(xué)習(xí)
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <ImageView android:id="@+id/compass_imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/compass" /> </LinearLayout>
MainActivity.java
import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.ImageView; /** * 電子羅盤 方向傳感器 */ public class ComPassActivity extends Activity implements SensorEventListener { private ImageView imageView; private float currentDegree = 0f; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.compass); imageView = (ImageView) findViewById(R.id.compass_imageView); // 傳感器管理器 SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE); // 注冊(cè)傳感器(Sensor.TYPE_ORIENTATION(方向傳感器);SENSOR_DELAY_FASTEST(0毫秒延遲); // SENSOR_DELAY_GAME(20,000毫秒延遲)、SENSOR_DELAY_UI(60,000毫秒延遲)) sm.registerListener(ComPassActivity.this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST); } //傳感器報(bào)告新的值(方向改變) public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { float degree = event.values[0]; /* RotateAnimation類:旋轉(zhuǎn)變化動(dòng)畫類 參數(shù)說(shuō)明: fromDegrees:旋轉(zhuǎn)的開(kāi)始角度。 toDegrees:旋轉(zhuǎn)的結(jié)束角度。 pivotXType:X軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 pivotXValue:X坐標(biāo)的伸縮值。 pivotYType:Y軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 pivotYValue:Y坐標(biāo)的伸縮值 */ RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //旋轉(zhuǎn)過(guò)程持續(xù)時(shí)間 ra.setDuration(200); //羅盤圖片使用旋轉(zhuǎn)動(dòng)畫 imageView.startAnimation(ra); currentDegree = -degree; } } //傳感器精度的改變 public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
感謝各位的閱讀!關(guān)于“Android如何實(shí)現(xiàn)電子羅盤(指南針)方向傳感器的應(yīng)用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。