Class RippleDrawable

All Implemented Interfaces:
Drawable.Callback

public class RippleDrawable extends LayerDrawable
Drawable that shows a ripple effect in response to state changes. The anchoring position of the ripple for a given state may be specified by calling setHotspot(float, float) with the corresponding state attribute identifier.

A touch feedback drawable may contain multiple child layers, including a special mask layer that is not drawn to the screen. A single layer may be set as the mask from XML by specifying its android:id value as R.id.mask. At runtime, a single layer may be set as the mask using setId(..., R.id.mask) or an existing mask layer may be replaced using setDrawableByLayerId(R.id.mask, ...).

If a mask layer is set, the ripple effect will be masked against that layer before it is drawn over the composite of the remaining child layers.

If no mask layer is set, the ripple effect is masked against the composite of the child layers.

If no child layers or mask is specified and the ripple is set as a View background, the ripple will be drawn atop the first available parent background within the View's hierarchy. In this case, the drawing region may extend outside of the Drawable bounds.

  • Field Details

    • RADIUS_AUTO

      public static final int RADIUS_AUTO
      Radius value that specifies the ripple radius should be computed based on the size of the ripple's container.
      See Also:
  • Constructor Details

    • RippleDrawable

      public RippleDrawable(@NonNull ColorStateList color, @Nullable Drawable content, @Nullable Drawable mask)
      Creates a new ripple drawable with the specified ripple color and optional content and mask drawables.
      Parameters:
      color - The ripple color
      content - The content drawable, may be null
      mask - The mask drawable, may be null
  • Method Details

    • jumpToCurrentState

      public void jumpToCurrentState()
      Description copied from class: Drawable
      If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.
      Overrides:
      jumpToCurrentState in class LayerDrawable
    • 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 LayerDrawable
      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.
    • setBackgroundActive

      public void setBackgroundActive(boolean hovered, boolean focused, boolean pressed, boolean windowFocused)
    • 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 LayerDrawable
    • setVisible

      public boolean setVisible(boolean visible, boolean restart)
      Description copied from class: Drawable
      Set whether this Drawable is visible. This generally does not impact the Drawable's behavior, but is a hint that can be used by some Drawables, for example, to decide whether run animations.
      Overrides:
      setVisible in class LayerDrawable
      Parameters:
      visible - Set to true if visible, false if not.
      restart - You can supply true here to force the drawable to behave as if it has just become visible, even if it had last been set visible. Used for example to force animations to restart.
      Returns:
      true if the new visibility is different from its previous 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 LayerDrawable
      Returns:
      True if this drawable changes its appearance based on state, false otherwise.
      See Also:
    • 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 LayerDrawable
      Returns:
      true if state_focused is specified for this drawable.
    • setColor

      public void setColor(@NonNull ColorStateList color)
      Sets the ripple color.
      Parameters:
      color - Ripple color as a color state list.
    • setEffectColor

      public void setEffectColor(@NonNull ColorStateList color)
      Sets the ripple effect color.
      Parameters:
      color - Ripple color as a color state list.
    • getEffectColor

      @NonNull public ColorStateList getEffectColor()
      Returns:
      The ripple effect color as a color state list.
    • setRadius

      public void setRadius(int radius)
      Sets the radius in pixels of the fully expanded ripple.
      Parameters:
      radius - ripple radius in pixels, or RADIUS_AUTO to compute the radius based on the container size
    • getRadius

      public int getRadius()
      Returns:
      the radius in pixels of the fully expanded ripple if an explicit radius has been set, or RADIUS_AUTO if the radius is computed based on the container size
    • setDrawableByLayerId

      public boolean setDrawableByLayerId(int id, Drawable drawable)
      Description copied from class: LayerDrawable
      Replaces the Drawable for the layer with the given id.
      Overrides:
      setDrawableByLayerId in class LayerDrawable
      Parameters:
      id - The layer ID to search for.
      drawable - The replacement Drawable.
      Returns:
      Whether the Drawable was replaced (could return false if the id was not found).
    • canApplyTheme

      public boolean canApplyTheme()
      Overrides:
      canApplyTheme in class LayerDrawable
    • setHotspot

      public void setHotspot(float x, float y)
      Description copied from class: Drawable
      Specifies the hotspot's location within the drawable.
      Overrides:
      setHotspot in class LayerDrawable
      Parameters:
      x - The X coordinate of the center of the hotspot
      y - The Y coordinate of the center of the hotspot
    • setHotspotBounds

      public void setHotspotBounds(int left, int top, int right, int bottom)
      Description copied from class: Drawable
      Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.
      Overrides:
      setHotspotBounds in class LayerDrawable
      Parameters:
      left - position in pixels of the left bound
      top - position in pixels of the top bound
      right - position in pixels of the right bound
      bottom - position in pixels of the bottom bound
      See Also:
    • getHotspotBounds

      public void getHotspotBounds(@NonNull Rect outRect)
      Description copied from class: Drawable
      Populates outRect with the hotspot bounds.
      Overrides:
      getHotspotBounds in class LayerDrawable
      Parameters:
      outRect - the rect to populate with the hotspot bounds
      See Also:
    • getOutline

      public void getOutline(@NonNull Outline outline)
      Populates outline with the first available layer outline, excluding the mask layer.
      Overrides:
      getOutline in class LayerDrawable
      Parameters:
      outline - Outline in which to place the first available layer outline
      See Also:
    • draw

      public void draw(@NonNull Canvas canvas)
      Optimized for drawing ripples with a mask layer and optional content.
      Overrides:
      draw in class LayerDrawable
      Parameters:
      canvas - The canvas to draw into
    • getDirtyBounds

      @NonNull public Rect getDirtyBounds()
      Description copied from class: Drawable
      Return the drawable's dirty bounds Rect. Note: for efficiency, the returned object may be the same object stored in the drawable (though this is not guaranteed).

      By default, this returns the full drawable bounds. Custom drawables may override this method to perform more precise invalidation.

      Overrides:
      getDirtyBounds in class Drawable
      Returns:
      The dirty bounds of this drawable (which may change later, so caller beware). DO NOT ALTER the returned object as it may change the stored bounds of this drawable.
    • invalidateSelf

      public void invalidateSelf()
      Description copied from class: Drawable
      Use the current Drawable.Callback implementation to have this Drawable redrawn. Does nothing if there is no Callback attached to the Drawable.
      Overrides:
      invalidateSelf in class Drawable
      See Also:
    • getConstantState

      public 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 LayerDrawable
      Returns:
      The ConstantState associated to that Drawable.
      See Also:
    • mutate

      @NonNull public Drawable mutate()
      Description copied from class: Drawable
      Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

      Calling this method on a mutable Drawable will have no effect.

      Overrides:
      mutate in class LayerDrawable
      Returns:
      This drawable.
      See Also: