簡易指南針實現

開發指南針的思蹄很簡單:程序先準備一張指南針圖片,該圖片上方向指引指同北方。接下來開發一個檢測方向的傳感器,程序檢測到手機頂部繞z軸轉過多少度,讓指南針圖片反向轉過多少度便可。java

main.xmlandroid

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/znzImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/znz" />

</LinearLayout>

Compass.javaapp

package org.crazyit.sensor;

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;
import android.widget.TextView;

/**
 * Description: <br/>
 * site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
 * Copyright (C), 2001-2014, Yeeku.H.Lee <br/>
 * This program is protected by copyright laws. <br/>
 * Program Name: <br/>
 * Date:
 * 
 * @author Yeeku.H.Lee kongyeeku@163.com
 * @version 1.0
 */
public class Compass extends Activity implements SensorEventListener {
    // 定義顯示指南針的圖片
    ImageView znzImage;
    TextView tv;
    // 記錄指南針圖片轉過的角度
    float currentDegree = 0f;

    // 定義Sensor管理器
    SensorManager mSensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 獲取界面中顯示指南針的圖片
        tv = (TextView) findViewById(R.id.txtInfo);
        znzImage = (ImageView) findViewById(R.id.znzImage);
        // 獲取傳感器管理服務
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 爲系統的方向傳感器註冊監聽器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        // 取消註冊
        mSensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    protected void onStop() {
        // 取消註冊
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // 獲取觸發event的傳感器類型
        int sensorType = event.sensor.getType();
        switch (sensorType) {
        case Sensor.TYPE_ORIENTATION:
            // 獲取繞Z軸轉過的角度。
            float degree = event.values[0];
            // 建立旋轉動畫(反向轉過degree度)
            RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            // 設置動畫的持續時間
            ra.setDuration(200);
            // 運行動畫
            znzImage.startAnimation(ra);
            currentDegree = -degree;
            tv.setText("角度:" + currentDegree);
            break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
}
image
相關文章
相關標籤/搜索