获取手机的方向,但修复屏幕方向为纵向 [英] Get phone orientation but fix screen orientation to portrait

查看:242
本文介绍了获取手机的方向,但修复屏幕方向为纵向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拿到手机的方向,但保留在屏幕方向为纵向。 所以无论用户打开手机横向或纵向的看法保持不变,但我可以得到它是否转向横向或纵向。

I want to get the phone orientation but keep the screen orientation to portrait. So no matter the user turns the phone to landscape or portrait, the view stays the same, but I can get whether it is turned to landscape or portrait.

活动设置为安卓screenOrientation =画像的将修复两个,但我也无法通过

Setting the activity to android:screenOrientation="portrait" will fix both but I wouldn't be able to detect the phone orientation via

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;
    }
}

有没有人一个想法如何解决这个问题?

Has anyone an idea how to fix that?

推荐答案

你能不能满足使用加速度计您的要求?如果是的话,也许是这样的(未测试)段将满足您的要求。

Could you satisfy your requirement with the accelerometer? If so, perhaps something like this (untested) fragment would suit your purposes.

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);

这篇关于获取手机的方向,但修复屏幕方向为纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆