获取电话方向,但将屏幕方向修改为纵向

Arm*_*man 28 android screen-orientation orientation-changes

我希望获得手机方向,但保持屏幕方向为纵向.因此,无论用户将手机转为横向还是纵向,视图都保持不变,但我可以看到它是转向横向还是纵向.

将活动设置为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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

edr*_*ian 32

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这样更简单,可重用,您还可以获得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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案,但请注意,这仅在具有纵向模式作为默认模式的智能手机上提供正确的结果.在平板电脑上(默认使用横向),在横向方向的情况下,方向的值为0,也就是说,在应用逻辑之前,必须在方向值上添加270.有关默认的设备方向,请参阅http://stackoverflow.com/questions/4553650/how-to-check-device-natural-default-orientation-on-android-ie-get-landscape (4认同)

Phi*_*ons 20

你能用加速度计满足你的要求吗?如果是这样的话,也许这样的(未经测试的)片段会适合您的目的.

SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(new SensorEventListener() {
            int orientation=-1;;

            @Override
            public void onSensorChanged(SensorEvent event) {
                if (event.values[1]<6.5 && event.values[1]>-6.5) {
                    if (orientation!=1) {
                        Log.d("Sensor", "Landscape");
                    }
                    orientation=1;
                } else {
                    if (orientation!=0) {
                        Log.d("Sensor", "Portrait");
                    }
                    orientation=0;
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub

            }
        }, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
Run Code Online (Sandbox Code Playgroud)

  • 如果设备平放在桌子上,没有多大价值. (4认同)
  • 谁知道如何检测reverse_landscape? (2认同)