private void getMetricsWithSize( DisplayMetrics outMetrics, CompatibilityInfo compatInfo, Configuration configuration, int width, int height) { outMetrics.densityDpi = outMetrics.noncompatDensityDpi = logicalDensityDpi; outMetrics.density = outMetrics.noncompatDensity = logicalDensityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE; outMetrics.scaledDensity = outMetrics.noncompatScaledDensity = outMetrics.density; outMetrics.xdpi = outMetrics.noncompatXdpi = physicalXDpi; outMetrics.ydpi = outMetrics.noncompatYdpi = physicalYDpi; width = (configuration != null && configuration.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) ? (int) ((configuration.screenWidthDp * outMetrics.density) + 0.5f) : width; height = (configuration != null && configuration.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) ? (int) ((configuration.screenHeightDp * outMetrics.density) + 0.5f) : height; outMetrics.noncompatWidthPixels = outMetrics.widthPixels = width; outMetrics.noncompatHeightPixels = outMetrics.heightPixels = height; if (!compatInfo.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) { compatInfo.applyToDisplayMetrics(outMetrics); } }
@Implementation public DisplayMetrics getDisplayMetrics() { if (displayMetrics == null) { if (display == null) { display = Robolectric.newInstanceOf(Display.class); } displayMetrics = new DisplayMetrics(); display.getMetrics(displayMetrics); } displayMetrics.density = this.density; return displayMetrics; }
/** * Initializes and acquires the scene, creating various Android objects such as context, inflater, * and parser. * * @param timeout the time to wait if another rendering is happening. * @return whether the scene was prepared * @see #acquire(long) * @see #release() */ public Result init(long timeout) { // acquire the lock. if the result is null, lock was just acquired, otherwise, return // the result. Result result = acquireLock(timeout); if (result != null) { return result; } // setup the ParserFactory ParserFactory.setParserFactory(mParams.getLayoutlibCallback().getParserFactory()); HardwareConfig hardwareConfig = mParams.getHardwareConfig(); // setup the display Metrics. DisplayMetrics metrics = new DisplayMetrics(); metrics.densityDpi = metrics.noncompatDensityDpi = hardwareConfig.getDensity().getDpiValue(); metrics.density = metrics.noncompatDensity = metrics.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT; metrics.scaledDensity = metrics.noncompatScaledDensity = metrics.density; metrics.widthPixels = metrics.noncompatWidthPixels = hardwareConfig.getScreenWidth(); metrics.heightPixels = metrics.noncompatHeightPixels = hardwareConfig.getScreenHeight(); metrics.xdpi = metrics.noncompatXdpi = hardwareConfig.getXdpi(); metrics.ydpi = metrics.noncompatYdpi = hardwareConfig.getYdpi(); RenderResources resources = mParams.getResources(); // build the context mContext = new BridgeContext( mParams.getProjectKey(), metrics, resources, mParams.getAssets(), mParams.getLayoutlibCallback(), getConfiguration(), mParams.getTargetSdkVersion(), mParams.isRtlSupported()); setUp(); return SUCCESS.createResult(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidProperties.init(this); // Set pixel density based on Android device DisplayMetrics metrics = new DisplayMetrics(); try { WindowManager winMgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE); winMgr.getDefaultDisplay().getMetrics(metrics); } catch (Exception e) { metrics.density = 1; } AndroidProperties.set("pixel_density", String.valueOf(metrics.density), this); loginWithFacebook(); }
@Test public void testPropertiesResetToDefault() { DisplayMetrics displayMetrics = new DisplayMetrics(); displayMetrics.density = 1.0f; DisplayMetricsHolder.setWindowDisplayMetrics(displayMetrics); LayoutShadowNode reactShadowNode = spy(new LayoutShadowNode()); ReactStylesDiffMap map = buildStyles( "width", 10.0, "height", 10.0, "left", 10.0, "top", 10.0, "flex", 1.0, "padding", 10.0, "marginLeft", 10.0, "borderTopWidth", 10.0, "flexDirection", "row", "alignSelf", "stretch", "alignItems", "center", "justifyContent", "space_between", "position", "absolute"); reactShadowNode.updateProperties(map); verify(reactShadowNode).setStyleWidth(10.f); verify(reactShadowNode).setStyleHeight(10.f); verify(reactShadowNode).setPositionLeft(10.f); verify(reactShadowNode).setPositionTop(10.f); verify(reactShadowNode).setFlex(1.0f); verify(reactShadowNode).setPadding(Spacing.ALL, 10.f); verify(reactShadowNode).setMargin(Spacing.LEFT, 10.f); verify(reactShadowNode).setBorder(Spacing.TOP, 10.f); verify(reactShadowNode).setFlexDirection(CSSFlexDirection.ROW); verify(reactShadowNode).setAlignSelf(CSSAlign.STRETCH); verify(reactShadowNode).setAlignItems(CSSAlign.CENTER); verify(reactShadowNode).setJustifyContent(CSSJustify.SPACE_BETWEEN); verify(reactShadowNode).setPositionType(CSSPositionType.ABSOLUTE); map = buildStyles( "width", null, "height", null, "left", null, "top", null, "flex", null, "padding", null, "marginLeft", null, "borderTopWidth", null, "flexDirection", null, "alignSelf", null, "alignItems", null, "justifyContent", null, "position", null); reset(reactShadowNode); reactShadowNode.updateProperties(map); verify(reactShadowNode).setStyleWidth(CSSConstants.UNDEFINED); verify(reactShadowNode).setStyleHeight(CSSConstants.UNDEFINED); verify(reactShadowNode).setPositionLeft(CSSConstants.UNDEFINED); verify(reactShadowNode).setPositionTop(CSSConstants.UNDEFINED); verify(reactShadowNode).setFlex(0.f); verify(reactShadowNode).setPadding(Spacing.ALL, CSSConstants.UNDEFINED); verify(reactShadowNode).setMargin(Spacing.LEFT, CSSConstants.UNDEFINED); verify(reactShadowNode).setBorder(Spacing.TOP, CSSConstants.UNDEFINED); verify(reactShadowNode).setFlexDirection(CSSFlexDirection.COLUMN); verify(reactShadowNode).setAlignSelf(CSSAlign.AUTO); verify(reactShadowNode).setAlignItems(CSSAlign.STRETCH); verify(reactShadowNode).setJustifyContent(CSSJustify.FLEX_START); verify(reactShadowNode).setPositionType(CSSPositionType.RELATIVE); }
public void setDensity(float density) { this.density = density; if (displayMetrics != null) { displayMetrics.density = density; } }