Android實(shí)現(xiàn)電子羅盤(指南針)方向傳感器的應(yīng)用
簡介
現(xiàn)在每部Android手機(jī)里邊都會內(nèi)置有許多傳感器,如光照傳感器、加速度傳感器、地磁傳感器、壓力傳感器、溫度傳感器等,它們能夠監(jiān)測到各種發(fā)生在手機(jī)撒花姑娘的物理事件。當(dāng)然Android系統(tǒng)只是負(fù)責(zé)將這些傳感器所輸出的信息傳遞給我們,然后我們可以利用這些信息去開發(fā)一些好玩的應(yīng)用。
圖片神馬的在網(wǎng)上搜個指南針圖片就好了,方便學(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); // 注冊傳感器(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)變化動畫類 參數(shù)說明: fromDegrees:旋轉(zhuǎn)的開始角度。 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)過程持續(xù)時間 ra.setDuration(200); //羅盤圖片使用旋轉(zhuǎn)動畫 imageView.startAnimation(ra); currentDegree = -degree; } } //傳感器精度的改變 public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- android 傳感器(OnSensorChanged)使用介紹
- Android利用方向傳感器獲得手機(jī)的相對角度實(shí)例說明
- Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
- Android利用Sensor(傳感器)實(shí)現(xiàn)指南針小功能
- Android編程實(shí)現(xiàn)獲取所有傳感器數(shù)據(jù)的方法
- Android開發(fā)中的重力傳感器用法實(shí)例詳解
- Android利用Sensor(傳感器)實(shí)現(xiàn)水平儀功能
- Android開發(fā)中方向傳感器定義與用法詳解【附指南針實(shí)現(xiàn)方法】
- Android實(shí)現(xiàn)計(jì)步傳感器功能
- Android傳感器的簡單使用方法
相關(guān)文章
Android ListView滑動刪除操作(SwipeListView)
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動刪除操作,主要是學(xué)習(xí)SwipeListView開源框架。感興趣的小伙伴們可以參考一下2016-08-08Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)
這篇文章主要介紹了Android自定義控件案例匯總,自定義開關(guān)、Listview實(shí)現(xiàn)下拉刷新、側(cè)滑菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android12?藍(lán)牙適配的實(shí)現(xiàn)步驟
本文主要介紹了Android12?藍(lán)牙適配的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Android筆記之:App應(yīng)用之啟動界面SplashActivity的使用
當(dāng)前比較成熟一點(diǎn)的應(yīng)用基本上都會在進(jìn)入應(yīng)用之顯示一個啟動界面.這個啟動界面或簡單,或復(fù)雜,或簡陋,或華麗,用意不同,風(fēng)格也不同2013-04-04