gpt4 book ai didi

android - 获取手机方向,但将屏幕方向固定为纵向

转载 作者:IT老高 更新时间:2023-10-28 22:02:03 24 4
gpt4 key购买 nike

我想获取手机方向,但将屏幕方向保持为纵向。所以无论用户将手机切换为横向还是纵向, View 都保持不变,但我可以得到它是横向还是纵向。

将 Activity 设置为 android:screenOrientation="portrait" 将解决这两个问题,但我无法通过

检测手机方向
public void onConfigurationChanged(Configuration newConfig) {
switch (newConfig.orientation) {
case Configuration.ORIENTATION_PORTRAIT:
Toast.makeText(this, "Portrait", Toast.LENGTH_SHORT).show();
break;
case Configuration.ORIENTATION_LANDSCAPE:
Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}

有人知道如何解决这个问题吗?

最佳答案

这是一个用于轻松管理屏幕方向更改的多功能类:

public class OrientationManager extends OrientationEventListener {

public enum ScreenOrientation {
REVERSED_LANDSCAPE, LANDSCAPE, PORTRAIT, REVERSED_PORTRAIT
}

public ScreenOrientation screenOrientation;
private OrientationListener listener;

public OrientationManager(Context context, int rate, OrientationListener listener) {
super(context, rate);
setListener(listener);
}

public OrientationManager(Context context, int rate) {
super(context, rate);
}

public OrientationManager(Context context) {
super(context);
}

@Override
public void onOrientationChanged(int orientation) {
if (orientation == -1){
return;
}
ScreenOrientation newOrientation;
if (orientation >= 60 && orientation <= 140){
newOrientation = ScreenOrientation.REVERSED_LANDSCAPE;
} else if (orientation >= 140 && orientation <= 220) {
newOrientation = ScreenOrientation.REVERSED_PORTRAIT;
} else if (orientation >= 220 && orientation <= 300) {
newOrientation = ScreenOrientation.LANDSCAPE;
} else {
newOrientation = ScreenOrientation.PORTRAIT;
}
if(newOrientation != screenOrientation){
screenOrientation = newOrientation;
if(listener != null){
listener.onOrientationChange(screenOrientation);
}
}
}

public void setListener(OrientationListener listener){
this.listener = listener;
}

public ScreenOrientation getScreenOrientation(){
return screenOrientation;
}

public interface OrientationListener {

public void onOrientationChange(ScreenOrientation screenOrientation);
}
}

这样更简单、可重复使用,您还可以获得 REVERSE_LANDSCAPE 和 REVERSE_PORTRAIT 方向。

您必须实现 OrientationListener 才能仅在方向更改发生时收到通知。

不要忘记调用orientationManager.enable()开始方向跟踪,然后调用orientationManager.disable()(这两个方法继承自OrientationEventListener类)

更新:用例示例

MyFragment extends Fragment implements OrientationListener {

...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

orientationManager = new OrientationManager(getActivity(), SensorManager.SENSOR_DELAY_NORMAL, this);
orientationManager.enable();
}

@Override
public void onOrientationChange(ScreenOrientation screenOrientation) {
switch(screenOrientation){
case PORTRAIT:
case REVERSED_PORTRAIT:
MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case REVERSED_LANDSCAPE:
MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case LANDSCAPE:
MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}
}

关于android - 获取手机方向,但将屏幕方向固定为纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9021890/

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