Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
step2:設(shè)計(jì)應(yīng)用的UI界面,main.xml
<SPAN style="FONT-SIZE: 18px"><STRONG><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/compass"
android:id="@+id/imageView"
/>
</LinearLayout></STRONG></SPAN>
step3:MainActivity.java
import android.app.Activity;
import android.content.Context;
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 MainActivity extends Activity {
private ImageView imageView;
/** 傳感器管理器 */
private SensorManager manager;
private SensorListener listener = new SensorListener();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.imageView);
imageView.setKeepScreenOn(true);//屏幕高亮
//獲取系統(tǒng)服務(wù)(SENSOR_SERVICE)返回一個(gè)SensorManager 對(duì)象
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
/**
* 獲取方向傳感器
* 通過(guò)SensorManager對(duì)象獲取相應(yīng)的Sensor類(lèi)型的對(duì)象
*/
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
//應(yīng)用在前臺(tái)時(shí)候注冊(cè)監(jiān)聽(tīng)器
manager.registerListener(listener, sensor,
SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause() {
//應(yīng)用不在前臺(tái)時(shí)候銷(xiāo)毀掉監(jiān)聽(tīng)器
manager.unregisterListener(listener);
super.onPause();
}
private final class SensorListener implements SensorEventListener {
private float predegree = 0;
@Override
public void onSensorChanged(SensorEvent event) {
/**
* values[0]: x-axis 方向加速度
values[1]: y-axis 方向加速度
values[2]: z-axis 方向加速度
*/
float degree = event.values[0];// 存放了方向值
/**動(dòng)畫(huà)效果*/
RotateAnimation animation = new RotateAnimation(predegree, degree,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(200);
imageView.startAnimation(animation);
predegree=-degree;
/**
float x=event.values[SensorManager.DATA_X];
float y=event.values[SensorManager.DATA_Y];
float z=event.values[SensorManager.DATA_Z];
Log.i("XYZ", "x="+(int)x+",y="+(int)y+",z="+(int)z);
*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
}
step4:AndroidManifest.xml
<SPAN style="FONT-SIZE: 18px"><STRONG><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.roco.sensor"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></STRONG></SPAN>
- android 傳感器(OnSensorChanged)使用介紹
- Android利用方向傳感器獲得手機(jī)的相對(duì)角度實(shí)例說(shuō)明
- Android利用Sensor(傳感器)實(shí)現(xiàn)指南針小功能
- Android編程實(shí)現(xiàn)獲取所有傳感器數(shù)據(jù)的方法
- Android開(kāi)發(fā)中的重力傳感器用法實(shí)例詳解
- Android利用Sensor(傳感器)實(shí)現(xiàn)水平儀功能
- Android實(shí)現(xiàn)電子羅盤(pán)(指南針)方向傳感器的應(yīng)用
- Android開(kāi)發(fā)中方向傳感器定義與用法詳解【附指南針實(shí)現(xiàn)方法】
- Android實(shí)現(xiàn)計(jì)步傳感器功能
- Android傳感器的簡(jiǎn)單使用方法
相關(guān)文章
Android中再按一次退出提醒實(shí)現(xiàn)的兩種方法
今天小編就為大家分享一篇關(guān)于Android中再按一次退出提醒實(shí)現(xiàn)的兩種方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
Android自定義View實(shí)現(xiàn)九宮格圖形解鎖(Kotlin版)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)九宮格圖形解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
學(xué)習(xí)Android開(kāi)發(fā)之RecyclerView使用初探
Android開(kāi)發(fā)學(xué)習(xí)之路的第一課RecyclerView使用初探,感興趣的小伙伴們可以參考一下2016-07-07
Android 讓自定義TextView的drawableLeft與文本一起居中
本文主要介紹Android 自定義控件TextView顯示居中問(wèn)題,在開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)遇到控件的重寫(xiě),這里主要介紹TextView的drawableLeft與文本一起居中的問(wèn)題2016-07-07
Android ANR(Application Not Responding)的分析
這篇文章主要介紹了Android ANR(Application Not Responding)的分析的相關(guān)資料,這里說(shuō)明什么原因出現(xiàn)應(yīng)用程序的強(qiáng)制關(guān)閉,并說(shuō)明該如何避免,需要的朋友可以參考下2017-08-08
android開(kāi)發(fā)教程之實(shí)現(xiàn)listview下拉刷新和上拉刷新效果
這篇文章主要介紹了android實(shí)現(xiàn)listview下拉刷新和上拉刷新效果,Android的ListView上拉下拉刷新,原理都一樣,在Touch事件中操作header/footer的paddingTop屬性,需要的朋友可以參考下2014-02-02
Android提高之AudioRecord實(shí)現(xiàn)助聽(tīng)器的方法
這篇文章主要介紹了Android中AudioRecord實(shí)現(xiàn)助聽(tīng)器的方法,對(duì)進(jìn)行Android項(xiàng)目開(kāi)發(fā)有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
android sdk安裝及開(kāi)發(fā)環(huán)境部署
本文給大家詳細(xì)講解了android sdk安裝方法以及android開(kāi)發(fā)環(huán)境部署方法,非常的細(xì)致全面,有需要的小伙伴務(wù)必詳細(xì)研究下。2015-11-11
詳解Android——藍(lán)牙技術(shù) 帶你實(shí)現(xiàn)終端間數(shù)據(jù)傳輸
藍(lán)牙技術(shù)在智能硬件方面有很多用武之地,本篇文章主要介紹了Android——藍(lán)牙技術(shù),實(shí)現(xiàn)兩個(gè)終端間數(shù)據(jù)的傳輸,有興趣的朋友可以了解一下。2016-12-12

