Package icyllis.modernui.graphics.drawable

# Class ImageDrawable

java.lang.Object
icyllis.modernui.graphics.drawable.Drawable
icyllis.modernui.graphics.drawable.ImageDrawable

public class ImageDrawable extends Drawable
A Drawable that wraps an image and can be tiled, stretched, or aligned. You can create a ImageDrawable from a file path, an input stream, or from a `Image` object.

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from class icyllis.modernui.graphics.drawable.Drawable

  `Drawable.Callback, Drawable.ConstantState`

- ## Field Summary

  ### Fields inherited from class icyllis.modernui.graphics.drawable.Drawable

  `MAX_LEVEL`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ImageDrawable(Image image)`
  Deprecated.
  use `ImageDrawable(Resources, Image)` instead
  `ImageDrawable(Resources res, Image image)`
  Create drawable from an image, setting initial target density based on the display metrics of the resources.
  `ImageDrawable(Resources res, InputStream stream)`
  Create a drawable by decoding an image from the given input stream.
  `ImageDrawable(InputStream stream)`
  Deprecated.
  use `ImageDrawable(Resources, InputStream)` instead
  `ImageDrawable(String namespace, String path)`
  Create a drawable by opening a given file path and decoding the image.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clearMutated()`
  Clears the mutated state, allowing this drawable to be cached and mutated again.
  `void`
  `draw(Canvas canvas)`
  Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha).
  `int`
  `getAlpha()`
  Gets the current alpha value for the drawable.
  `ColorFilter`
  `getColorFilter()`
  Returns the current color filter, or `null` if none set.
  `final Drawable.ConstantState`
  `getConstantState()`
  Return a `Drawable.ConstantState` instance that holds the shared state of this Drawable.
  `int`
  `getGravity()`
  Get the gravity used to position/stretch the image within its bounds.
  `final Image`
  `getImage()`
  Returns the image used by this drawable to render.
  `int`
  `getIntrinsicHeight()`
  Returns the drawable's intrinsic height.
  `int`
  `getIntrinsicWidth()`
  Returns the drawable's intrinsic width.
  `final Paint`
  `getPaint()`
  Returns the paint used to render this drawable.
  `Shader.TileMode`
  `getTileModeX()`
  Indicates the repeat behavior of this drawable on the X axis.
  `Shader.TileMode`
  `getTileModeY()`
  Indicates the repeat behavior of this drawable on the Y axis.
  `boolean`
  `hasFocusStateSpecified()`
  Indicates whether this drawable has at least one state spec explicitly specifying state_focused.
  `boolean`
  `hasMipmap()`
  Indicates whether the mipmap hint is enabled on this drawable's image.
  `boolean`
  `isAntiAlias()`
  Indicates whether antialiasing is enabled for this drawable.
  `final boolean`
  `isAutoMirrored()`
  Tells if this Drawable will be automatically mirrored when its layout direction is RTL right-to-left.
  `boolean`
  `isDither()`
  Returns true if color error may be distributed to smooth color transition.
  `boolean`
  `isFilter()`
  Returns the current filter.
  `boolean`
  `isStateful()`
  Indicates whether this drawable will change its appearance based on state.
  `Drawable`
  `mutate()`
  A mutable ImageDrawable still shares its Image with any other Drawable that comes from the same resource.
  `protected void`
  `onBoundsChange(Rect bounds)`
  Override this in your subclass to change appearance if you vary based on the bounds.
  `protected boolean`
  `onStateChange(int[] stateSet)`
  Override this in your subclass to change appearance if you recognize the specified state.
  `void`
  `setAlpha(int alpha)`
  Specify an alpha value for the drawable.
  `void`
  `setAntiAlias(boolean aa)`
  Enables or disables antialiasing for this drawable.
  `void`
  `setAutoMirrored(boolean mirrored)`
  Set whether this Drawable is automatically mirrored when its layout direction is RTL (right-to left).
  `void`
  `setColorFilter(ColorFilter colorFilter)`
  Specify an optional color filter for the drawable.
  `void`
  `setDither(boolean dither)`
  Sets a hint that indicates if color error may be distributed to smooth color transition.
  `void`
  `setFilter(boolean filter)`
  Set to true to have the drawable filter texture images with bilinear sampling when they are scaled or rotated.
  `void`
  `setGravity(int gravity)`
  Set the gravity used to position/stretch the image within its bounds.
  `void`
  `setImage(Image image)`
  Switch to a new Image object.
  `void`
  `setMipmap(boolean mipmap)`
  Enables or disables the mipmap hint for this drawable's image.
  `void`
  `setSrcRect(int left, int top, int right, int bottom)`
  Specifies the subset of the image to draw.
  `void`
  `setSrcRect(Rect srcRect)`
  Specifies the subset of the image to draw.
  `void`
  `setTargetDensity(int density)`
  Set the density at which this drawable will be rendered.
  `void`
  `setTileModeX(Shader.TileMode mode)`
  Sets the repeat behavior of this drawable on the X axis.
  `void`
  `setTileModeXY(Shader.TileMode tileModeX, Shader.TileMode tileModeY)`
  Sets the repeat behavior of this drawable on both axis.
  `final void`
  `setTileModeY(Shader.TileMode mode)`
  Sets the repeat behavior of this drawable on the Y axis.
  `void`
  `setTintBlendMode(BlendMode blendMode)`
  Specifies a tint blending mode for this drawable.
  `void`
  `setTintList(ColorStateList tint)`
  Specifies tint color for this drawable as a color state list.

  ### Methods inherited from class icyllis.modernui.graphics.drawable.Drawable

  `applyTheme, canApplyTheme, copyBounds, copyBounds, getBounds, getCallback, getChangingConfigurations, getCurrent, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getPadding, getState, invalidateSelf, isVisible, jumpToCurrentState, onLayoutDirectionChanged, onLevelChange, scaleFromDensity, scaleFromDensity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setVisible, unscheduleSelf`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ImageDrawable

    @Deprecated public ImageDrawable(Image image)
    Deprecated.
    use `ImageDrawable(Resources, Image)` instead
    Create drawable from an image, setting target density to `DisplayMetrics.DENSITY_DEFAULT`.

  - ### ImageDrawable

    public ImageDrawable(Resources res, Image image)
    Create drawable from an image, setting initial target density based on the display metrics of the resources.

  - ### ImageDrawable

    public ImageDrawable(@NonNull String namespace, @NonNull String path)
    Create a drawable by opening a given file path and decoding the image.

    This method may only be called from UI thread.

  - ### ImageDrawable

    @Deprecated public ImageDrawable(@NonNull InputStream stream)
    Deprecated.
    use `ImageDrawable(Resources, InputStream)` instead
    Create a drawable by decoding an image from the given input stream.

  - ### ImageDrawable

    public ImageDrawable(Resources res, @NonNull InputStream stream)
    Create a drawable by decoding an image from the given input stream.

    This method silently ignores any IO exception. This method does not closed the given stream after read operation has completed. The stream will be at end if read operation succeeds.

    This method may only be called from UI thread.

- ## Method Details

  - ### getPaint

    @NonNull public final Paint getPaint()
    Returns the paint used to render this drawable.

  - ### getImage

    public final Image getImage()
    Returns the image used by this drawable to render. May be null.

  - ### setImage

    public void setImage(@Nullable Image image)
    Switch to a new Image object. Calling this method will also reset the subset to the full image, see `setSrcRect(Rect)`.

  - ### setTargetDensity

    public void setTargetDensity(int density)
    Set the density at which this drawable will be rendered.
    Parameters:
      `density` - The density scale for this drawable.
    See Also:
      - `Image.setDensity(int)`
      - `Image.getDensity()`

  - ### getGravity

    public int getGravity()
    Get the gravity used to position/stretch the image within its bounds.
    Returns:
      the gravity applied to the image

  - ### setGravity

    public void setGravity(int gravity)
    Set the gravity used to position/stretch the image within its bounds.
    Parameters:
      `gravity` - the gravity

  - ### setSrcRect

    public void setSrcRect(int left, int top, int right, int bottom)
    Specifies the subset of the image to draw. To draw the full image, call `setSrcRect(Rect)` with null.

    Calling this method when there's no image has no effect. Next call to `setImage(Image)` will reset the subset to the full image.

  - ### setSrcRect

    public void setSrcRect(@Nullable Rect srcRect)
    Specifies the subset of the image to draw. Null for the full image.

    Calling this method when there's no image has no effect. Next call to `setImage(Image)` will reset the subset to the full image.

    Parameters:
      `srcRect` - the subset of the image

  - ### setMipmap

    @Experimental public void setMipmap(boolean mipmap)
    Enables or disables the mipmap hint for this drawable's image.

    If the image is null calling this method has no effect.

    Parameters:
      `mipmap` - True if the image should use mipmaps, false otherwise.
    See Also:
      - `hasMipmap()`

  - ### hasMipmap

    @Experimental public boolean hasMipmap()
    Indicates whether the mipmap hint is enabled on this drawable's image.
    Returns:
      True if the mipmap hint is set, false otherwise. If the image is null, this method always returns false.
    See Also:
      - `setMipmap(boolean)`

  - ### setAntiAlias

    public void setAntiAlias(boolean aa)
    Enables or disables antialiasing for this drawable. Antialiasing affects the edges of the image only so it applies only when the drawable is rotated. The default is true.
    Parameters:
      `aa` - True if the image should be anti-aliased, false otherwise.
    See Also:
      - `isAntiAlias()`

  - ### isAntiAlias

    public boolean isAntiAlias()
    Indicates whether antialiasing is enabled for this drawable. The default is true.
    Returns:
      True if antialiasing is enabled, false otherwise.
    See Also:
      - `setAntiAlias(boolean)`

  - ### setDither

    public void setDither(boolean dither)
    Sets a hint that indicates if color error may be distributed to smooth color transition. For example, drawing 16-bit per channel image onto an 8-bit per channel device. The default value is false.

  - ### isDither

    public boolean isDither()
    Returns true if color error may be distributed to smooth color transition. The default value is false.

  - ### setFilter

    public void setFilter(boolean filter)
    Set to true to have the drawable filter texture images with bilinear sampling when they are scaled or rotated. The default is true.
    Parameters:
      `filter` - true to use bilinear sampling, false to use nearest neighbor sampling

  - ### isFilter

    public boolean isFilter()
    Returns the current filter. The default is true.

  - ### getTileModeX

    @Nullable public Shader.TileMode getTileModeX()
    Indicates the repeat behavior of this drawable on the X axis.
    Returns:
      `Shader.TileMode.CLAMP` if the image does not repeat, `Shader.TileMode.REPEAT` or `Shader.TileMode.MIRROR` otherwise.

  - ### getTileModeY

    @Nullable public Shader.TileMode getTileModeY()
    Indicates the repeat behavior of this drawable on the Y axis.
    Returns:
      `Shader.TileMode.CLAMP` if the image does not repeat, `Shader.TileMode.REPEAT` or `Shader.TileMode.MIRROR` otherwise.

  - ### setTileModeX

    public void setTileModeX(@Nullable Shader.TileMode mode)
    Sets the repeat behavior of this drawable on the X axis. By default, the drawable does not repeat its image. Using `Shader.TileMode.REPEAT` or `Shader.TileMode.MIRROR` the image can be repeated (or tiled) if the image is smaller than this drawable.
    Parameters:
      `mode` - The repeat mode for this drawable.
    See Also:
      - `setTileModeY(Shader.TileMode)`
      - `setTileModeXY(Shader.TileMode, Shader.TileMode)`

  - ### setTileModeY

    public final void setTileModeY(@Nullable Shader.TileMode mode)
    Sets the repeat behavior of this drawable on the Y axis. By default, the drawable does not repeat its image. Using `Shader.TileMode.REPEAT` or `Shader.TileMode.MIRROR` the image can be repeated (or tiled) if the image is smaller than this drawable.
    Parameters:
      `mode` - The repeat mode for this drawable.
    See Also:
      - `setTileModeX(Shader.TileMode)`
      - `setTileModeXY(Shader.TileMode, Shader.TileMode)`

  - ### setTileModeXY

    public void setTileModeXY(@Nullable Shader.TileMode tileModeX, @Nullable Shader.TileMode tileModeY)
    Sets the repeat behavior of this drawable on both axis. By default, the drawable does not repeat its image. Using `Shader.TileMode.REPEAT` or `Shader.TileMode.MIRROR` the image can be repeated (or tiled) if the image is smaller than this drawable.
    Parameters:
      `tileModeX` - The X tile mode for this drawable.
      `tileModeY` - The Y tile mode for this drawable.
    See Also:
      - `setTileModeX(Shader.TileMode)`
      - `setTileModeY(Shader.TileMode)`

  - ### setAutoMirrored

    public void setAutoMirrored(boolean mirrored)
    Description copied from class: `Drawable`
    Set whether this Drawable is automatically mirrored when its layout direction is RTL (right-to left). See `LayoutDirection`.
    Overrides:
      `setAutoMirrored` in class `Drawable`
    Parameters:
      `mirrored` - Set to true if the Drawable should be mirrored, false if not.

  - ### isAutoMirrored

    public final boolean isAutoMirrored()
    Description copied from class: `Drawable`
    Tells if this Drawable will be automatically mirrored when its layout direction is RTL right-to-left. See `LayoutDirection`.
    Overrides:
      `isAutoMirrored` in class `Drawable`
    Returns:
      boolean Returns true if this Drawable will be automatically mirrored.

  - ### onBoundsChange

    protected void onBoundsChange(@NonNull Rect bounds)
    Description copied from class: `Drawable`
    Override this in your subclass to change appearance if you vary based on the bounds.
    Overrides:
      `onBoundsChange` in class `Drawable`

  - ### draw

    public void draw(@NonNull Canvas canvas)
    Description copied from class: `Drawable`
    Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha).
    Specified by:
      `draw` in class `Drawable`
    Parameters:
      `canvas` - The canvas to draw into

  - ### setAlpha

    public void setAlpha(int alpha)
    Description copied from class: `Drawable`
    Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.
    Overrides:
      `setAlpha` in class `Drawable`

  - ### getAlpha

    public int getAlpha()
    Description copied from class: `Drawable`
    Gets the current alpha value for the drawable. 0 means fully transparent, 255 means fully opaque. This method is implemented by Drawable subclasses and the value returned is specific to how that class treats alpha. The default return value is 255 if the class does not override this method to return a value specific to its use of alpha.
    Overrides:
      `getAlpha` in class `Drawable`

  - ### setTintList

    public void setTintList(@Nullable ColorStateList tint)
    Description copied from class: `Drawable`
    Specifies tint color for this drawable as a color state list.

    A Drawable's drawing content will be blended together with its tint before it is drawn to the screen.

    Note: Setting a color filter via `Drawable.setColorFilter(ColorFilter)` overrides tint.

    Overrides:
      `setTintList` in class `Drawable`
    Parameters:
      `tint` - Color state list to use for tinting this drawable, or `null` to clear the tint
    See Also:
      - `Drawable.setTint(int)`
      - `Drawable.setTintBlendMode(BlendMode)`

  - ### setTintBlendMode

    public void setTintBlendMode(@NonNull BlendMode blendMode)
    Description copied from class: `Drawable`
    Specifies a tint blending mode for this drawable.

    Defines how this drawable's tint color should be blended into the drawable before it is drawn to screen. Default tint mode is `BlendMode.SRC_IN`.

    Note: Setting a color filter via `Drawable.setColorFilter(ColorFilter)`

    Overrides:
      `setTintBlendMode` in class `Drawable`
    Parameters:
      `blendMode` - BlendMode to apply to the drawable, a value of null sets the default blend mode value of `BlendMode.SRC_IN`
    See Also:
      - `Drawable.setTint(int)`
      - `Drawable.setTintList(ColorStateList)`

  - ### setColorFilter

    public void setColorFilter(@Nullable ColorFilter colorFilter)
    Description copied from class: `Drawable`
    Specify an optional color filter for the drawable.

    If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.

    Pass `null` to remove any existing color filter.

    Note: Setting a non-`null` color filter disables `tint`.

    Overrides:
      `setColorFilter` in class `Drawable`
    Parameters:
      `colorFilter` - The color filter to apply, or `null` to remove the existing color filter

  - ### getColorFilter

    @Nullable public ColorFilter getColorFilter()
    Description copied from class: `Drawable`
    Returns the current color filter, or `null` if none set.
    Overrides:
      `getColorFilter` in class `Drawable`
    Returns:
      the current color filter, or `null` if none set

  - ### mutate

    @NonNull public Drawable mutate()
    A mutable ImageDrawable still shares its Image with any other Drawable that comes from the same resource.
    Overrides:
      `mutate` in class `Drawable`
    Returns:
      This drawable.
    See Also:
      - `Drawable.ConstantState`
      - `Drawable.getConstantState()`

  - ### clearMutated

    public void clearMutated()
    Description copied from class: `Drawable`
    Clears the mutated state, allowing this drawable to be cached and mutated again.
    Overrides:
      `clearMutated` in class `Drawable`

  - ### onStateChange

    protected boolean onStateChange(@NonNull int[] stateSet)
    Description copied from class: `Drawable`
    Override this in your subclass to change appearance if you recognize the specified state.
    Overrides:
      `onStateChange` in class `Drawable`
    Returns:
      Returns true if the state change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last state.

  - ### isStateful

    public boolean isStateful()
    Description copied from class: `Drawable`
    Indicates whether this drawable will change its appearance based on state. Clients can use this to determine whether it is necessary to calculate their state and call setState.
    Overrides:
      `isStateful` in class `Drawable`
    Returns:
      True if this drawable changes its appearance based on state, false otherwise.
    See Also:
      - `Drawable.setState(int[])`

  - ### hasFocusStateSpecified

    public boolean hasFocusStateSpecified()
    Description copied from class: `Drawable`
    Indicates whether this drawable has at least one state spec explicitly specifying state_focused.

    Note: A View uses a `Drawable` instance as its background and it changes its appearance based on a state. On keyboard devices, it should specify its state_focused to make sure the user knows which view is holding the focus.

    Overrides:
      `hasFocusStateSpecified` in class `Drawable`
    Returns:
      `true` if state_focused is specified for this drawable.

  - ### getIntrinsicWidth

    public int getIntrinsicWidth()
    Description copied from class: `Drawable`
    Returns the drawable's intrinsic width.

    Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.

    Overrides:
      `getIntrinsicWidth` in class `Drawable`
    Returns:
      the intrinsic width, or -1 if no intrinsic width

  - ### getIntrinsicHeight

    public int getIntrinsicHeight()
    Description copied from class: `Drawable`
    Returns the drawable's intrinsic height.

    Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.

    Overrides:
      `getIntrinsicHeight` in class `Drawable`
    Returns:
      the intrinsic height, or -1 if no intrinsic height

  - ### getConstantState

    public final Drawable.ConstantState getConstantState()
    Description copied from class: `Drawable`
    Return a `Drawable.ConstantState` instance that holds the shared state of this Drawable.
    Overrides:
      `getConstantState` in class `Drawable`
    Returns:
      The ConstantState associated to that Drawable.
    See Also:
      - `Drawable.ConstantState`
      - `Drawable.mutate()`