Baseflow/PhotoView · error · IllegalArgumentException

Scale must be within the range of minScale and maxScale

Error message

Scale must be within the range of minScale and maxScale

What it means

PhotoView.setScale throws IllegalArgumentException when the requested scale falls outside the [minimumScale, maximumScale] range configured on the attacher. The bounds exist so zoom gestures and cleanup logic stay consistent; a scale outside them would leave the view in a state the internal cleanup()/checkMatrixBounds() code would immediately fight against. Defaults are typically 1.0 / 2.0 / 3.0 unless changed via setScaleLevels/setMinimumScale/setMaximumScale.

Source

Thrown at photoview/src/main/java/com/github/chrisbanes/photoview/PhotoViewAttacher.java:462

        mOnViewDragListener = listener;
    }

    public void setScale(float scale) {
        setScale(scale, false);
    }

    public void setScale(float scale, boolean animate) {
        setScale(scale,
            (mImageView.getRight()) / 2,
            (mImageView.getBottom()) / 2,
            animate);
    }

    public void setScale(float scale, float focalX, float focalY,
        boolean animate) {
        // Check to see if the scale is within bounds
        if (scale < mMinScale || scale > mMaxScale) {
            throw new IllegalArgumentException("Scale must be within the range of minScale and maxScale");
        }
        if (animate) {
            mImageView.post(new AnimatedZoomRunnable(getScale(), scale,
                focalX, focalY));
        } else {
            mSuppMatrix.setScale(scale, scale, focalX, focalY);
            checkAndDisplayMatrix();
        }
    }

    /**
     * Set the zoom interpolator
     *
     * @param interpolator the zoom interpolator
     */
    public void setZoomInterpolator(Interpolator interpolator) {
        mInterpolator = interpolator;
    }

View on GitHub (pinned to 565505d5cb)

Solutions

  1. Clamp the requested scale to the configured range: float s = Math.max(photoView.getMinimumScale(), Math.min(scale, photoView.getMaximumScale()));
  2. If the out-of-range scale is intentional, widen the range first with setScaleLevels(min, mid, max) (or setMaximumScale/setMinimumScale) so it satisfies Util.checkZoomLevels, then call setScale.
  3. Compute the desired zoom from drawable dimensions and derive min/max levels from it instead of hardcoding 3.0 as max.

Example fix

// before
float fitZoom = (float) viewWidth / (float) drawableWidth;
photoView.setScale(fitZoom); // throws if fitZoom > maxScale

// after
float clamped = Math.max(photoView.getMinimumScale(),
    Math.min(fitZoom, photoView.getMaximumScale()));
photoView.setScale(clamped);
Defensive patterns

Strategy: validation

Validate before calling

float safeScale = Math.max(photoView.getMinimumScale(),
    Math.min(desiredScale, photoView.getMaximumScale()));
photoView.setScale(safeScale, animate);

Prevention

When it happens

Trigger: Calling photoView.setScale(4f) while maxScale is still 3.0; calling setScale with a computed value (e.g. screenWidth / drawableWidth) that exceeds max or is below min; calling setScale(randomScale) where the random range was generated against stale or custom min/max levels; calling setScale before custom levels set via setScaleLevels() have been applied.

Common situations: Apps that compute a 'fit width' or 'fit height' zoom factor from bitmap dimensions (often >1 after setScaleLevels(1f, 1.75f)); the official sample's random-scale menu action; state restoration that reapplies a persisted scale after levels were narrowed; unit tests that assume default levels while the production code changed them.

Related errors


AI-assisted analysis of Baseflow/PhotoView@565505d5cb (2026-08-14). Data as JSON: /api/errors/1c7300f368ee32a1. Report an issue: GitHub.