gpt4 book ai didi

Android - 'sensorPortrait' 方向不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:27 26 4
gpt4 key购买 nike

我遇到了方向 sensorPortrait 不起作用的问题,我尝试通过 list 和 Activity 本身启用

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

但这似乎只是锁定在正常的纵向模式下,但是如果我尝试“fullSensor”

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

根据文档

The orientation is determined by the device orientation sensor for any of the 4 orientations. This is similar to "sensor" except this allows any of the 4 possible screen orientations, regardless of what the device will normally do (for example, some devices won't normally use reverse portrait or reverse landscape, but this enables those). Added in API level 9.

它确实做到了,所有 4 个方向都是可能的。如果我也试试

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

我能够实现反向肖像,这让我回到了我最初的问题,为什么 sensorPortrait 不起作用?看起来它与“fullSensor”文档中的这一行有关

regardless of what the device will normally do (for example, some devices won't normally use reverse portrait or reverse landscape, but this enables those)

那么我该如何启用它,这可能吗?为什么 fullSensor 似乎覆盖了它而不是 sensorPortrait?我似乎找不到任何关于如何这样做的提及。这question建议 PhoneWindowManager 对此负责。

理想的解决方案只是创建一个 OrientationEventListener() 并根据通过 onOrientationChanged(int orientation)< 返回的值手动调用 setRequestedOrientation()/

最佳答案

作为解决方法,我创建了一个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
}
}

尽管对于像这样简单的事情来说,这似乎有些矫枉过正。

使用它很简单扩展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
}
}

关于Android - 'sensorPortrait' 方向不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com