Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> 安卓省電與加速 >> anroid 利用方向傳感器或者加速度傳感器實現手機或者平板6個方向操作

anroid 利用方向傳感器或者加速度傳感器實現手機或者平板6個方向操作

編輯:安卓省電與加速

最近項目裡面要實現android設備6個方向操作遠程設備移動,首先想到的是方向傳感器,但是由於android設備的多樣性,在測試過程中並不是所有的設備都有方向傳感器,當沒有方向傳感器的話,就使用加速度傳感器來實現,但是有一個缺點就是當水平移動的時候,就會有錯誤,其次就是項目中程序規定是橫屏,然後手機和平板第一方向是不同的,手機默認豎屏正方向為0,現在橫屏則方向為1,而平板默認就是橫屏的,所以橫屏正方向為0。

對於源碼裡面的


[java]
private float accDefaultX = 9999; 
private float accDefaultY = 9999; 
private float fBaseDefault = 9999; 

 private float accDefaultX = 9999;
 private float accDefaultY = 9999;
 private float fBaseDefault = 9999;是用來確定初始方向的,程序是按照開啟傳感器的方向為水平方向,如果需要使用現實中的水平方向為設備的水平方向,則可以去掉


[java]
if (accDefaultX == fBaseDefault) { 
                accDefaultX = fValues[0]; 
            } 
            if (accDefaultY == fBaseDefault) { 
                accDefaultY = fValues[1]; 
            } 
            mx = fValues[0] - accDefaultX; 
            my = fValues[1] - accDefaultY; 

if (accDefaultX == fBaseDefault) {
    accDefaultX = fValues[0];
   }
   if (accDefaultY == fBaseDefault) {
    accDefaultY = fValues[1];
   }
   mx = fValues[0] - accDefaultX;
   my = fValues[1] - accDefaultY;

 

源碼如下:


[java]
package com.zhangjie.sensor; 
 
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.util.Log; 
import android.view.Display; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.widget.Button; 
 
public class OriginSensor extends Activity implements SensorEventListener{ 
 
    public static final String TAG = "OriginSensor"; 
    private SensorManager mSensorManager; 
    private Sensor aSensor; 
    private WindowManager mWindowManager; 
    private Display mDisplay; 
    private int orientation = 0; 
    private float accDefaultX = 9999; 
    private float accDefaultY = 9999; 
    private float fBaseDefault = 9999; 
    private double angle = 25; 
    private float[] fValues = null; 
    public static int Sensor_Type = -1; 
    float mx, my; 
    Button clickButton; 
    boolean iscleck = false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
        mDisplay = mWindowManager.getDefaultDisplay(); 
        orientation = mDisplay.getOrientation(); 
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
        clickButton = new Button(this); 
        clickButton.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                if (!iscleck) { 
                    EnableSensor(); 
                    iscleck = true; 
                }else { 
                    DisableSensor(); 
                    iscleck = false; 
                } 
            } 
        }); 
        setContentView(clickButton); 
    } 
 
    public void EnableSensor(){ 
        if (Sensor_Type == -1) { 
            SelectSensorType(); 
            if (Sensor_Type == 2) { 
                //屏蔽功能  
            } 
        }else { 
            aSensor = mSensorManager.getDefaultSensor(Sensor_Type); 
            mSensorManager.registerListener(this, aSensor,SensorManager.SENSOR_DELAY_UI); 
        } 
        accDefaultX = fBaseDefault; //這樣做的原因為每次重啟,將重啟的位置作為水平位置  
        accDefaultY = fBaseDefault; 
    } 
     
    public void DisableSensor(){ 
        mSensorManager.unregisterListener(this); 
        current_dir = 0; 
    } 
    /*
     * 優先選擇方向傳感器,如果沒有方向傳感器則選擇加速度傳感器,如果沒有加速度傳感器則屏蔽重力感應功能
     * Sensor_Type = 3 :方向傳感器
     * Sensor_Type = 1 :加速度傳感器
     * Sensor_Type = 2:屏蔽重力感應功能
     */ 
    public void SelectSensorType(){ 
        aSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 
        boolean ok = mSensorManager.registerListener(this, aSensor,SensorManager.SENSOR_DELAY_UI); 
        if (ok) { 
            Sensor_Type = Sensor.TYPE_ORIENTATION; 
            angle = 25; 
            Log.e(TAG, "啟用方向傳感器"); 
        }else 
        { 
            aSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
            boolean ok2 = mSensorManager.registerListener(this, aSensor,SensorManager.SENSOR_DELAY_UI); 
            if (ok2) { 
                Sensor_Type = Sensor.TYPE_ACCELEROMETER; 
                angle = 2.5; 
                Log.e(TAG, "啟用加速度傳感器"); 
            }else { 
                Sensor_Type = 2; 
                Log.e(TAG, "屏蔽重力感應功能"); 
            } 
        } 
    } 
 
 
    @Override 
    public void onSensorChanged(SensorEvent event) { 
        if (Sensor_Type == Sensor.TYPE_ACCELEROMETER) { 
            fValues = event.values; 
            if (accDefaultX == fBaseDefault) { 
                accDefaultX = fValues[0]; 
            } 
            if (accDefaultY == fBaseDefault) { 
                accDefaultY = fValues[1]; 
            } 
            mx = fValues[0] - accDefaultX; 
            my = fValues[1] - accDefaultY; 
            Log.e("test", "vlaue[0]: "+mx+  "   value[1]:"+my); 
            if (orientation == 0) { 
                if (my < -2.5 && Math.abs(mx) < 0.5 && current_dir != 1) { 
                    current_dir = 1;  //前  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (my > 2.5 && Math.abs(mx) < 0.5 && current_dir != 2) { 
                    current_dir = 2;  //後  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (Math.abs(mx)  <  0.5 && Math.abs(my) < 0.5 && current_dir != 3) { 
                    current_dir = 3;  //停止  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx > 1.5 && my <= 0.0 && current_dir != 4) { 
                    current_dir = 4;  //左轉/左前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx < -1.5 && my <= 0.0 && current_dir != 5) { 
                    current_dir = 5;  //右轉/右前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx > 1.5 && my > 1.5 && current_dir != 6) { 
                    current_dir = 6;  //左後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx < -1.5 && my > 1.5 && current_dir != 7) { 
                    current_dir = 7;  //右後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                } 
            }else if (orientation == 1) { 
                if (mx < -2.5 && Math.abs(my) < 0.5 && current_dir != 1) { 
                    current_dir = 1;  //前  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx > 2.5 && Math.abs(my) < 0.5 && current_dir != 2) { 
                    current_dir = 2;  //後  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (Math.abs(mx)  <  0.5 && Math.abs(my) < 0.5 && current_dir != 3) { 
                    current_dir = 3;  //停止  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx <= 0.0 && my < -1.5 && current_dir != 4) { 
                    current_dir = 4;  //左轉/左前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx <= 0.0 && my > 1.5 && current_dir != 5) { 
                    current_dir = 5;  //右轉/右前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx > 1.5 && my < -1.5 && current_dir != 6) { 
                    current_dir = 6;  //左後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (mx > 1.5 && my > 1.5 && current_dir != 7) { 
                    current_dir = 7;  //右後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                } 
            } 
        }else if (Sensor_Type == Sensor.TYPE_ORIENTATION) { 
            if (orientation == 0) { 
                fValues = event.values; 
                Log.e("test", "vlaue[0]: "+fValues[0]+  "   value[1]:"+fValues[1]+"  value[2]:"+fValues[2]); 
                if (fValues[1] > angle && (Math.abs(fValues[2]) <  5) && current_dir != 1) {   
                    current_dir = 1;  //前  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] < -angle && (Math.abs(fValues[2])  <  5) && current_dir != 2) { 
                    current_dir = 2;  //後  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (Math.abs(fValues[1])  <  5 && Math.abs(fValues[2]) < 5 && current_dir != 3) { 
                    current_dir = 3;  //停止  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] > 0 && fValues[2] > 15 && current_dir != 4) { 
                    current_dir = 4;  //左轉/左前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] > 0 && fValues[2] < -15 && current_dir != 5) { 
                    current_dir = 5;  //右轉/右前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] < -15 && fValues[2] > 15 && current_dir != 6) { 
                    current_dir = 6;  //左後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] < -15 && fValues[2] < -15 && current_dir != 7) { 
                    current_dir = 7;  //右後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                } 
            }else if (orientation == 1) { 
                fValues = event.values; 
                Log.e("test_1", "vlaue[0]: "+fValues[0]+  "   value[1]:"+fValues[1]+"  value[2]:"+fValues[2]); 
                if (fValues[2] < -angle && (Math.abs(fValues[1])  <  5) && current_dir != 1) {   
                    current_dir = 1;  //前  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[2] > angle && (Math.abs(fValues[1])  <  5) && current_dir != 2) { 
                    current_dir = 2;  //後  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (Math.abs(fValues[1])  <  5 && Math.abs(fValues[2]) < 5 && current_dir != 3) { 
                    current_dir = 3;  //停止  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] > 15 && fValues[2] <= 5 && current_dir != 4) { 
                    current_dir = 4;  //左轉/左前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] < -15 && fValues[2] <= 5 && current_dir != 5) { 
                    current_dir = 5;  //右轉/右前轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] > 15 && fValues[2] > 15 && current_dir != 6) { 
                    current_dir = 6;  //左後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                }else if (fValues[1] < -15 && fValues[2] > 15 && current_dir != 7) { 
                    current_dir = 7;  //右後轉  
                    Log.e("atest", orientation+ "  "+current_dir); 
                } 
            } 
        } 
    } 
 
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
         
    } 
 
    @Override 
    protected void onDestroy() { 
        super.onDestroy(); 
    } 
     
    public int current_dir = 0; 

package com.zhangjie.sensor;

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.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class OriginSensor extends Activity implements SensorEventListener{

 public static final String TAG = "OriginSensor";
 private SensorManager mSensorManager;
 private Sensor aSensor;
 private WindowManager mWindowManager;
 private Display mDisplay;
 private int orientation = 0;
 private float accDefaultX = 9999;
 private float accDefaultY = 9999;
 private float fBaseDefault = 9999;
 private double angle = 25;
 private float[] fValues = null;
 public static int Sensor_Type = -1;
 float mx, my;
 Button clickButton;
 boolean iscleck = false;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
     mDisplay = mWindowManager.getDefaultDisplay();
     orientation = mDisplay.getOrientation();
  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  clickButton = new Button(this);
  clickButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    if (!iscleck) {
     EnableSensor();
     iscleck = true;
    }else {
     DisableSensor();
     iscleck = false;
    }
   }
  });
  setContentView(clickButton);
 }

 public void EnableSensor(){
  if (Sensor_Type == -1) {
   SelectSensorType();
   if (Sensor_Type == 2) {
    //屏蔽功能
   }
  }else {
   aSensor = mSensorManager.getDefaultSensor(Sensor_Type);
   mSensorManager.registerListener(this, aSensor,SensorManager.SENSOR_DELAY_UI);
  }
  accDefaultX = fBaseDefault; //這樣做的原因為每次重啟,將重啟的位置作為水平位置
  accDefaultY = fBaseDefault;
 }
 
 public void DisableSensor(){
  mSensorManager.unregisterListener(this);
  current_dir = 0;
 }
 /*
  * 優先選擇方向傳感器,如果沒有方向傳感器則選擇加速度傳感器,如果沒有加速度傳感器則屏蔽重力感應功能
  * Sensor_Type = 3 :方向傳感器
  * Sensor_Type = 1 :加速度傳感器
  * Sensor_Type = 2:屏蔽重力感應功能
  */
 public void SelectSensorType(){
  aSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  boolean ok = mSensorManager.registerListener(this, aSensor,SensorManager.SENSOR_DELAY_UI);
  if (ok) {
   Sensor_Type = Sensor.TYPE_ORIENTATION;
   angle = 25;
   Log.e(TAG, "啟用方向傳感器");
  }else
  {
   aSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   boolean ok2 = mSensorManager.registerListener(this, aSensor,SensorManager.SENSOR_DELAY_UI);
   if (ok2) {
    Sensor_Type = Sensor.TYPE_ACCELEROMETER;
    angle = 2.5;
    Log.e(TAG, "啟用加速度傳感器");
   }else {
    Sensor_Type = 2;
    Log.e(TAG, "屏蔽重力感應功能");
   }
  }
 }


 @Override
 public void onSensorChanged(SensorEvent event) {
  if (Sensor_Type == Sensor.TYPE_ACCELEROMETER) {
   fValues = event.values;
   if (accDefaultX == fBaseDefault) {
    accDefaultX = fValues[0];
   }
   if (accDefaultY == fBaseDefault) {
    accDefaultY = fValues[1];
   }
   mx = fValues[0] - accDefaultX;
   my = fValues[1] - accDefaultY;
   Log.e("test", "vlaue[0]: "+mx+  "   value[1]:"+my);
   if (orientation == 0) {
    if (my < -2.5 && Math.abs(mx) < 0.5 && current_dir != 1) {
     current_dir = 1;  //前
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (my > 2.5 && Math.abs(mx) < 0.5 && current_dir != 2) {
     current_dir = 2;  //後
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (Math.abs(mx)  <  0.5 && Math.abs(my) < 0.5 && current_dir != 3) {
     current_dir = 3;  //停止
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx > 1.5 && my <= 0.0 && current_dir != 4) {
     current_dir = 4;  //左轉/左前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx < -1.5 && my <= 0.0 && current_dir != 5) {
     current_dir = 5;  //右轉/右前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx > 1.5 && my > 1.5 && current_dir != 6) {
     current_dir = 6;  //左後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx < -1.5 && my > 1.5 && current_dir != 7) {
     current_dir = 7;  //右後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }
   }else if (orientation == 1) {
    if (mx < -2.5 && Math.abs(my) < 0.5 && current_dir != 1) {
     current_dir = 1;  //前
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx > 2.5 && Math.abs(my) < 0.5 && current_dir != 2) {
     current_dir = 2;  //後
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (Math.abs(mx)  <  0.5 && Math.abs(my) < 0.5 && current_dir != 3) {
     current_dir = 3;  //停止
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx <= 0.0 && my < -1.5 && current_dir != 4) {
     current_dir = 4;  //左轉/左前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx <= 0.0 && my > 1.5 && current_dir != 5) {
     current_dir = 5;  //右轉/右前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx > 1.5 && my < -1.5 && current_dir != 6) {
     current_dir = 6;  //左後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (mx > 1.5 && my > 1.5 && current_dir != 7) {
     current_dir = 7;  //右後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }
   }
  }else if (Sensor_Type == Sensor.TYPE_ORIENTATION) {
   if (orientation == 0) {
    fValues = event.values;
    Log.e("test", "vlaue[0]: "+fValues[0]+  "   value[1]:"+fValues[1]+"  value[2]:"+fValues[2]);
    if (fValues[1] > angle && (Math.abs(fValues[2]) <  5) && current_dir != 1) { 
     current_dir = 1;  //前
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] < -angle && (Math.abs(fValues[2])  <  5) && current_dir != 2) {
     current_dir = 2;  //後
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (Math.abs(fValues[1])  <  5 && Math.abs(fValues[2]) < 5 && current_dir != 3) {
     current_dir = 3;  //停止
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] > 0 && fValues[2] > 15 && current_dir != 4) {
     current_dir = 4;  //左轉/左前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] > 0 && fValues[2] < -15 && current_dir != 5) {
     current_dir = 5;  //右轉/右前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] < -15 && fValues[2] > 15 && current_dir != 6) {
     current_dir = 6;  //左後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] < -15 && fValues[2] < -15 && current_dir != 7) {
     current_dir = 7;  //右後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }
   }else if (orientation == 1) {
    fValues = event.values;
    Log.e("test_1", "vlaue[0]: "+fValues[0]+  "   value[1]:"+fValues[1]+"  value[2]:"+fValues[2]);
    if (fValues[2] < -angle && (Math.abs(fValues[1])  <  5) && current_dir != 1) { 
     current_dir = 1;  //前
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[2] > angle && (Math.abs(fValues[1])  <  5) && current_dir != 2) {
     current_dir = 2;  //後
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (Math.abs(fValues[1])  <  5 && Math.abs(fValues[2]) < 5 && current_dir != 3) {
     current_dir = 3;  //停止
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] > 15 && fValues[2] <= 5 && current_dir != 4) {
     current_dir = 4;  //左轉/左前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] < -15 && fValues[2] <= 5 && current_dir != 5) {
     current_dir = 5;  //右轉/右前轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] > 15 && fValues[2] > 15 && current_dir != 6) {
     current_dir = 6;  //左後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }else if (fValues[1] < -15 && fValues[2] > 15 && current_dir != 7) {
     current_dir = 7;  //右後轉
     Log.e("atest", orientation+ "  "+current_dir);
    }
   }
  }
 }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
 }
 
 public int current_dir = 0;
}

 

 

 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved