ComplicationDrawable
class ComplicationDrawable : Drawable
kotlin.Any | ||
↳ | android.graphics.drawable.Drawable | |
↳ | androidx.wear.watchface.complications.rendering.ComplicationDrawable |
A styleable drawable object that draws complications. You can create a ComplicationDrawable from XML inflation or by using one of the constructor methods.
Constructing a ComplicationDrawable
To construct a ComplicationDrawable programmatically, use the
constructor. Afterwards, styling attributes you want to modify can be set via set methods.
public void onCreate(SurfaceHolder holder) { ... ComplicationDrawable complicationDrawable = new ComplicationDrawable(WatchFaceService.this); complicationDrawable.setBackgroundColorActive(backgroundColor); complicationDrawable.setTextColorActive(textColor); ... }
Constructing a ComplicationDrawable from XML
Constructing a ComplicationDrawable from an XML file makes it easier to modify multiple styling attributes at once without calling any set methods. You may also use different XML files to switch between different styles your watch face supports.
To construct a ComplicationDrawable from a drawable XML file, you may create an XML file in your project's res/drawable
folder. A ComplicationDrawable with red text and white title in active mode, and white text and white title in ambient mode would look like this:
<?xml version="1.0" encoding="utf-8"?> <android.support.wearable.complications.rendering.ComplicationDrawable xmlns:app="http://schemas.android.com/apk/res-auto" app:textColor="#FFFF0000" app:titleColor="#FFFFFFFF"> <ambient app:textColor="#FFFFFFFF" /> </android.support.wearable.complications.rendering.ComplicationDrawable>
A top-level drawable
tag with the class
attribute may also be used to construct a ComplicationDrawable from an XML file:
<?xml version="1.0" encoding="utf-8"?> <drawable class="android.support.wearable.complications.rendering.ComplicationDrawable" xmlns:app="http://schemas.android.com/apk/res-auto" app:textColor="#FFFF0000" app:titleColor="#FFFFFFFF"> <ambient app:textColor="#FFFFFFFF" /> </drawable>
To inflate a ComplicationDrawable from XML file, use the getDrawable(Context, int)
method. ComplicationDrawable needs access to the current context in order to style and draw the complication.
public void onCreate(SurfaceHolder holder) { ... ComplicationDrawable complicationDrawable = (ComplicationDrawable) getDrawable(R.drawable.complication); complicationDrawable.setContext(WatchFaceService.this); ... }
Syntax:
<?xml version="1.0" encoding="utf-8"?> <android.support.wearable.complications.rendering.ComplicationDrawable xmlns:app="http://schemas.android.com/apk/res-auto" app:backgroundColor="color" app:backgroundDrawable="drawable" app:borderColor="color" app:borderDashGap="dimension" app:borderDashWidth="dimension" app:borderRadius="dimension" app:borderStyle="none|solid|dashed" app:borderWidth="dimension" app:highlightColor="color" app:iconColor="color" app:rangedValuePrimaryColor="color" app:rangedValueProgressHidden="boolean" app:rangedValueRingWidth="dimension" app:rangedValueSecondaryColor="color" app:textColor="color" app:textSize="dimension" app:textTypeface="string" app:titleColor="color" app:titleSize="dimension" app:titleTypeface="string"> <ambient app:backgroundColor="color" app:backgroundDrawable="drawable" app:borderColor="color" app:borderDashGap="dimension" app:borderDashWidth="dimension" app:borderRadius="dimension" app:borderStyle="none|solid|dashed" app:borderWidth="dimension" app:highlightColor="color" app:iconColor="color" app:rangedValuePrimaryColor="color" app:rangedValueRingWidth="dimension" app:rangedValueSecondaryColor="color" app:textColor="color" app:textSize="dimension" app:textTypeface="string" app:titleColor="color" app:titleSize="dimension" app:titleTypeface="string" /> </android.support.wearable.complications.rendering.ComplicationDrawable>
Attributes of the top-level tag apply to both active and ambient modes while attributes of the inner ambient
tag only apply to ambient mode. As an exception, top-level only rangedValueProgressHidden
attribute applies to both modes, and cannot be overridden in ambient mode. To hide ranged value in only one of the active or ambient modes, you may consider setting rangedValuePrimaryColor
and rangedValueSecondaryColor
to
instead.
Drawing a ComplicationDrawable
Depending on the size and shape of the bounds, the layout of the complication may change. For instance, a short text complication with an icon that is drawn on square bounds would draw the icon above the short text, but a short text complication with an icon that is drawn on wide rectangular bounds might draw the icon to the left of the short text instead.
Summary
Public constructors | |
---|---|
<init>() Default constructor. |
|
Creates a ComplicationDrawable using the given context. |
|
<init>(@NonNull drawable: ComplicationDrawable) |
Public methods | |
---|---|
Unit |
Draws the complication for the last known time. |
ComplicationStyle |
Returns complication style for active mode. |
ComplicationStyle |
Returns complication style for ambient mode. |
ComplicationData? |
Returns the |
ComplicationRenderer? |
Returns complication renderer. |
Context? |
Returns the |
Long |
Returns the time in milliseconds since the epoch used for rendering |
static ComplicationDrawable? |
getDrawable(@NonNull context: Context, id: Int) Creates a ComplicationDrawable from a resource. |
Long |
Returns the highlight duration. |
CharSequence? | |
Int | |
Unit |
inflate(@NonNull r: Resources, @NonNull parser: XmlPullParser, @NonNull attrs: AttributeSet, @Nullable theme: Theme?) Inflates this ComplicationDrawable from an XML resource. |
Boolean |
Whether the complication, when rendering in ambient mode, should apply a style suitable for display on devices with burn in protection. |
Boolean |
Returns whether the complication is currently highlighted. |
Boolean |
Returns whether the complication is rendered in ambient mode. |
Boolean |
Returns whether the complication, when rendering in ambient mode, should apply a style suitable for low bit ambient mode. |
Boolean |
Returns |
Boolean |
Sends the tap action for the complication if tap coordinates are inside the complication bounds. |
Unit |
Does nothing. |
Unit |
setBurnInProtection(burnInProtection: Boolean) Sets whether the complication, when rendering in ambient mode, should apply a style suitable for display on devices with burn in protection. |
Unit |
setColorFilter(@Nullable colorFilter: ColorFilter?) Does nothing. |
Unit |
setComplicationData(@Nullable complicationData: ComplicationData?, loadDrawablesAsync: Boolean) Sets the complication data to be drawn. |
Unit |
setContext(@NonNull context: Context) Sets the context used to render the complication. |
Unit |
setCurrentTimeMillis(currentTimeMillis: Long) Sets the current time in mulliseconds since the epoch. |
Unit |
setHighlightDuration(@IntRange(0) highlightDurationMillis: Long) Sets the duration for the complication to stay highlighted after calling the |
Unit |
setHighlighted(isHighlighted: Boolean) Sets whether the complication is currently highlighted. |
Unit |
setInAmbientMode(inAmbientMode: Boolean) Sets whether the complication should be rendered in ambient mode. |
Unit |
setLowBitAmbient(lowBitAmbient: Boolean) Sets whether the complication, when rendering in ambient mode, should apply a style suitable for low bit ambient mode. |
Unit |
setNoDataText(@Nullable noDataText: CharSequence?) Sets the text to be rendered when |
Unit |
setRangedValueProgressHidden(rangedValueProgressHidden: Boolean) Sets if the ranged value progress should be hidden when |
Protected methods | |
---|---|
Unit |
onBoundsChange(@NonNull bounds: Rect) |
Public constructors
<init>
ComplicationDrawable()
Default constructor.
<init>
ComplicationDrawable(@NonNull context: Context)
Creates a ComplicationDrawable using the given context. If this constructor is used, calling setContext(Context)
may not be necessary.
<init>
ComplicationDrawable(@NonNull drawable: ComplicationDrawable)
Public methods
draw
fun draw(@NonNull canvas: Canvas): Unit
Draws the complication for the last known time. Last known time is derived from ComplicationDrawable#setCurrentTimeMillis(long)}.
Parameters | |
---|---|
canvas |
Canvas: Canvas for the complication to be drawn onto |
getActiveStyle
@NonNull fun getActiveStyle(): ComplicationStyle
Returns complication style for active mode.
getAmbientStyle
@NonNull fun getAmbientStyle(): ComplicationStyle
Returns complication style for ambient mode.
getComplicationData
@Nullable fun getComplicationData(): ComplicationData?
Returns the ComplicationData
to be drawn by this ComplicationDrawable.
getComplicationRenderer
@Nullable fun getComplicationRenderer(): ComplicationRenderer?
Returns complication renderer.
getContext
@Nullable fun getContext(): Context?
Returns the Context
used to render the complication.
getCurrentTimeMillis
fun getCurrentTimeMillis(): Long
Returns the time in milliseconds since the epoch used for rendering ComplicationData
with time dependent text.
getDrawable
@Nullable static fun getDrawable(
@NonNull context: Context,
id: Int
): ComplicationDrawable?
Creates a ComplicationDrawable from a resource.
Parameters |
---|