本文主要是介绍Android根据重力感应选装方向,四个方向都支持,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
不废话,直接上源码:
辅助工具类ChangeOrientationHandler.java
public class ChangeOrientationHandler extends Handler {private Activity activity;public ChangeOrientationHandler(Activity ac) {super();activity = ac;}@Overridepublic void handleMessage(Message msg) {if (msg.what == 888) {int orientation = msg.arg1;if (orientation > 45 && orientation < 135) {activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);Log.d(MainActivity.TAG, "横屏翻转");} else if (orientation > 135 && orientation < 225) {activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);Log.d(MainActivity.TAG, "竖屏翻转: 倒立");} else if (orientation > 225 && orientation < 315) {activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);Log.d(MainActivity.TAG, "横屏翻转 ");} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {activity.setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);Log.d(MainActivity.TAG, "竖屏翻转: 正立");}}super.handleMessage(msg);}
}
辅助工具类 OrientationSensorListener.java
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;if (magnitude * 4 >= Z * Z) {float OneEightyOverPi = 57.29577957855f;float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;orientation = 90 - (int) Math.round(angle);while (orientation >= 360) {orientation -= 360;}while (orientation < 0) {orientation += 360;}}if (rotateHandler != null) {rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();}}
}
可在activity或者baseactivity中使用
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";private Button button;private Handler handler;private OrientationSensorListener listener;private SensorManager sm;private Sensor sensor;@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler = new ChangeOrientationHandler(this);sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);listener = new OrientationSensorListener(handler);sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {public void onClick(View view) {Log.d(TAG, "onClick: " + getResources().getConfiguration().orientation);}});}@Overrideprotected void onPause() {
sm.unregisterListener(listener);super.onPause();}@Overrideprotected void onResume() {
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);super.onResume();}
}
原文地址:
这篇关于Android根据重力感应选装方向,四个方向都支持的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!