您好,登錄后才能下訂單哦!
怎么在Android項目中實現一個重力傳感器功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
首先我們創建一個傳感器管理器和一個傳感器監聽器,管理器用來管理傳感器以及創建各種各樣的傳感器,監聽器用來監視傳感器的變化并且進行相應的操作
private SensorManager sensorManager; private MySensorEventListener mySensorEventListener; mySensorEventListener= new MySensorEventListener();//這個監聽器當然是我們自己定義的,在重力感應器感應到手機位置有變化的時候,我們可以采取相應的操作,這里緊緊是將x,y,z的值打印出來 private final class MySensorEventListener implements SensorEventListener{ @Override //可以得到傳感器實時測量出來的變化值 public void onSensorChanged(SensorEvent event) { //重力傳感器 if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){ float x = event.values[SensorManager.DATA_X]; float y = event.values[SensorManager.DATA_Y]; float z = event.values[SensorManager.DATA_Z]; //tv_accelerometer是界面上的一個TextView標簽,不再贅述 tv_orientation.setText("Orientation:"+x+","+y+","+z); } }
我們在onResume()
方法中創建重力傳感器,并向系統注冊監聽器
protected void onResume() { Sensor sensor_accelerometer=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(mySensorEventListener,sensor_accelerometer, SensorManager.SENSOR_DELAY_UI); super.onResume(); }
最后我們在onPause()
中注銷所有傳感器的監聽,釋放重力感應器資源!
protected void onPause() { //注銷所有傳感器的監聽 sensorManager.unregisterListener(mySensorEventListener); super.onPause(); }
到此,有關重力傳感器的介紹完畢!
接下來看一個Android用重力傳感器做橫豎屏切換的例子
在播放視頻的時候,可能要做橫豎屏的切換,但是,用戶可以設置自己的手機關掉屏幕旋轉,這個時候就需要想其他的辦法了,比如:重力傳感器。
public class ScreenSwitchUtils { private static final String TAG = ScreenSwitchUtils.class.getSimpleName(); private volatile static ScreenSwitchUtils mInstance; private Activity mActivity; // 是否是豎屏 private boolean isPortrait = true; private SensorManager sm; private OrientationSensorListener listener; private Sensor sensor; private SensorManager sm1; private Sensor sensor1; private OrientationSensorListener1 listener1; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 888: int orientation = msg.arg1; if (orientation > 45 && orientation < 135) { } else if (orientation > 135 && orientation < 225) { } else if (orientation > 225 && orientation < 315) { if (isPortrait) { Log.e(test, 切換成橫屏); mActivity.setRequestedOrientation(0); isPortrait = false; } } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) { if (!isPortrait) { Log.e(test,切換成豎屏); mActivity.setRequestedOrientation(1); isPortrait = true; } } break; default: break; } }; }; /** 返回ScreenSwitchUtils單例 **/ public static ScreenSwitchUtils init(Context context) { if (mInstance == null) { synchronized (ScreenSwitchUtils.class) { if (mInstance == null) { mInstance = new ScreenSwitchUtils(context); } } } return mInstance; } private ScreenSwitchUtils(Context context) { Log.d(TAG, init orientation listener.); // 注冊重力感應器,監聽屏幕旋轉 sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); listener = new OrientationSensorListener(mHandler); // 根據 旋轉之后/點擊全屏之后 兩者方向一致,激活sm. sm1 = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); sensor1 = sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); listener1 = new OrientationSensorListener1(); } /** 開始監聽 */ public void start(Activity activity) { Log.d(TAG, start orientation listener.); mActivity = activity; sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI); } /** 停止監聽 */ public void stop() { Log.d(TAG, stop orientation listener.); sm.unregisterListener(listener); sm1.unregisterListener(listener1); } /** * 手動橫豎屏切換方向 */ public void toggleScreen() { sm.unregisterListener(listener); sm1.registerListener(listener1, sensor1,SensorManager.SENSOR_DELAY_UI); if (isPortrait) { isPortrait = false; // 切換成橫屏 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { isPortrait = true; // 切換成豎屏 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } public boolean isPortrait(){ return this.isPortrait; } /** * 重力感應監聽者 */ public class OrientationSensorListener implements SensorEventListener { private static final int _DATA_X = 0; private static final int _DATA_Y = 1; private static final int _DATA_Z = 2; public static final int ORIENTATION_UNKNOWN = -1; private Handler rotateHandler; public OrientationSensorListener(Handler handler) { rotateHandler = handler; } public void onAccuracyChanged(Sensor arg0, int arg1) { } public void onSensorChanged(SensorEvent event) { float[] values = event.values; int orientation = ORIENTATION_UNKNOWN; float X = -values[_DATA_X]; float Y = -values[_DATA_Y]; float Z = -values[_DATA_Z]; float magnitude = X * X + Y * Y; // Don't trust the angle if the magnitude is small compared to the y // value if (magnitude * 4 >= Z * Z) { // 屏幕旋轉時 float OneEightyOverPi = 57.29577957855f; float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi; orientation = 90 - (int) Math.round(angle); // normalize to 0 - 359 range while (orientation >= 360) { orientation -= 360; } while (orientation < 0) { orientation += 360; } } if (rotateHandler != null) { rotateHandler.obtainMessage(888, orientation, 0).sendToTarget(); } } } public class OrientationSensorListener1 implements SensorEventListener { private static final int _DATA_X = 0; private static final int _DATA_Y = 1; private static final int _DATA_Z = 2; public static final int ORIENTATION_UNKNOWN = -1; public OrientationSensorListener1() { } public void onAccuracyChanged(Sensor arg0, int arg1) { } public void onSensorChanged(SensorEvent event) { float[] values = event.values; int orientation = ORIENTATION_UNKNOWN; float X = -values[_DATA_X]; float Y = -values[_DATA_Y]; float Z = -values[_DATA_Z]; float magnitude = X * X + Y * Y; // Don't trust the angle if the magnitude is small compared to the y // value if (magnitude * 4 >= Z * Z) { // 屏幕旋轉時 float OneEightyOverPi = 57.29577957855f; float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi; orientation = 90 - (int) Math.round(angle); // normalize to 0 - 359 range while (orientation >= 360) { orientation -= 360; } while (orientation < 0) { orientation += 360; } } if (orientation > 225 && orientation < 315) {// 檢測到當前實際是橫屏 if (!isPortrait) { sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI); sm1.unregisterListener(listener1); } } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {// 檢測到當前實際是豎屏 if (isPortrait) { sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI); sm1.unregisterListener(listener1); } } } } }
使用的時候:
public class MainActivity extends Activity implements OnClickListener { private ScreenSwitchUtils instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); instance = ScreenSwitchUtils.init(this.getApplicationContext()); } @Override protected void onStart() { super.onStart(); instance.start(this); } @Override protected void onStop() { super.onStop(); instance.stop(); } @SuppressLint(NewApi) @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.e(test, onConfigurationChanged); if (instance.isPortrait()) { // 切換成豎屏 LayoutParams params1 = new RelativeLayout.LayoutParams(screenWidth, DensityUtil.dip2px(this, 160)); videoView.setLayoutParams(params1); Toast.makeText(getApplicationContext(), 豎屏, 0).show(); Log.e(test, 豎屏); } else { // 切換成橫屏 LayoutParams params1 = new RelativeLayout.LayoutParams(screenHeight, screenWidth); videoView.setLayoutParams(params1); Toast.makeText(getApplicationContext(), 橫屏, 0).show(); Log.e(test, 橫屏); } } @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.iv_stretch: instance.toggleScreen(); break; } } }
調用了activity.setRequestedOrientation()
以后,會觸發activity.onConfigurationChanged();
可以在這里面重新設置播放界面的大小。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。