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);
    }
  }
Esempio n. 2
0
  @Test
  @org.robolectric.annotation.Config(
      sdk = {
        Build.VERSION_CODES.JELLY_BEAN_MR1,
        Build.VERSION_CODES.JELLY_BEAN_MR2,
        Build.VERSION_CODES.KITKAT,
        Build.VERSION_CODES.LOLLIPOP
      })
  public void shouldCreateMutableBitmapWithDisplayMetrics() throws Exception {
    final DisplayMetrics metrics = new DisplayMetrics();
    metrics.densityDpi = 1000;

    final Bitmap bitmap = Bitmap.createBitmap(metrics, 100, 100, Config.ARGB_8888);
    assertThat(bitmap.isMutable()).isTrue();
    assertThat(bitmap.getDensity()).isEqualTo(1000);
  }
  /**
   * 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();
  }