欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android方向傳感器的使用方法

 更新時(shí)間:2018年10月19日 10:25:20   作者:亦魚  
這篇文章主要介紹了Android方向傳感器的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

在應(yīng)用程序中使用SensorManager.getOrientation()來獲得原始數(shù)據(jù)。

public static float[] getOrientation (float[] R, float[] values)

第一個(gè)參數(shù)是R用來保存磁場(chǎng)和加速度的數(shù)據(jù),通過該函數(shù)獲取方位角。

第二個(gè)參數(shù)是函數(shù)輸出,數(shù)據(jù)自動(dòng)填充。

  • values[0]:方向角,但用(磁場(chǎng)+加速度)得到的數(shù)據(jù)范圍是(-180~180),也就是說,0表示正北,90表示正東,180/-180表示正南,-90表示正西。而直接通過方向感應(yīng)器數(shù)據(jù)范圍是(0~359)360/0表示正北,90表示正東,180表示正南,270表示正西。
  • values[1]:pitch 傾斜角即由靜止?fàn)顟B(tài)開始,前后翻轉(zhuǎn),手機(jī)頂部往上抬起(0~-90),手機(jī)尾部往上抬起(0~90)
  • values[2]:roll 旋轉(zhuǎn)角 即由靜止?fàn)顟B(tài)開始,左右翻轉(zhuǎn),手機(jī)左側(cè)抬起(0~90),手機(jī)右側(cè)抬起(0~-90)

通過函數(shù)getRotationMatrix獲取R

public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)

注冊(cè)監(jiān)聽

sensorManager.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_GAME); 
sensorManager.registerListener(this, mag_sensor,SensorManager.SENSOR_DELAY_GAME); 

主要代碼

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.Menu; 
import android.widget.TextView; 
 
public class MainActivity extends Activity implements SensorEventListener{ 
 
  private SensorManager sensorManager; 
  private Sensor acc_sensor; 
  private Sensor mag_sensor; 
  //加速度傳感器數(shù)據(jù) 
  float accValues[] = new float[3]; 
  //地磁傳感器數(shù)據(jù) 
  float magValues[] = new float[3]; 
  //旋轉(zhuǎn)矩陣,用來保存磁場(chǎng)和加速度的數(shù)據(jù) 
  float r[] = new float[9]; 
  //模擬方向傳感器的數(shù)據(jù)(原始數(shù)據(jù)為弧度) 
  float values[] = new float[3]; 
  TextView showTV = null; 
  
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    show_change=(TextView) findViewById(R.id.show_change); 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    acc_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    mag_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
    // 注冊(cè)監(jiān)聽: 
    sensorManager.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_GAME); 
    sensorManager.registerListener(this, mag_sensor,SensorManager.SENSOR_DELAY_GAME); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  }
   
  // 回調(diào)方法 
  @Override 
  public void onSensorChanged(SensorEvent event) { 
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ 
      accValues = event.values.clone();
    } 
    else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){ 
      magValues = event.values.clone();
    }
    
    /**
     * r:要填充的旋轉(zhuǎn)數(shù)組 
     * I: 將磁場(chǎng)數(shù)據(jù)轉(zhuǎn)換進(jìn)實(shí)際的重力坐標(biāo)中,一般默認(rèn)情況下可以設(shè)置為null 
     * gravity: 加速度傳感器數(shù)據(jù) 
     * geomagnetic:地磁傳感器數(shù)據(jù) 
     */ 
    SensorManager.getRotationMatrix(r, null, accValues, magValues);
     
    /** 
     * R:旋轉(zhuǎn)數(shù)組 
     * values:模擬方向傳感器的數(shù)據(jù) 
     */ 
    SensorManager.getOrientation(r, values); 
     
    //將弧度轉(zhuǎn)化為角度后輸出 
    StringBuffer buff = new StringBuffer(); 
    for(float value : values){ 
      value=(float) Math.toDegrees(value); 
      buff.append(value + " "); 
    } 
    
    showTV.setText(buff.toString());   
  } 
   
  @Override 
  public void onAccuracyChanged(Sensor sensor, int accuracy) { 
  } 
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論