// Center as much as possible in one or both axis.  Centering is
  // defined as follows:  if the image is scaled down below the
  // view's dimensions then center it (literally).  If the image
  // is scaled larger than the view and is translated out of view
  // then translate it back into view (i.e. eliminate black bars).
  protected void center(boolean horizontal, boolean vertical) {
    if (mBitmapDisplayed.getBitmap() == null) {
      return;
    }

    Matrix m = getImageViewMatrix();

    RectF rect =
        new RectF(
            0,
            0,
            mBitmapDisplayed.getBitmap().getWidth(),
            mBitmapDisplayed.getBitmap().getHeight());

    m.mapRect(rect);

    float height = rect.height();
    float width = rect.width();

    float deltaX = 0, deltaY = 0;

    if (vertical) {
      int viewHeight = getHeight();
      if (height < viewHeight) {
        deltaY = (viewHeight - height) / 2 - rect.top;
      } else if (rect.top > 0) {
        deltaY = -rect.top;
      } else if (rect.bottom < viewHeight) {
        deltaY = getHeight() - rect.bottom;
      }
    }

    if (horizontal) {
      int viewWidth = getWidth();
      if (width < viewWidth) {
        deltaX = (viewWidth - width) / 2 - rect.left;
      } else if (rect.left > 0) {
        deltaX = -rect.left;
      } else if (rect.right < viewWidth) {
        deltaX = viewWidth - rect.right;
      }
    }

    postTranslate(deltaX, deltaY);
    setImageMatrix(getImageViewMatrix());
  }
 protected void postTranslateCenter(float dx, float dy) {
   super.postTranslate(dx, dy);
   center(true, true);
 }
 protected void panBy(float dx, float dy) {
   postTranslate(dx, dy);
   setImageMatrix(getImageViewMatrix());
 }