尝试写播放器页面
...
第一步先搞清怎么做横竖屏切换
(1)强制竖屏显示;(2)强制横屏显示;(3)根据重力感应切换横竖屏
...
完成第一步,先搞清 ActivityInfo
ActivityInfo
/*保持横屏*/
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
/*保持竖屏*/
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT7
/*根据用户朝向*/
ActivityInfo.SCREEN_ORIENTATION_USER
/*不受重力影响*/
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
/*当手机底部朝上时,根据重力变换朝向,形成相对竖屏旋转180°的效果*/
ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
/*当手机底部朝上时,没有任何切换效果,保持上一个状态不变*/
ActivityInfo.SCREEN_ORIENTATION_SENSOR
/*横屏动态转换*/
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
/*竖屏动态转换*/
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
完成第一步,先搞清 横竖屏切换的生命周期
...
完成第一步,做横竖屏切换
<pre>
/**
* Created by alex on 2016/5/1.
*/
public class OrientationActivity extends AppCompatActivity {
private MyOrientationEventListener orientationEventListener;
private Button btAuto;
private Button bt2Land;
private Button bt2Port;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orientation);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
initView();
initOrientation();
}
private void initView() {
btAuto = (Button) findViewById(R.id.bt_auto);
if (btAuto != null) {
btAuto.setOnClickListener(new MyOnClickListener());
}
bt2Land = (Button) findViewById(R.id.bt_2_land);
if (bt2Land != null) {
bt2Land.setOnClickListener(new MyOnClickListener());
}
bt2Port = (Button) findViewById(R.id.bt_2_port);
if (bt2Port != null) {
bt2Port.setOnClickListener(new MyOnClickListener());
}
}
private final class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
if (R.id.bt_auto == v.getId()) {
/\*当手机底部朝上时,根据重力变换朝向,形成相对竖屏旋转180°的效果\*/
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
} else if (R.id.bt_2_land == v.getId()) {
/\*保持横屏\*/
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (R.id.bt_2_port == v.getId()) {
/\*保持竖屏\*/
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
private void initOrientation() {
/\*传感器\*/
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
orientationEventListener = new MyOrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL);
orientationEventListener.enable();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
KLog.e("newConfig = " + newConfig);
if (Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation) {
KLog.e("onConfigurationChanged :" + "landscape 横屏");
} else if (Configuration.ORIENTATION_PORTRAIT == newConfig.orientation) {
KLog.e("onConfigurationChanged :" + "portrait 竖屏");
}
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if (Surface.ROTATION_0 == rotation) {
/\*顶部在上\*/
} else if (Surface.ROTATION_90 == rotation) {
/\*左边在上\*/
} else if (Surface.ROTATION_180 == rotation) {
/\*底部在上\*/
} else if (Surface.ROTATION_270 == rotation) {
/\*右边在上\*/
}
setContentView(R.layout.activity_orientation);
initView();
}
private class MyOrientationEventListener extends OrientationEventListener {
public MyOrientationEventListener(Context context) {
super(context);
}
/\*\*
\* @param context
\* @param rate 比率 可以调节灵敏度 SensorManager.SENSOR_DELAY_FASTEST 等
\*/
public MyOrientationEventListener(Context context, int rate) {
super(context, rate);
}
@Override
public void onOrientationChanged(int orientation) {
int dxOrientation = 0;
if(orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
/\*手机平放时,检测不到有效的角度\*/
return;
}else if( orientation > 350 || orientation< 10 ) {
/\*顶部在上\*/
dxOrientation = 0;
} else if( orientation > 80 &&orientation < 100 ) {
/\*左边在上\*/
dxOrientation= 90;
} else if( orientation > 170 &&orientation < 190 ) {
/\*底部在上\*/
dxOrientation= 180;
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
} else if( orientation > 260 &&orientation < 280 ) {
/\*右边在上\*/
dxOrientation= 270;
} else {
return;
}
KLog.e("dxOrientation = "+dxOrientation);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
KLog.e("onDestroy ");
/\*必须disable 不然页面关闭后 它依然在工作 不断打印\*/
if (orientationEventListener != null) {
orientationEventListener.disable();
}
}
}
</pre>
先看效果, 下载apk文件