コード例 #1
0
  /** Set the position and scale of an image in screen coordinates */
  protected boolean setPos(
      float centerX,
      float centerY,
      float scaleX,
      float scaleY,
      float angle,
      int flags,
      boolean isDraggedOrPinched) {

    // Reset the scale if the object is not scalable (otherwise the ws and
    // hs calculations below are not 100% accurate and will result in a
    // slight but noticable scaling of non-scalable objects
    if (!isScalable()) {
      scaleX = 1.0f;
      scaleY = 1.0f;
    }

    // Reset the angle if the drawable is not rotatable (for the same reason
    // as above)
    if (!isRotatable()) {
      this.setAngle(0.0f);
    }

    float ws = (width / 2) * scaleX, hs = (height / 2) * scaleY;
    float newMinX = centerX - ws,
        newMinY = centerY - hs,
        newMaxX = centerX + ws,
        newMaxY = centerY + hs;

    float scaleXChange = 1.0f, scaleYChange = 1.0f, angleChange = 0.0f;

    // Min and max values need to be set when item is dragged or scaled
    if ((flags & FLAG_FORCEXY) != 0
        || this.isDragable()
        || (flags & FLAG_FORCESCALE) != 0
        || this.isScalable()) {
      this.minX = newMinX;
      this.minY = newMinY;
      this.maxX = newMaxX;
      this.maxY = newMaxY;
    }

    if ((flags & FLAG_FORCEXY) != 0 || this.isDragable()) {

      this.centerX = centerX;
      this.centerY = centerY;
    }

    if ((flags & FLAG_FORCESCALE) != 0 || this.isScalable()) {
      scaleXChange = scaleX / this.scaleX;
      scaleYChange = scaleY / this.scaleY;
      this.setScale(scaleX, scaleY);
    }

    if ((flags & FLAG_FORCEROTATE) != 0 || this.isRotatable()) {
      angleChange = angle - this.angle;
      // this.angle = angle;
      this.setAngle(angle);
    }

    if (isDraggedOrPinched && this.hasSuperDrawable()) {
      PointF relativePosition = getRelativePositionToSuperobject();
      // we do not want to use setRelativePosition, because this will
      // overwrite the angle
      // this.setRelativePosition(relativePosition.x, relativePosition.y);
      this.setRelativePosition(relativePosition);
    }

    // Iterate through the subobjects and change their position
    for (MultiTouchDrawable subobject : subDrawables) {
      PointF absolutePosition = this.getAbsolutePositionOfSubobject(subobject);
      subobject.setPos(
          absolutePosition.x,
          absolutePosition.y,
          subobject.scaleX * scaleXChange,
          subobject.scaleY * scaleYChange,
          subobject.angle + angleChange,
          FLAG_FORCEXY,
          false);
    }

    return true;
  }