public class RippleDrawable extends LayerDrawable
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
android.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" />
Drawable.Callback, Drawable.ConstantState
Modifier and Type | Field and Description |
---|---|
static int |
RADIUS_AUTO
Radius value that specifies the ripple radius should be computed based
on the size of the ripple's container.
|
INSET_UNDEFINED, PADDING_MODE_NEST, PADDING_MODE_STACK
Constructor and Description |
---|
RippleDrawable(ColorStateList color,
Drawable content,
Drawable mask)
Creates a new ripple drawable with the specified ripple color and
optional content and mask drawables.
|
Modifier and Type | Method and Description |
---|---|
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
Drawable.ConstantState instance that holds the shared state of this Drawable. |
Rect |
getDirtyBounds()
Return the drawable's dirty bounds Rect.
|
void |
getHotspotBounds(Rect outRect)
Populates
outRect with the hotspot bounds. |
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
void |
getOutline(Outline outline)
Populates
outline with the first available layer outline,
excluding the mask layer. |
int |
getRadius() |
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
Drawable.Callback implementation to have this Drawable
redrawn. |
boolean |
isProjected()
Whether this drawable requests projection.
|
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.
|
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 |
setColor(ColorStateList color)
Sets the ripple color.
|
boolean |
setDrawableByLayerId(int id,
Drawable drawable)
Replaces the
Drawable for the layer with the given id. |
void |
setForceSoftware(boolean forceSoftware)
Sets whether to disable RenderThread animations for this ripple.
|
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.
|
addLayer, clearMutated, findDrawableByLayerId, findIndexByLayerId, getAlpha, getBottomPadding, getChangingConfigurations, getDrawable, getEndPadding, getId, getIntrinsicHeight, getIntrinsicWidth, getLayerGravity, getLayerHeight, getLayerInsetBottom, getLayerInsetEnd, getLayerInsetLeft, getLayerInsetRight, getLayerInsetStart, getLayerInsetTop, getLayerWidth, getLeftPadding, getNumberOfLayers, getPadding, getPaddingMode, getRightPadding, getStartPadding, getTopPadding, invalidateDrawable, isAutoMirrored, onLayoutDirectionChanged, onLevelChange, scheduleDrawable, setAlpha, setAutoMirrored, setColorFilter, setDither, setDrawable, setId, setLayerGravity, setLayerHeight, setLayerInset, setLayerInsetBottom, setLayerInsetEnd, setLayerInsetLeft, setLayerInsetRelative, setLayerInsetRight, setLayerInsetStart, setLayerInsetTop, setLayerSize, setLayerWidth, setOpacity, setPadding, setPaddingRelative, setTintList, setTintMode, unscheduleDrawable
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getColorFilter, getCurrent, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getOpticalInsets, getState, getTransparentRegion, inflate, isFilterBitmap, isVisible, obtainAttributes, parseTintMode, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setLayoutDirection, setLevel, setState, setTint, setXfermode, unscheduleSelf
public static final int RADIUS_AUTO
public RippleDrawable(ColorStateList color, Drawable content, Drawable mask)
color
- The ripple colorcontent
- The content drawable, may be null
mask
- The mask drawable, may be null
public void jumpToCurrentState()
Drawable
jumpToCurrentState
in class LayerDrawable
public int getOpacity()
Drawable
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 Drawable.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 Drawable.setAlpha(int)
or Drawable.setColorFilter(android.graphics.ColorFilter)
methods. Some subclasses,
such as BitmapDrawable
, ColorDrawable
, and GradientDrawable
,
do account for the value of Drawable.setAlpha(int)
, but the general behavior is dependent
upon the implementation of the subclass.
getOpacity
in class LayerDrawable
PixelFormat
protected boolean onStateChange(int[] stateSet)
Drawable
onStateChange
in class LayerDrawable
protected void onBoundsChange(Rect bounds)
Drawable
onBoundsChange
in class LayerDrawable
public boolean setVisible(boolean visible, boolean restart)
Drawable
setVisible
in class LayerDrawable
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.public boolean isProjected()
Drawable
isProjected
in class LayerDrawable
public boolean isStateful()
Drawable
isStateful
in class LayerDrawable
Drawable.setState(int[])
public void setColor(ColorStateList color)
color
- Ripple color as a color state list.public void setRadius(int radius)
radius
- ripple radius in pixels, or RADIUS_AUTO
to
compute the radius based on the container sizepublic int getRadius()
RADIUS_AUTO
if the radius is
computed based on the container sizepublic void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class LayerDrawable
r
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public boolean setDrawableByLayerId(int id, Drawable drawable)
LayerDrawable
Drawable
for the layer with the given id.setDrawableByLayerId
in class LayerDrawable
id
- The layer ID to search for.drawable
- The replacement Drawable
.Drawable
was replaced (could return false if
the id was not found).public void setPaddingMode(int mode)
LayerDrawable.PADDING_MODE_STACK
.setPaddingMode
in class LayerDrawable
mode
- padding mode, one of:
LayerDrawable.PADDING_MODE_NEST
to nest each layer inside the
padding of the previous layer
LayerDrawable.PADDING_MODE_STACK
to stack each layer directly
atop the previous layer
LayerDrawable.getPaddingMode()
public void applyTheme(Resources.Theme t)
Drawable
applyTheme
in class LayerDrawable
t
- the theme to applypublic boolean canApplyTheme()
canApplyTheme
in class LayerDrawable
public void setHotspot(float x, float y)
Drawable
setHotspot
in class LayerDrawable
x
- The X coordinate of the center of the hotspoty
- The Y coordinate of the center of the hotspotpublic void setHotspotBounds(int left, int top, int right, int bottom)
Drawable
setHotspotBounds
in class LayerDrawable
left
- position in pixels of the left boundtop
- position in pixels of the top boundright
- position in pixels of the right boundbottom
- position in pixels of the bottom boundDrawable.getHotspotBounds(android.graphics.Rect)
public void getHotspotBounds(Rect outRect)
Drawable
outRect
with the hotspot bounds.getHotspotBounds
in class LayerDrawable
outRect
- the rect to populate with the hotspot boundsDrawable.setHotspotBounds(int, int, int, int)
public void getOutline(Outline outline)
outline
with the first available layer outline,
excluding the mask layer.getOutline
in class LayerDrawable
outline
- Outline in which to place the first available layer outlineView.setOutlineProvider(android.view.ViewOutlineProvider)
public void draw(Canvas canvas)
draw
in class LayerDrawable
canvas
- The canvas to draw intopublic void invalidateSelf()
Drawable
Drawable.Callback
implementation to have this Drawable
redrawn. Does nothing if there is no Callback attached to the
Drawable.public Rect getDirtyBounds()
Drawable
By default, this returns the full drawable bounds. Custom drawables may override this method to perform more precise invalidation.
getDirtyBounds
in class Drawable
public void setForceSoftware(boolean forceSoftware)
forceSoftware
- true if RenderThread animations should be disabled, false otherwisepublic Drawable.ConstantState getConstantState()
Drawable
Drawable.ConstantState
instance that holds the shared state of this Drawable.getConstantState
in class LayerDrawable
Drawable.ConstantState
,
Drawable.mutate()
public Drawable mutate()
Drawable
mutate
in class LayerDrawable
Drawable.ConstantState
,
Drawable.getConstantState()