Esempio n. 1
0
  protected void updateViewSize(final boolean landscape, int width, int height, LMode mode) {

    RectBox d = getScreenDimension();

    this.maxWidth = (int) d.getWidth();
    this.maxHeight = (int) d.getHeight();

    if (landscape && (d.getWidth() > d.getHeight())) {
      maxWidth = (int) d.getWidth();
      maxHeight = (int) d.getHeight();
    } else if (landscape && (d.getWidth() < d.getHeight())) {
      maxHeight = (int) d.getWidth();
      maxWidth = (int) d.getHeight();
    } else if (!landscape && (d.getWidth() < d.getHeight())) {
      maxWidth = (int) d.getWidth();
      maxHeight = (int) d.getHeight();
    } else if (!landscape && (d.getWidth() > d.getHeight())) {
      maxHeight = (int) d.getWidth();
      maxWidth = (int) d.getHeight();
    }

    if (mode != LMode.Max) {
      if (landscape) {
        this.zoomWidth = width;
        this.zoomHeight = height;
      } else {
        this.zoomWidth = height;
        this.zoomHeight = width;
      }
    } else {
      if (landscape) {
        this.zoomWidth = maxWidth >= width ? width : maxWidth;
        this.zoomHeight = maxHeight >= height ? height : maxHeight;
      } else {
        this.zoomWidth = maxWidth >= height ? height : maxWidth;
        this.zoomHeight = maxHeight >= width ? width : maxHeight;
      }
    }

    if (mode == LMode.Fill) {

      LSystem.setScaleWidth(((float) maxWidth) / zoomWidth);
      LSystem.setScaleHeight(((float) maxHeight) / zoomHeight);

    } else if (mode == LMode.FitFill) {

      RectBox res = AndroidGraphicsUtils.fitLimitSize(zoomWidth, zoomHeight, maxWidth, maxHeight);
      maxWidth = res.width;
      maxHeight = res.height;
      LSystem.setScaleWidth(((float) maxWidth) / zoomWidth);
      LSystem.setScaleHeight(((float) maxHeight) / zoomHeight);

    } else if (mode == LMode.Ratio) {

      maxWidth = View.MeasureSpec.getSize(maxWidth);
      maxHeight = View.MeasureSpec.getSize(maxHeight);

      float userAspect = (float) zoomWidth / (float) zoomHeight;
      float realAspect = (float) maxWidth / (float) maxHeight;

      if (realAspect < userAspect) {
        maxHeight = Math.round(maxWidth / userAspect);
      } else {
        maxWidth = Math.round(maxHeight * userAspect);
      }

      LSystem.setScaleWidth(((float) maxWidth) / zoomWidth);
      LSystem.setScaleHeight(((float) maxHeight) / zoomHeight);

    } else if (mode == LMode.MaxRatio) {

      maxWidth = View.MeasureSpec.getSize(maxWidth);
      maxHeight = View.MeasureSpec.getSize(maxHeight);

      float userAspect = (float) zoomWidth / (float) zoomHeight;
      float realAspect = (float) maxWidth / (float) maxHeight;

      if ((realAspect < 1 && userAspect > 1) || (realAspect > 1 && userAspect < 1)) {
        userAspect = (float) zoomHeight / (float) zoomWidth;
      }

      if (realAspect < userAspect) {
        maxHeight = Math.round(maxWidth / userAspect);
      } else {
        maxWidth = Math.round(maxHeight * userAspect);
      }

      LSystem.setScaleWidth(((float) maxWidth) / zoomWidth);
      LSystem.setScaleHeight(((float) maxHeight) / zoomHeight);

    } else {

      LSystem.setScaleWidth(1f);
      LSystem.setScaleHeight(1f);
    }

    LSystem.setScaleWidth(((float) maxWidth) / zoomWidth);
    LSystem.setScaleHeight(((float) maxHeight) / zoomHeight);
    LSystem.viewSize.setSize(zoomWidth, zoomHeight);

    StringBuffer sbr = new StringBuffer();
    sbr.append("Mode:").append(mode);
    sbr.append("\nWidth:").append(zoomWidth).append(",Height:" + zoomHeight);
    sbr.append("\nMaxWidth:").append(maxWidth).append(",MaxHeight:" + maxHeight);
    Log.d("Android2DSize", sbr.toString());
  }