public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCallback, Drawable.Callback, RequestKey.Callback
The actual bitmap decode work is handled by DecodeTask
.
If being used with a long-lived cache (static cache, attached to the Application instead of the
Activity, etc) then make sure to call unbind()
at the appropriate
times so the cache has accurate unref counts. The
BitmapDrawableImageView
class has been created to do the
appropriate unbind operation when the view is detached from the window.
Drawable.Callback, Drawable.ConstantState
Modifier and Type | Field and Description |
---|---|
protected RequestKey |
mCurrKey |
protected int |
mDecodeHeight |
protected int |
mDecodeWidth |
protected Paint |
mPaint |
protected RequestKey |
mPrevKey |
Constructor and Description |
---|
BasicBitmapDrawable(Resources res,
BitmapCache cache,
boolean limitDensity) |
Modifier and Type | Method and Description |
---|---|
void |
bind(RequestKey key)
Binds to the given key and start the decode process.
|
protected void |
decode(RequestKey.FileDescriptorFactory factory)
Should only be overriden, not called.
|
void |
draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
|
void |
fileDescriptorFactoryCreated(RequestKey key,
RequestKey.FileDescriptorFactory factory)
Notifies that the
RequestKey.FileDescriptorFactory has been created. |
protected ReusableBitmap |
getBitmap() |
protected float |
getDecodeHorizontalCenter()
Clients can override this to specify which section of the source image to decode from.
|
protected int |
getDecodeStrategy()
Return one of the STRATEGY constants in
DecodeTask.DecodeOptions . |
protected float |
getDecodeVerticalCenter()
Clients can override this to specify which section of the source image to decode from.
|
protected float |
getDrawVerticalCenter() |
protected float |
getDrawVerticalOffsetMultiplier() |
protected Executor |
getExecutor() |
RequestKey |
getKey() |
int |
getLayoutDirectionLocal()
Returns the resolved layout direction for this Drawable.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
RequestKey |
getPreviousKey() |
protected boolean |
hasBitmap() |
void |
invalidateDrawable(Drawable who)
Called when the drawable needs to be redrawn.
|
protected void |
loadFileDescriptorFactory()
Should only be overriden, not called.
|
void |
onDecodeBegin(RequestKey key)
Notifies that the async task's work is about to begin.
|
void |
onDecodeCancel(RequestKey key)
The task has been canceled, and
DecodeTask.DecodeCallback.onDecodeComplete(RequestKey, ReusableBitmap)
will not be called. |
void |
onDecodeComplete(RequestKey key,
ReusableBitmap result)
The task is now complete and the ReusableBitmap is available for use.
|
protected void |
onDecodeFailed()
Called when the decode process is cancelled at any time.
|
protected void |
onDrawBitmap(Canvas canvas,
Rect src,
Rect dst)
Override this method to customize how to draw the bitmap to the canvas for the given bounds.
|
void |
onLayoutDirectionChangeLocal(int layoutDirection)
Called when the drawable's resolved layout direction changes.
|
void |
scheduleDrawable(Drawable who,
Runnable what,
long when)
A Drawable can call this to schedule the next frame of its
animation.
|
void |
setAlpha(int alpha)
Specify an alpha value for the drawable. 0 means fully transparent, and
255 means fully opaque.
|
protected void |
setBitmap(ReusableBitmap bmp)
Should only be overriden, not called.
|
void |
setColorFilter(ColorFilter cf)
Specify an optional color filter for the drawable.
|
void |
setDecodeDimensions(int width,
int height)
Set the dimensions to decode into.
|
protected void |
setImage(RequestKey key)
Should only be overriden, not called.
|
void |
setLayoutDirectionLocal(int layoutDirection)
Set layout direction.
|
void |
unbind()
Unbinds the current key and bitmap from the drawable.
|
void |
unbind(boolean temporary)
Unbinds the current key and bitmap from the drawable.
|
void |
unscheduleDrawable(Drawable who,
Runnable what)
A Drawable can call this to unschedule an action previously
scheduled with
Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, long) . |
applyTheme, canApplyTheme, clearColorFilter, clearMutated, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getAlpha, getBounds, getCallback, getChangingConfigurations, getColorFilter, getConstantState, getCurrent, getDirtyBounds, getHotspotBounds, getIntrinsicHeight, getIntrinsicWidth, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getOpticalInsets, getOutline, getPadding, getState, getTransparentRegion, inflate, inflate, invalidateSelf, isAutoMirrored, isFilterBitmap, isProjected, isStateful, isVisible, jumpToCurrentState, mutate, obtainAttributes, onBoundsChange, onLayoutDirectionChanged, onLevelChange, onStateChange, parseTintMode, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setDither, setFilterBitmap, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setTintList, setTintMode, setVisible, setXfermode, unscheduleSelf
protected RequestKey mCurrKey
protected RequestKey mPrevKey
protected int mDecodeWidth
protected int mDecodeHeight
protected final Paint mPaint
public BasicBitmapDrawable(Resources res, BitmapCache cache, boolean limitDensity)
public final RequestKey getKey()
public final RequestKey getPreviousKey()
protected ReusableBitmap getBitmap()
public void setDecodeDimensions(int width, int height)
public void setLayoutDirectionLocal(int layoutDirection)
layoutDirection
- the resolved layout direction for the drawable,
either View.LAYOUT_DIRECTION_LTR
or View.LAYOUT_DIRECTION_RTL
public void onLayoutDirectionChangeLocal(int layoutDirection)
layoutDirection
- the new resolved layout directionpublic int getLayoutDirectionLocal()
View.LAYOUT_DIRECTION_LTR
,
View.LAYOUT_DIRECTION_RTL
setLayoutDirectionLocal(int)
public void bind(RequestKey key)
mPrevKey
.
All UI operations should be called from the UI thread.public void unbind()
mPrevKey
.
All UI operations should be called from the UI thread.public void unbind(boolean temporary)
mPrevKey
.
All UI operations should be called from the UI thread.protected void setImage(RequestKey key)
protected void setBitmap(ReusableBitmap bmp)
protected void loadFileDescriptorFactory()
public void fileDescriptorFactoryCreated(RequestKey key, RequestKey.FileDescriptorFactory factory)
RequestKey.Callback
RequestKey.FileDescriptorFactory
has been created. This must be called on
the UI thread.fileDescriptorFactoryCreated
in interface RequestKey.Callback
key
- The key that the FileDescriptorFactory was created for. The callback should
check that the key has not changed.factory
- The FileDescriptorFactory to decode from. Pass null to cancel decode.protected void onDecodeFailed()
protected void decode(RequestKey.FileDescriptorFactory factory)
protected int getDecodeStrategy()
DecodeTask.DecodeOptions
.protected Executor getExecutor()
protected float getDrawVerticalCenter()
protected float getDrawVerticalOffsetMultiplier()
protected float getDecodeHorizontalCenter()
protected float getDecodeVerticalCenter()
public void draw(Canvas canvas)
Drawable
protected boolean hasBitmap()
protected void onDrawBitmap(Canvas canvas, Rect src, Rect dst)
getBitmap()
.public void setAlpha(int alpha)
Drawable
public void setColorFilter(ColorFilter cf)
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
.
setColorFilter
in class Drawable
cf
- The color filter to apply, or null
to remove the
existing color filterpublic 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 Drawable
PixelFormat
public void onDecodeBegin(RequestKey key)
DecodeTask.DecodeCallback
N.B. this method runs on the UI thread.
onDecodeBegin
in interface DecodeTask.DecodeCallback
public void onDecodeComplete(RequestKey key, ReusableBitmap result)
DecodeTask.DecodeCallback
onDecodeComplete
in interface DecodeTask.DecodeCallback
public void onDecodeCancel(RequestKey key)
DecodeTask.DecodeCallback
DecodeTask.DecodeCallback.onDecodeComplete(RequestKey, ReusableBitmap)
will not be called.onDecodeCancel
in interface DecodeTask.DecodeCallback
public void invalidateDrawable(Drawable who)
Drawable.Callback
invalidateDrawable
in interface Drawable.Callback
who
- The drawable that is requesting the update.public void scheduleDrawable(Drawable who, Runnable what, long when)
Drawable.Callback
Handler.postAtTime(Runnable, Object, long)
with
the parameters (what, who, when) to perform the
scheduling.scheduleDrawable
in interface Drawable.Callback
who
- The drawable being scheduled.what
- The action to execute.when
- The time (in milliseconds) to run. The timebase is
SystemClock.uptimeMillis()
public void unscheduleDrawable(Drawable who, Runnable what)
Drawable.Callback
Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, long)
. An implementation can
generally simply call
Handler.removeCallbacks(Runnable, Object)
with
the parameters (what, who) to unschedule the drawable.unscheduleDrawable
in interface Drawable.Callback
who
- The drawable being unscheduled.what
- The action being unscheduled.