RippleDrawable


public class RippleDrawable
extends LayerDrawable

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.LayerDrawable
       ↳ android.graphics.drawable.RippleDrawable


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 run time, a single layer may be set as the mask using setId(..., android.R.id.mask) or an existing mask layer may be replaced using setDrawableByLayerId(android.R.id.mask, ...).

 <!-- A red ripple masked against an opaque rectangle. -->
 <ripple android:color="#ffff0000">
   <item android:id="@android:id/mask"
         android:drawable="@android:color/white" />
 </ripple>
 

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.

 <!-- A green ripple drawn atop a black rectangle. -->
 <ripple android:color="#ff00ff00">
   <item android:drawable="@android:color/black" />
 </ripple>

 <!-- A blue ripple drawn atop a drawable resource. -->
 <ripple android:color="#ff0000ff">
   <item android:drawable="@drawable/my_drawable" />
 </ripple>
 

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.

 <!-- An unbounded red ripple. -->
 <ripple android:color="#ffff0000" />
 

Summary

XML attributes

android:color The color to use for ripple effects. 
android:effectColor Secondary color of the ripple effect. 
android:radius The radius of the ripple when fully expanded. 

Inherited XML attributes

Constants

int RADIUS_AUTO

Radius value that specifies the ripple radius should be computed based on the size of the ripple's container.

Inherited constants

Public constructors

RippleDrawable(ColorStateList color, Drawable content, Drawable mask)

Creates a new ripple drawable with the specified ripple color and optional content and mask drawables.

Public methods

void applyTheme(Resources.Theme t)

Applies the specified theme to this Drawable and its children.

boolean canApplyTheme()
void draw(Canvas canvas)

Optimized for drawing ripples with a mask layer and optional content.

Drawable.ConstantState getConstantState()

Return a ConstantState instance that holds the shared state of this Drawable.

Rect getDirtyBounds()

Return the drawable's dirty bounds Rect.

ColorStateList getEffectColor()
void getHotspotBounds(Rect outRect)

Populates outRect with the hotspot bounds.

int getOpacity()

This method is deprecated. This method is no longer used in graphics optimizations

void getOutline(Outline outline)

Populates outline with the first available layer outline, excluding the mask layer.

int getRadius()
boolean hasFocusStateSpecified()

Indicates whether this drawable has at least one state spec explicitly specifying R.attr.state_focused.

void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

void invalidateSelf()

Use the current Callback implementation to have this Drawable redrawn.

boolean isStateful()

Indicates whether this drawable will change its appearance based on state.

void jumpToCurrentState()

If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.

Drawable mutate()

Make this drawable mutable.

void setColor(ColorStateList color)

Sets the ripple color.

boolean setDrawableByLayerId(int id, Drawable drawable)

Replaces the Drawable for the layer with the given id.

void setEffectColor(ColorStateList color)

Sets the ripple effect color.

void setHotspot(float x, float y)

Specifies the hotspot's location within the drawable.

void setHotspotBounds(int left, int top, int right, int bottom)

Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.

void setPaddingMode(int mode)

Specifies how layer padding should affect the bounds of subsequent layers.

void setRadius(int radius)

Sets the radius in pixels of the fully expanded ripple.

boolean setVisible(boolean visible, boolean restart)

Set whether this Drawable is visible.

Protected methods

void onBoundsChange(Rect bounds)

Override this in your subclass to change appearance if you vary based on the bounds.

boolean onStateChange(int[] stateSet)

Override this in your subclass to change appearance if you recognize the specified state.

Inherited methods

XML attributes

android:color

The color to use for ripple effects. This attribute is required.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

Related methods:

android:effectColor

Secondary color of the ripple effect.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

Related methods:

android:radius

The radius of the ripple when fully expanded. By default, the radius is computed based on the size of the ripple's container.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

Constants

RADIUS_AUTO

Added in API level 23
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.

Constant Value: -1 (0xffffffff)

Public constructors

RippleDrawable

Added in API level 21
public RippleDrawable (ColorStateList color, 
                Drawable content, 
                Drawable mask)

Creates a new ripple drawable with the specified ripple color and optional content and mask drawables.

Parameters
color ColorStateList: The ripple color This value cannot be null.

content Drawable: The content drawable, may be null

mask Drawable: The mask drawable, may be null

Public methods

applyTheme

Added in API level 21
public void applyTheme (Resources.Theme t)

Applies the specified theme to this Drawable and its children.

Parameters
t Resources.Theme: This value cannot be null.

canApplyTheme

Added in API level 21
public boolean canApplyTheme ()

Returns
boolean

draw

Added in API level 21
public void draw (Canvas canvas)

Optimized for drawing ripples with a mask layer and optional content.

Parameters
canvas Canvas: This value cannot be null.

getConstantState

Added in API level 21
public Drawable.ConstantState getConstantState ()

Return a ConstantState instance that holds the shared state of this Drawable.

Returns
Drawable.ConstantState The ConstantState associated to that Drawable. This value may be null.

getDirtyBounds

Added in API level 21
public Rect getDirtyBounds ()

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.

Returns
Rect The dirty bounds of this drawable This value cannot be null.

getEffectColor

Added in API level 31
public ColorStateList getEffectColor ()

Returns
ColorStateList The ripple effect color as a color state list. This value cannot be null.

getHotspotBounds

Added in API level 23
public void getHotspotBounds (Rect outRect)

Populates outRect with the hotspot bounds.

Parameters
outRect Rect: the rect to populate with the hotspot bounds This value cannot be null.

getOpacity

Added in API level 21
public int getOpacity ()

This method is deprecated.
This method is no longer used in graphics optimizations

Return the opacity/transparency of this Drawable. The returned value is one of the abstract format constants in PixelFormat: PixelFormat.UNKNOWN, PixelFormat.TRANSLUCENT, PixelFormat.TRANSPARENT, or PixelFormat.OPAQUE.

An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.

Generally a Drawable should be as conservative as possible with the value it returns. For example, if it contains multiple child drawables and only shows one of them at a time, if only one of the children is TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be returned. You can use the method resolveOpacity(int, int) to perform a standard reduction of two opacities to the appropriate single output.

Note that the returned value does not necessarily take into account a custom alpha or color filter that has been applied by the client through the setAlpha(int) or setColorFilter(ColorFilter) methods. Some subclasses, such as BitmapDrawable, ColorDrawable, and GradientDrawable, do account for the value of setAlpha(int), but the general behavior is dependent upon the implementation of the subclass.

Returns
int int The opacity class of the Drawable. Value is PixelFormat.UNKNOWN, PixelFormat.TRANSLUCENT, PixelFormat.TRANSPARENT, or PixelFormat.OPAQUE

getOutline

Added in API level 21
public void getOutline (Outline outline)

Populates outline with the first available layer outline, excluding the mask layer.

Parameters
outline Outline: Outline in which to place the first available layer outline This value cannot be null.

getRadius

Added in API level 23
public int getRadius ()

Related XML Attributes:

Returns
int 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

hasFocusStateSpecified

Added in API level 31
public boolean hasFocusStateSpecified ()

Indicates whether this drawable has at least one state spec explicitly specifying R.attr.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 R.attr.state_focused to make sure the user knows which view is holding the focus.

Returns
boolean true if R.attr.state_focused is specified for this drawable.

inflate

Added in API level 21
public void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme. This can't be called more than once for each Drawable. Note that framework may have called this once to create the Drawable instance from XML resource.

Parameters
r Resources: This value cannot be null.

parser XmlPullParser: This value cannot be null.

attrs AttributeSet: This value cannot be null.

theme Resources.Theme: This value may be null.

Throws
IOException
XmlPullParserException

invalidateSelf

Added in API level 21
public void invalidateSelf ()

Use the current Callback implementation to have this Drawable redrawn. Does nothing if there is no Callback attached to the Drawable.

isStateful

Added in API level 21
public boolean isStateful ()

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.

Returns
boolean True if this drawable changes its appearance based on state, false otherwise.

jumpToCurrentState

Added in API level 21
public void jumpToCurrentState ()

If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.

mutate

Added in API level 21
public Drawable mutate ()

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.

Returns
Drawable This drawable. This value cannot be null.

setColor

Added in API level 21
public void setColor (ColorStateList color)

Sets the ripple color.

Related XML Attributes:

Parameters
color ColorStateList: Ripple color as a color state list. This value cannot be null.

setDrawableByLayerId

Added in API level 21
public boolean setDrawableByLayerId (int id, 
                Drawable drawable)

Replaces the Drawable for the layer with the given id.

Parameters
id int: The layer ID to search for.

drawable Drawable: The replacement Drawable.

Returns
boolean Whether the Drawable was replaced (could return false if the id was not found).

setEffectColor

Added in API level 31
public void setEffectColor (ColorStateList color)

Sets the ripple effect color.

Related XML Attributes:

Parameters
color ColorStateList: Ripple color as a color state list. This value cannot be null.

setHotspot

Added in API level 21
public void setHotspot (float x, 
                float y)

Specifies the hotspot's location within the drawable.

Parameters
x float: The X coordinate of the center of the hotspot

y float: The Y coordinate of the center of the hotspot

setHotspotBounds

Added in API level 21
public void setHotspotBounds (int left, 
                int top, 
                int right, 
                int bottom)

Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.

Parameters
left int: position in pixels of the left bound

top int: position in pixels of the top bound

right int: position in pixels of the right bound

bottom int: position in pixels of the bottom bound

setPaddingMode

Added in API level 21
public void setPaddingMode (int mode)

Specifies how layer padding should affect the bounds of subsequent layers. The default and recommended value for RippleDrawable is LayerDrawable.PADDING_MODE_STACK.

Parameters
mode int: padding mode, one of:

setRadius

Added in API level 23
public void setRadius (int radius)

Sets the radius in pixels of the fully expanded ripple.

Related XML Attributes:

Parameters
radius int: ripple radius in pixels, or RADIUS_AUTO to compute the radius based on the container size

setVisible

Added in API level 21
public boolean setVisible (boolean visible, 
                boolean restart)

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.

Parameters
visible boolean: Set to true if visible, false if not.

restart boolean: 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
boolean boolean Returns true if the new visibility is different than its previous state.

Protected methods

onBoundsChange

Added in API level 21
protected void onBoundsChange (Rect bounds)

Override this in your subclass to change appearance if you vary based on the bounds.

Parameters
bounds Rect: This value cannot be null.

onStateChange

Added in API level 21
protected boolean onStateChange (int[] stateSet)

Override this in your subclass to change appearance if you recognize the specified state.

Parameters
stateSet int: This value cannot be null.

Returns
boolean 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.