一.背景
做一个播放器,然后像爱奇艺那样
1.自由旋转屏幕
2.点击最大化不会立马随着传感器变化(我的解决方案就是当最大化的时候监听又没有转动过90度,如果有就让他随着传感器变化,如果没有就不随着传感器变化)
public class MainActivity extends AppCompatActivity {
private boolean fullScreenFlag = false;
private OrientationEventListener mOrientationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btRotate = (Button) findViewById(R.id.bt_rotate);
btRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!fullScreenFlag){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
mOrientationListener.enable();
}else{
System.out.println("xcqw 去竖屏");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
mOrientationListener.disable();
}
}
});
mOrientationListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int i) {
System.out.println("xcqw angle"+i);
if(i>90 && i<270){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
};
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
mOrientationListener.disable();
}
fullScreenFlag =!fullScreenFlag;
super.onConfigurationChanged(newConfig);
}
}
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
注意:
实际使用过程中,发现华为P9在
1.
mOrientationListener.enable()的时候回去向用户申请权限access fitness data ,注意!!!只有华为p9
所有只要开始mOrientationListenser.enable()就行,后面通过
ActivityInfo.SCREEN_ORIENTATION_SENSOR 和
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
控制是否可以监听到morientationListenner.
2.就是横屏(A)如果进入一个竖屏页面(B)如果要保持B的竖屏模式可以通过设置android:orientation来指定,然后通过orientationListenser监测让
ActivityInfo.SCREEN_ORIENTATION_SENSOR 就可以自动旋转屏