Android 利用方向感應器實現指南針具體步驟

來源:互聯網
上載者:User

step1:建立一個項目Compass,並將一張指南針圖片匯入到res/drawable-hdpi目錄中

step2:設計應用的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);//螢幕高亮
//擷取系統服務(SENSOR_SERVICE)返回一個SensorManager 對象
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
/**
* 擷取方向感應器
* 通過SensorManager對象擷取相應的Sensor類型的對象
*/
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
//應用在前台時候註冊監聽器
manager.registerListener(listener, sensor,
SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause() {
//應用不在前台時候銷毀掉監聽器
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];// 存放了方向值
/**動畫效果*/
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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.