Code example for Matrix

Methods: invert, mapVectors, postTranslate

0
  public boolean translate(float dx, float dy) {
    Log.e("Matrix", "Panning");
	  if (imageToScreenMatrix == null) {
      return true; 
    } 
    imageToScreenMatrix.postTranslate(dx, dy);
    return checkImageOnScreen(); 
  } 
 
  public float[] getScreenCenterCoordinates(float[] result) {
    if (screenView == null) {
      return null; 
    } 
    if (result == null) {
      result = new float[2];
    } 
    result[0] = screenView.getWidth() / 2f;
    result[1] = screenView.getHeight() / 2f;
    return result;
  } 
 
  public float[] getImageCenterCoordinates(float[] result) {
    if (imageWidth <= 0 || imageHeight <= 0) {
      return null; 
    } 
    if (result == null) {
      result = new float[2];
    } 
    result[0] = imageWidth / 2f;
    result[1] = imageHeight / 2f;
    return result;
  } 
 
  public static float[] convertImageToScreenCoordinates(float[] imageCoords) {
    if (imageToScreenMatrix == null) {
      return null; 
    } 
    imageToScreenMatrix.mapPoints(imageCoords);
    return imageCoords;
  } 
 
  public static float[] convertScreenToImageCoordinates(float[] screenCoords) {
    if (imageToScreenMatrix == null) {
      return null; 
    } 
    imageToScreenMatrix.invert(screenToImageMatrix);
    screenToImageMatrix.mapPoints(screenCoords);
    return screenCoords;
  } 
 
  private void initMatrix() { 
    if (screenView == null || imageWidth <= 0 || imageHeight <= 0) {
      imageToScreenMatrix = null;
      return; 
    } 
    // First rotate image keeping upper left at (0, 0) 
    imageToScreenMatrix = new Matrix();
    if (imageOrientation != 0) {
      imageToScreenMatrix.setRotate(imageOrientation);
      int translateX = 0;
      int translateY = 0;
      if (imageOrientation == 90) {
        translateX = imageHeight;
      } else if (imageOrientation == 180) {
        translateX = imageWidth;
        translateY = imageHeight;
      } else if (imageOrientation == 270) {
        translateY = imageWidth;
      } 
      imageToScreenMatrix.postTranslate(translateX, translateY);
    } 
    // Find rotated image center coordinates 
    float[] imageCenter = getImageCenterCoordinates(null);
    imageToScreenMatrix.mapPoints(imageCenter);
    // Translate imageToScreenMatrix so that image and screen centers match 
    float xd = imageCenter[0] - (screenView.getWidth() / 2f);
    float yd = imageCenter[1] - (screenView.getHeight() / 2f);
    imageToScreenMatrix.postTranslate(xd, yd);
    zoomLevel = 1f;
 
    screenToImageMatrix = new Matrix();
  } 
 
  /** 
   * Returns 'false' if image was "forced" to stay on screen 
   */ 
  private boolean checkImageOnScreen() { 
    float[] screenCenter = getScreenCenterCoordinates(null);
    convertScreenToImageCoordinates(screenCenter);
 
    float dx = 0f;
    float dy = 0f;
    if (screenCenter[0] < 0) {
      dx = screenCenter[0];
    } else if (imageWidth < screenCenter[0]) {
      dx = screenCenter[0] - imageWidth;
    } 
    if (screenCenter[1] < 0) {
      dy = screenCenter[1];
    } else if (imageHeight < screenCenter[1]) {
      dy = screenCenter[1] - imageHeight;
    } 
 
    if (dx != 0f || dy != 0f) {
      // dx and dy are in image coordinates, map to screen (w/o translation) 
      float[] screenDiff = new float[] { dx, dy };
      imageToScreenMatrix.mapVectors(screenDiff);
      // Now adjust translation to keep map on screen 
      imageToScreenMatrix.postTranslate(screenDiff[0], screenDiff[1]);
      return false; 
    } 
    return true; 
  } 
}