Exemplo n.º 1
0
  /**
   * Zoom to a specified rect IN CSS PIXELS.
   *
   * <p>While we usually use device pixels, @zoomToRect must be specified in CSS pixels.
   */
  private boolean animatedZoomTo(RectF zoomToRect) {
    setState(PanZoomState.ANIMATED_ZOOM);
    final float startZoom = getMetrics().zoomFactor;

    RectF viewport = getMetrics().getViewport();
    // 1. adjust the aspect ratio of zoomToRect to match that of the current viewport,
    // enlarging as necessary (if it gets too big, it will get shrunk in the next step).
    // while enlarging make sure we enlarge equally on both sides to keep the target rect
    // centered.
    float targetRatio = viewport.width() / viewport.height();
    float rectRatio = zoomToRect.width() / zoomToRect.height();
    if (FloatUtils.fuzzyEquals(targetRatio, rectRatio)) {
      // all good, do nothing
    } else if (targetRatio < rectRatio) {
      // need to increase zoomToRect height
      float newHeight = zoomToRect.width() / targetRatio;
      zoomToRect.top -= (newHeight - zoomToRect.height()) / 2;
      zoomToRect.bottom = zoomToRect.top + newHeight;
    } else { // targetRatio > rectRatio) {
      // need to increase zoomToRect width
      float newWidth = targetRatio * zoomToRect.height();
      zoomToRect.left -= (newWidth - zoomToRect.width()) / 2;
      zoomToRect.right = zoomToRect.left + newWidth;
    }

    float finalZoom = viewport.width() / zoomToRect.width();

    ImmutableViewportMetrics finalMetrics = getMetrics();
    finalMetrics =
        finalMetrics.setViewportOrigin(
            zoomToRect.left * finalMetrics.zoomFactor, zoomToRect.top * finalMetrics.zoomFactor);
    finalMetrics = finalMetrics.scaleTo(finalZoom, new PointF(0.0f, 0.0f));

    // 2. now run getValidViewportMetrics on it, so that the target viewport is
    // clamped down to prevent overscroll, over-zoom, and other bad conditions.
    finalMetrics = getValidViewportMetrics(finalMetrics);

    bounce(finalMetrics);
    return true;
  }