android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现
来源:互联网 发布:广州市云润大数据 编辑:程序博客网 时间:2024/06/05 18:57
android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现
前言:根据屏幕的旋转自动实现应用界面旋转,横屏、竖屏、反向横屏、反向竖屏。
布局文件
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_ori" android:text="方向" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:src="@mipmap/map" android:layout_width="wrap_content" android:layout_height="wrap_content"/></android.support.constraint.ConstraintLayout>
监听器类:
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; } @Override public void onSensorChanged(SensorEvent sensorEvent) { float[] values = sensorEvent.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(); } } @Override public void onAccuracyChanged(Sensor sensor, int i) { }}
Activity类
public class OrientationActivity extends AppCompatActivity { private Button button; private Handler handler; private OrientationSensorListener listener; private SensorManager sm; private Sensor sensor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_orientation); Log.i("mylog:", "onCreate()"); handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 888) { int orientation = msg.arg1; if (orientation > 45 && orientation < 135) { OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); Log.d("mylog", "横屏翻转: "); } else if (orientation > 135 && orientation < 225) { OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); Log.d("mylog", "竖屏翻转: "); } else if (orientation > 225 && orientation < 315) { OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); Log.d("mylog", "横屏: "); } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) { OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Log.d("mylog", "竖屏: "); } } super.handleMessage(msg); } }; 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.btn_ori); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.i("mylog:", getResources().getConfiguration().orientation +""); } }); } @Override protected void onPause() { super.onPause(); sm.unregisterListener(listener); } @Override protected void onResume() { super.onResume(); sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI); }}
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ndt.testdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".com.OrientationActivity" android:configChanges="keyboardHidden|screenSize|orientation"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application></manifest>
阅读全文
0 0
- android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现
- Android实现屏幕旋转
- 字符串,单链表的反向实现
- java反向列表的实现
- Android官方DataBinding(十二):双向绑定之反向绑定的InverseBindingMethods改造和实现
- JAVA用TCP 实现反向连接屏幕监视
- JAVA用TCP 实现反向连接屏幕监视
- JAVA实现反向连接_屏幕监视 (TCP)
- JAVA实现反向连接_____屏幕监视 (TCP)
- MID默认横屏,打开竖屏APK方向反向的问题
- MID默认横屏,打开竖屏APK方向反向的问题
- 实现WinCE的转屏(屏幕旋转):转自互联网
- Android实现屏幕旋转方法
- Android实现屏幕旋转方法
- Android实现屏幕旋转方法
- Mysql group_concat的反向应用实现(Mysql列转行)
- Mysql group_concat的反向应用实现(Mysql列转行)
- Mysql group_concat的反向应用实现(Mysql列转行)
- VMware虚拟机中ubuntu 16.04.3 安装、root密码设置、Xshell无法连接、ubuntu重启关机命令汇总
- 大佬的JAVA游戏服务开发经验
- redis安装
- Oracle(五)Pl/sql语法,record,cursor,Pl/sql控制语句,Pl/sql编写分页过程
- ONVIF协议详解
- android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现
- postgresql查看数据库,表,索引,表空间以及大小
- spring boot 扫描不到自定义的Controller。。。
- 关于synchronized的理解记录
- RHEL7 配置FTP yum 源
- 前端chrome浏览器调试技巧
- easyui 删除etree 文件夹节点下最后一个文件,文件夹图标不变
- Android 图片压缩
- UBIFS文件系统介绍