亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)電子羅盤(指南針)方向傳感器的應用

 更新時間:2019年03月27日 14:32:21   作者:人走丿茶涼  
今天小編就為大家分享一篇關于Android實現(xiàn)電子羅盤(指南針)方向傳感器的應用,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

簡介

現(xiàn)在每部Android手機里邊都會內(nèi)置有許多傳感器,如光照傳感器、加速度傳感器、地磁傳感器、壓力傳感器、溫度傳感器等,它們能夠監(jiān)測到各種發(fā)生在手機撒花姑娘的物理事件。當然Android系統(tǒng)只是負責將這些傳感器所輸出的信息傳遞給我們,然后我們可以利用這些信息去開發(fā)一些好玩的應用。

圖片神馬的在網(wǎng)上搜個指南針圖片就好了,方便學習

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); 
  } 
  //傳感器報告新的值(方向改變) 
  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坐標的伸縮值。 
      pivotYType:Y軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
      pivotYValue:Y坐標的伸縮值 
      */ 
      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)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接

相關文章

最新評論