7 package com.google.appinventor.components.runtime.util;
9 import android.content.Context;
10 import android.graphics.Point;
11 import android.util.DisplayMetrics;
12 import android.util.Log;
13 import android.view.Display;
14 import android.view.WindowManager;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
25 private static final String LOG_TAG =
"ScreenDensityUtil";
44 DisplayMetrics dm = context.getResources().getDisplayMetrics();
46 Point rawDims =
new Point();
47 getRawScreenDim(context, rawDims);
49 int width = rawDims.x;
50 int height = rawDims.y;
52 int shortSize, longSize;
61 float aspect = ((float)longSize) / shortSize;
65 int newLongSize = (int)(newShortSize * aspect + 0.5f);
66 int newWidth, newHeight;
68 newWidth = newShortSize;
69 newHeight = newLongSize;
71 newWidth = newLongSize;
72 newHeight = newShortSize;
75 float sw = width/(float)newWidth;
76 float sh = height/(float)newHeight;
88 private static void getRawScreenDim(Context context, Point outSize) {
90 final DisplayMetrics metrics =
new DisplayMetrics();
91 final WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
92 Display display = wm.getDefaultDisplay();
97 display.getMetrics(metrics);
98 outSize.x = metrics.widthPixels;
99 outSize.y = metrics.heightPixels;
102 JellybeanUtil.getRealSize(display, outSize);
103 }
else if ( sdkLevel > SdkLevel.LEVEL_GINGERBREAD_MR1){
108 Method getRawH = Display.class.getMethod(
"getRawHeight");
109 Method getRawW = Display.class.getMethod(
"getRawWidth");
111 outSize.x = (Integer) getRawW.invoke(display);
112 outSize.y = (Integer) getRawH.invoke(display);
113 }
catch (IllegalArgumentException e) {
114 Log.e(LOG_TAG,
"Error reading raw screen size", e);
115 }
catch (IllegalAccessException e) {
116 Log.e(LOG_TAG,
"Error reading raw screen size", e);
117 }
catch (InvocationTargetException e) {
118 Log.e(LOG_TAG,
"Error reading raw screen size", e);
120 }
catch (NoSuchMethodException e) {
121 Log.e(LOG_TAG,
"Error reading raw screen size", e);
126 outSize.x = display.getWidth();
127 outSize.y = display.getHeight();