2013-10-22 34 views
2

我在檢測沒有硬件按鈕的設備上的屏幕大小時遇到​​問題。屏幕大小和硬件菜單(適用的SDK)

例如在肖像工作屏幕尺寸800x1232,但橫向1280x752。

在我的應用程序按鈕位於邊界,我需要確切的大小的屏幕,而界面改變方向。

如何在willRotateToInterfaceOrientation中獲得未來的屏幕尺寸:duration:?

如何檢測設備是否有硬件菜單按鈕?並且如果按鈕不是硬件,將會很好地獲得菜單欄的大小。

謝謝

回答

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;