IT TIP

Android 기기의 CURRENT 방향 (ActivityInfo.SCREEN_ORIENTATION_ *)은 어떻게 얻습니까?

itqueen 2020. 12. 28. 22:22
반응형

Android 기기의 CURRENT 방향 (ActivityInfo.SCREEN_ORIENTATION_ *)은 어떻게 얻습니까?


I는 장치의 상세한 방향, 바람직하게는 한 알하고자 SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAITActivityInfo또는 동등한.

여기에 StackOverflow에 대한 일부 답변이 포함되어 있습니다.

getWindowManager().getDefaultDisplay().getRotation()

그러나 이것은 실제로 장치가 세로 또는 가로 모드인지 여부를 알려주지 않고 자연스러운 위치를 기준으로 어떻게 회전했는지 만 알려주지 않습니다. 처음에는 가로 또는 세로가 될 수 있습니다.

getResources().getConfiguration().orientation

반환은 다음 세 가지 중 하나를 ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, ORIENTATION_SQUARE, 다음 정말 (이 거꾸로이 나있는 측면의가에 켜져 있는지) 휴대 전화가 켜져있는 방법을 말해주지 않는다.

후자를 함께 사용하여 DisplayMetrics장치의 자연스러운 방향을 찾을 수 있다는 것을 알고 있지만 더 좋은 방법은 없습니까?


결국 다음 솔루션을 사용했습니다.

private int getScreenOrientation() {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0
            || rotation == Surface.ROTATION_180) && height > width ||
        (rotation == Surface.ROTATION_90
            || rotation == Surface.ROTATION_270) && width > height) {
        switch(rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to " +
                        "portrait.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;              
        }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else {
        switch(rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_180:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            case Surface.ROTATION_270:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to " +
                        "landscape.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;              
        }
    }

    return orientation;
}

참고 : 일부 사용자 (아래 설명의 Geltrude 및 holtaf)는 자연스러운 방향의 회전 방향이 표준화되지 않았기 때문에이 솔루션이 모든 장치에서 작동하지 않을 것이라고 지적했습니다.


간단한 접근 방식은

getResources().getConfiguration().orientation

1은 Potrait 용이고 2는 Landscape 용입니다.


public static int getScreenOrientation(Activity activity) {
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int orientation = activity.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
          } else {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
          }
        }
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
          } else {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
          }
        }
        return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
      }

getResources().getConfiguration().orientation현재 사용중인 방향을 아는 표준 방법입니다. 그러나 그것이 당신의 필요를 충족시키지 못한다면 아마도 센서를 사용하여 각도로 계산할 수 있습니다. 이것이것을 읽으십시오


I think your problem is that you can detect landscape and portrait but not reverse landscape and reverse protrait as they are not supported in older versions. To detect what you can do is that you can use both oreintation and rotation. I am giving you an idea it may be useful for you.

try this i think it may solve your problem.

            int orientation = getResources().getConfiguration().orientation;
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            int actual_orientation = -1;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE
            &&  (rotation == Surface.ROTATION_0 
            ||  rotation == Surface.ROTATION_90)){
                orientation = Configuration.ORIENTATION_LANDSCAPE;
            } else if (orientation == Configuration.ORIENTATION_PORTRAIT
                  &&  (rotation == Surface.ROTATION_0 
                   ||  rotation == Surface.ROTATION_90)) {
                orientation = Configuration.ORIENTATION_PORTRAIT;
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE
                  &&  (rotation == Surface.ROTATION_180 
                   ||  rotation == Surface.ROTATION_270)){
                orientation = //any constant for reverse landscape orientation;
            } else {
                if (orientation == Configuration.ORIENTATION_PORTRAIT
                        &&  (rotation == Surface.ROTATION_180 
                         ||  rotation == Surface.ROTATION_270)){
                      orientation = //any constant for reverse portrait orientation;
                }
            }

I ended up using Zoltán's answer above, which works great, except when I tried it on a tablet (a Samsung P6210 Galaxy Tab 7.0 Plus). In portrait mode, it returned SCREEN_ORIENTATION_REVERSE_PORTRAIT. So in the else statement (if natural orientation is landscape) I swapped the cases for ROTATION_90 and ROTATION_270, and everything seems to work fine. (I don't have enough reputation to post this as a comment to Zoltán's answer.)


You could do it in a very simple way: get the screen widhtand height. screen width will be always higher when device is in landscape orientation.

Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth(); 
    int height = display.getHeight();
    Toast.makeText(getApplicationContext(), "" + width + "," + height,
            Toast.LENGTH_SHORT).show();
    if (width > height) {
        Toast.makeText(getApplicationContext(), "LandScape",
                Toast.LENGTH_SHORT).show();
    }

Does this solve your problem?

public static int getscrOrientation(Activity act)
{
    Display getOrient = act.getWindowManager()
            .getDefaultDisplay();

    int orientation = getOrient.getOrientation();

    // Sometimes you may get undefined orientation Value is 0
    // simple logic solves the problem compare the screen
    // X,Y Co-ordinates and determine the Orientation in such cases
    if (orientation == Configuration.ORIENTATION_UNDEFINED) {

        Configuration config = act.getResources().getConfiguration();
        orientation = config.orientation;

        if (orientation == Configuration.ORIENTATION_UNDEFINED) {
            // if height and widht of screen are equal then
            // it is square orientation
            if (getOrient.getWidth() == getOrient.getHeight()) {
                orientation = Configuration.ORIENTATION_SQUARE;
            } else { // if widht is less than height than it is portrait
                if (getOrient.getWidth() < getOrient.getHeight()) {
                    orientation = Configuration.ORIENTATION_PORTRAIT;
                } else { // if it is not any of the above it will defineitly
                            // be landscape
                    orientation = Configuration.ORIENTATION_LANDSCAPE;
                }
            }
        }
    }
    return orientation; // return value 1 is portrait and 2 is Landscape
                        // Mode
}

ReferenceURL : https://stackoverflow.com/questions/10380989/how-do-i-get-the-current-orientation-activityinfo-screen-orientation-of-an-a

반응형