koral--/android-gif-drawable · error · IllegalArgumentException
y must be < height
Error message
y must be < height
What it means
GifDrawable.getPixel(x, y) validates coordinates against the current frame buffer dimensions. If y is greater than or equal to the GIF's height, an IllegalArgumentException('y must be < height') is thrown. The check is explicit because the reused Bitmap may be larger than the current GIF.
Solutions
- Check y against the drawable's intrinsic height before calling getPixel: if (y >= drawable.getIntrinsicHeight()) return/throw.
- Use drawable.getIntrinsicHeight() (or GifDrawable#getHeight) to compute coordinates dynamically instead of hardcoding.
- Catch IllegalArgumentException around getPixel as a last resort.
Example fix
// before
int color = gifDrawable.getPixel(10, 500);
// after
if (10 < gifDrawable.getIntrinsicWidth() && 500 < gifDrawable.getIntrinsicHeight()) {
int color = gifDrawable.getPixel(10, 500);
} Defensive patterns
Strategy: validation
Validate before calling
if (x >= 0 && y >= 0 && x < gifDrawable.getIntrinsicWidth() && y < gifDrawable.getIntrinsicHeight()) { gifDrawable.getPixel(x, y); } Type guard
boolean isWithinGifBounds(GifDrawable d, int x, int y) { return x >= 0 && y >= 0 && x < d.getIntrinsicWidth() && y < d.getIntrinsicHeight(); } Try / catch
try { int color = gifDrawable.getPixel(x, y); } catch (IllegalArgumentException e) { /* coordinate out of bounds — use fallback color */ } Prevention
- Always derive pixel coordinates from getIntrinsicWidth()/getIntrinsicHeight()
- Never hardcode coordinates across multiple GIF assets
- Remember the reused bitmap may be larger than the GIF
When it happens
Trigger: Calling getPixel(x, y) with y >= gifDrawable.getIntrinsicHeight() (the native handle's height), e.g. a hardcoded pixel coordinate applied to a differently sized GIF.
Common situations: Code that assumed a fixed GIF size (e.g. sampled pixel coordinates for color detection) reused across assets of varying dimensions; resizing/reusing drawables without updating pixel coordinates.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Bitmap is recycled
- Loop count of range <0, 65535>
- Speed factor is not positive
- Sample size out of range <1, 65535>
- Bitmap ia too small, size must be greater than or equal to…
AI-assisted analysis of koral--/android-gif-drawable@26ff795f78 (2026-09-10).
Data as JSON: /api/errors/95e6e524ab56f3b0.
Report an issue: GitHub.
Appendix: source
Thrown at android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifDrawable.java:759
}
/**
* Returns the {@link Color} at the specified location. Throws an exception
* if x or y are out of bounds (negative or >= to the width or height
* respectively). The returned color is a non-premultiplied ARGB value.
*
* @param x The x coordinate (0...width-1) of the pixel to return
* @param y The y coordinate (0...height-1) of the pixel to return
* @return The argb {@link Color} at the specified coordinate
* @throws IllegalArgumentException if x, y exceed the drawable's bounds
* @throws IllegalStateException if drawable is recycled
*/
public int getPixel(@IntRange(from = 0) int x, @IntRange(from = 0) int y) {
if (x >= mNativeInfoHandle.getWidth()) { //need to check explicitly because reused bitmap may be larger
throw new IllegalArgumentException("x must be < width");
}
if (y >= mNativeInfoHandle.getHeight()) {
throw new IllegalArgumentException("y must be < height");
}
return mBuffer.getPixel(x, y);
}
@Override
protected void onBoundsChange(@NonNull Rect bounds) {
mDstRect.set(bounds);
if (mTransform != null) {
mTransform.onBoundsChange(bounds);
}
}
/**
* Reads and renders new frame if needed then draws last rendered frame.
*
* @param canvas canvas to draw into
*/
@OverrideView on GitHub (pinned to 26ff795f78)