Beispiel #1
0
    /**
     * Set the size of this window in absolute pixels. The window will expand and shrink around the
     * top-left corner, unless you've set a different anchor point with {@link
     * #setAnchorPoint(float, float)}.
     *
     * <p>Changes will not applied until you {@link #commit()}.
     *
     * @param width
     * @param height
     * @param skip Don't call {@link #setPosition(int, int)} to avoid stack overflow.
     * @return The same Editor, useful for method chaining.
     */
    private Editor setSize(int width, int height, boolean skip) {
      if (mParams != null) {
        if (anchorX < 0 || anchorX > 1 || anchorY < 0 || anchorY > 1) {
          throw new IllegalStateException("Anchor point must be between 0 and 1, inclusive.");
        }

        int lastWidth = mParams.width;
        int lastHeight = mParams.height;

        if (width != UNCHANGED) {
          mParams.width = width;
        }
        if (height != UNCHANGED) {
          mParams.height = height;
        }

        // set max width/height
        int maxWidth = mParams.maxWidth;
        int maxHeight = mParams.maxHeight;

        if (Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE)) {
          maxWidth = (int) Math.min(maxWidth, displayWidth);
          maxHeight = (int) Math.min(maxHeight, displayHeight);
        }

        // keep window between min and max
        mParams.width = Math.min(Math.max(mParams.width, mParams.minWidth), maxWidth);
        mParams.height = Math.min(Math.max(mParams.height, mParams.minHeight), maxHeight);

        // keep window in aspect ratio
        if (Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_ASPECT_RATIO_ENABLE)) {
          int ratioWidth = (int) (mParams.height * touchInfo.ratio);
          int ratioHeight = (int) (mParams.width / touchInfo.ratio);
          if (ratioHeight >= mParams.minHeight && ratioHeight <= mParams.maxHeight) {
            // width good adjust height
            mParams.height = ratioHeight;
          } else {
            // height good adjust width
            mParams.width = ratioWidth;
          }
        }

        if (!skip) {
          // set position based on anchor point
          setPosition(
              (int) (mParams.x + lastWidth * anchorX), (int) (mParams.y + lastHeight * anchorY));
        }
      }

      return this;
    }