屏幕尺寸和硬件菜单(Apportable SDK)

时间:2013-10-22 09:08:40

标签: apportable

我在检测没有硬件按钮的设备上的屏幕尺寸时遇到问题。

例如,在纵向工作屏幕尺寸为800x1232,但在横向1280x752中。

在我位于边框的应用按钮中,当界面改变方向时,我需要精确的屏幕尺寸。

如何在willRotateToInterfaceOrientation获取未来的屏幕尺寸:持续时间:?

如何检测设备是否有硬件菜单按钮?如果按钮不是硬件,那么获取菜单栏的大小将是完美的。

由于

1 个答案:

答案 0 :(得分:1)

我使用Android SDK解决了这个问题。您可以查看如何将java与apportable here一起使用。

这是我的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;