我遇到了一个方向sensorPortrait不起作用的问题,我尝试通过清单和活动本身启用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
但这似乎只是在正常人像模式下锁定,但是如果我尝试`fullSensor'
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
Run Code Online (Sandbox Code Playgroud)
根据文档
方向由设备方向传感器针对4个方向中的任何一个确定。这类似于“传感器”,除了它允许4种可能的屏幕方向中的任何一种,而与设备的正常运行方式无关(例如,某些设备通常不会使用反向肖像或反向风景,但是启用了这些功能)。在API级别9中添加。
正是这样,所有四个方向都是可能的。如果我也尝试
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
我能够获得反向肖像,这使我回到原来的问题,为什么不起作用sensorPortrait?看起来它与文档“ fullSensor”中的这一行有关
不管设备通常会做什么(例如,某些设备通常不会使用反向肖像或反向风景,但是启用了这些功能)
那么我如何启用它,这有可能吗?为什么fullSensor似乎要覆盖它而不是sensorPortrait?我似乎找不到任何提及方法。这个问题表明PhoneWindowManager对此负有责任。
理想的解决方案是否只是根据通过返回的值来创建OrientationEventListener()和手动调用?setRequestedOrientation()onOrientationChanged(int orientation)
作为一种解决方法,我创建了一个SensorPortraitActivity:
public class SensorPortraitActivity extends AppCompatActivity {
private static final int PORTRAIT = 0;
private static final int REVERSE_PORTRAIT = 180;
private static final int OFFSET = 45;
private static final int UNKNOWN = -1;
// account for 0 = 360 (eg. -1 = 359)
private static final int PORTRAIT_START = PORTRAIT - OFFSET + 360;
private static final int PORTRAIT_END = PORTRAIT + OFFSET;
private static final int REVERSE_PORTRAIT_START = REVERSE_PORTRAIT - OFFSET;
private static final int REVERSE_PORTRAIT_END = REVERSE_PORTRAIT + OFFSET;
private OrientationChangeListener mListener;
private OrientationEventListener mOrientationListener;
private CurrentOrientation mCurrentOrientation;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mOrientationListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int i) {
orientationChanged(i);
}
};
}
@Override
protected void onResume() {
super.onResume();
mOrientationListener.enable();
}
@Override
protected void onPause() {
super.onPause();
mOrientationListener.disable();
}
//optional
public void setOrientationChangeListener(OrientationChangeListener listener){
mListener = listener;
}
private void orientationChanged(int degrees) {
if (degrees != UNKNOWN){
if (degrees >= PORTRAIT_START || degrees <= PORTRAIT_END){
if (mCurrentOrientation != CurrentOrientation.PORTRAIT){
mCurrentOrientation = CurrentOrientation.PORTRAIT;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (mListener != null){
mListener.onPortrait();
}
}
} else if (degrees >= REVERSE_PORTRAIT_START && degrees <= REVERSE_PORTRAIT_END){
if (mCurrentOrientation != CurrentOrientation.REVERSE_PORTRAIT){
mCurrentOrientation = CurrentOrientation.REVERSE_PORTRAIT;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
if (mListener != null) {
mListener.onReversePortrait();
}
}
}
}
}
interface OrientationChangeListener {
void onPortrait();
void onReversePortrait();
}
enum CurrentOrientation {
PORTRAIT, REVERSE_PORTRAIT
}
}
Run Code Online (Sandbox Code Playgroud)
尽管对于像这样简单的事情来说似乎有些矫kill过正。
简单使用 extend SensorPortraitActivity
public class ExampleActivity extends SensorPortraitActivity implements SensorPortraitView.OrientationChangeListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set listener if you want callbacks
super.setOrientationChangeListener(this);
}
@Override
public void onPortrait() {
//portrait orientation
}
@Override
public void onReversePortrait() {
//reverse portrait orientation
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
795 次 |
| 最近记录: |