2

I have an issue with detecting screen size on device which does not have hardware buttons.

For example in portrait working screen size 800x1232, but in landscape 1280x752.

In my app buttons located at the border and I need exact size of the screen while interface changing orientation.

How to get future screen size in willRotateToInterfaceOrientation:duration: ?

How to detect if device has hardware menu buttons or not? And will be perfect to get the size of menu bar, if buttons not hardware.

Thanks

4

1 回答 1

1

我已经使用 Android SDK 来解决这个问题。您可以在此处查看如何将 java 与 apportable 一起使用。

这是我的java代码

public Point screenSize(Activity activity) {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();

    int API_LEVEL =  android.os.Build.VERSION.SDK_INT;
    if (API_LEVEL >= 17) {
        disp.getRealMetrics(displayMetrics);
    } else {
        disp.getMetrics(displayMetrics);
    }

    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;

    return new Point(width, height); //String.format("%d,%d,%d,%d", portraitNavBarHeight, landscapeNavBarHeight, width, height);
}

public Point navigationBarHeights(Activity activity) {

    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int resourceIdLandscape = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
    int portraitNavBarHeight = 0;
    int landscapeNavBarHeight = 0;

    boolean hasPermanentMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey();
    if (resourceId > 0 && !hasPermanentMenuKey) {
        portraitNavBarHeight = resources.getDimensionPixelSize(resourceId);
    }

    if (resourceIdLandscape > 0 && !hasPermanentMenuKey) {
        landscapeNavBarHeight = resources.getDimensionPixelSize(resourceIdLandscape);
    }

    return new Point(portraitNavBarHeight, landscapeNavBarHeight); //String.format("%d,%d,%d,%d", portraitNavBarHeight, landscapeNavBarHeight, width, height);
}

和 Objective-C 部分

+ (void)initializeJava
{
[super initializeJava]; 

    // here your initialize code
    ...

    [KeyboardBridge registerInstanceMethod:@"screenSize"
                                  selector:@selector(screenSize:)
                               returnValue:[AndroidPoint className]
                                 arguments:[AndroidActivity className], nil];

    [KeyboardBridge registerInstanceMethod:@"navigationBarHeights"
                                  selector:@selector(navigationBarHeights:)
                               returnValue:[AndroidPoint className]
                                 arguments:[AndroidActivity className], nil];

}

- (CGRect)displayMetrics {
    AndroidPoint *screenSize = [self screenSize:[AndroidActivity currentActivity]];
    AndroidPoint *navigationBarHeights = [self navigationBarHeights:[AndroidActivity currentActivity]];

    return CGRectMake(navigationBarHeights.x, navigationBarHeights.y, screenSize.x, screenSize.y);
}

和最终结果

CGRect rect = [[[KeyboardBridge alloc] init] displayMetrics];

float portraitWidth;
float portraitHeight;
if(rect.size.width > rect.size.height) {
    portraitHeight = rect.size.width;
    portraitWidth = rect.size.height;
} else {
    portraitHeight = rect.size.height;
    portraitWidth = rect.size.width;
}

landscapeSize.width =  portraitHeight;
landscapeSize.height = portraitWidth - rect.origin.y;

portraitSize.width = portraitWidth;
portraitSize.height = portraitHeight - rect.origin.x;
于 2014-03-14T10:33:05.273 回答