InProgressShapesView
class InProgressShapesView<ShapeSpecT : Any, InProgressShapeT : InProgressShape<ShapeSpecT, CompletedShapeT>, CompletedShapeT : Any> : FrameLayout
| kotlin.Any | ||||
| ↳ | android.view.View | |||
| ↳ | android.view.ViewGroup | |||
| ↳ | android.widget.FrameLayout | |||
| ↳ | androidx.ink.authoring.InProgressShapesView |
Displays in-progress shapes, as defined by a ShapeWorkflow, as MotionEvent user inputs are provided incrementally. This is a more generalized version of InProgressStrokesView for specialized developer needs for custom shapes. For normal usage, InProgressStrokesView is recommended. For a Jetpack Compose equivalent which also provides a default input handler, see androidx.ink.authoring.compose.InProgressShapes instead.
Summary
Public constructors |
|---|
<ShapeSpecT : Any, InProgressShapeT : InProgressShape<ShapeSpecT, CompletedShapeT>, CompletedShapeT : Any> InProgressShapesView( |
Public functions |
|
|---|---|
Unit |
addCompletedShapesListener(Add a listener to be notified when shapes are finished. |
Boolean |
addToShape(event: MotionEvent, pointerId: Int, prediction: MotionEvent?)Add |
Unit |
addToShape(Add input data from a |
Unit |
addToShape(Add input data, from a particular pointer within a |
Boolean |
cancelShape(event: MotionEvent, pointerId: Int)Cancel the corresponding in-progress shape with |
Unit |
cancelShape(shapeId: InProgressStrokeId, event: MotionEvent?)Cancel the building of a shape. |
Unit |
Cancel all in-progress shapes. |
Unit |
Removes all listeners that had previously been added with |
Unit |
Eagerly initialize rather than waiting for the first shape to be drawn. |
Boolean |
finishShape(event: MotionEvent, pointerId: Int)Finish the corresponding in-progress shape with |
Unit |
finishShape(input: StrokeInput, shapeId: InProgressStrokeId)Finish providing inputs for the shape represented by the given |
Unit |
finishShape(Complete the building of a shape, with the last input data coming from a particular pointer of a |
Map<InProgressStrokeId, CompletedShapeT> |
Returns all the finished shapes that are still being rendered by this view, with map iteration order in the z-order that the shapes are being rendered, from back to front. |
Boolean |
Returns true if there are any in-progress shapes. |
Unit |
@UiThreadStop this view from rendering the shapes with the given IDs. |
Unit |
removeCompletedShapesListener(Removes a listener that had previously been added with |
InProgressStrokeId |
startShape(Start building a shape with the provided |
InProgressStrokeId |
startShape(Start building a shape using a particular pointer within a |
Public properties |
|
|---|---|
ShapeWorkflow<ShapeSpecT, InProgressShapeT, CompletedShapeT>? |
A |
CountingIdlingResource? |
Allows a test to easily wait until all in-progress shapes are completed and handed off. |
Path? |
Denote an area of this |
Matrix |
The transform matrix to convert |
Public constructors
InProgressShapesView
<ShapeSpecT : Any, InProgressShapeT : InProgressShape<ShapeSpecT, CompletedShapeT>, CompletedShapeT : Any> InProgressShapesView(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: @AttrRes Int = 0
)
Public functions
addCompletedShapesListener
fun addCompletedShapesListener(
listener: InProgressShapesCompletedListener<CompletedShapeT>
): Unit
Add a listener to be notified when shapes are finished. These shapes will continue to be rendered within this view until removeCompletedShapes is called. All of the shapes that have been delivered to listeners but have not yet been removed with removeCompletedShapes are available through getCompletedShapes.
addToShape
fun addToShape(event: MotionEvent, pointerId: Int, prediction: MotionEvent? = null): Boolean
Add event data for pointerId to the corresponding in-progress shape, if present. The shape must have been started with an overload of startShape that accepts a MotionEvent.
| Parameters | |
|---|---|
event: MotionEvent |
The next |
pointerId: Int |
The index of the relevant pointer in the |
prediction: MotionEvent? = null |
An optional predicted |
| Returns | |
|---|---|
Boolean |
Whether the pointer corresponds to an in-progress shape. |
addToShape
fun addToShape(
inputs: StrokeInputBatch,
shapeId: InProgressStrokeId,
prediction: StrokeInputBatch = ImmutableStrokeInputBatch.EMPTY
): Unit
Add input data from a StrokeInputBatch to an existing shape. The shape must have been started with an overload of startShape that accepts a StrokeInput.
| Parameters | |
|---|---|
inputs: StrokeInputBatch |
The next |
shapeId: InProgressStrokeId |
The |
prediction: StrokeInputBatch = ImmutableStrokeInputBatch.EMPTY |
Predicted |
addToShape
fun addToShape(
event: MotionEvent,
pointerId: Int,
shapeId: InProgressStrokeId,
prediction: MotionEvent? = null
): Unit
Add input data, from a particular pointer within a MotionEvent, to an existing shape. The shape must have been started with an overload of startShape that accepts a MotionEvent.
| Parameters | |
|---|---|
event: MotionEvent |
The next |
pointerId: Int |
The identifier of the pointer within |
shapeId: InProgressStrokeId |
The |
prediction: MotionEvent? = null |
Predicted |
cancelShape
fun cancelShape(event: MotionEvent, pointerId: Int): Boolean
Cancel the corresponding in-progress shape with event data for pointerId, if present. The shape must have been started with an overload of startShape that accepts a MotionEvent.
| Parameters | |
|---|---|
event: MotionEvent |
The |
pointerId: Int |
the id of the relevant pointer in the |
| Returns | |
|---|---|
Boolean |
Whether the pointer corresponded to an in-progress shape. |
cancelShape
fun cancelShape(shapeId: InProgressStrokeId, event: MotionEvent? = null): Unit
Cancel the building of a shape. It will no longer be visible within this InProgressShapesView, and no CompletedShapeT object will come through InProgressShapesCompletedListener for the given shapeId.
This is typically done for one of three reasons:
-
A
MotionEventwithMotionEvent.getActionMaskedofMotionEvent.ACTION_CANCEL. This tends to be when an entire gesture has been canceled, for example when a parentViewusesandroid.view.ViewGroup.onInterceptTouchEventto intercept and handle the gesture itself. -
A
MotionEventwithMotionEvent.getFlagscontainingMotionEvent.FLAG_CANCELED. This tends to be when the system has detected an unintentional touch, such as from the user resting their palm on the screen while writing or drawing, after some events from that unintentional pointer have already been delivered. -
An app's business logic reinterprets a gesture previously used for inking as something else, and the earlier inking may be seen as unintentional. For example, an app that uses single-pointer gestures for inking and dual-pointer gestures for pan/zoom/rotate will start inking when the first pointer goes down, but when the second pointer goes down it may want to cancel the shape from the first pointer rather than leave the small ink marks on the screen.
Does nothing if a shape with the given shapeId is not in progress.
| Parameters | |
|---|---|
shapeId: InProgressStrokeId |
The |
event: MotionEvent? = null |
The |
cancelUnfinishedShapes
fun cancelUnfinishedShapes(): Unit
Cancel all in-progress shapes.
clearCompletedShapesListeners
fun clearCompletedShapesListeners(): Unit
Removes all listeners that had previously been added with addCompletedShapesListener.
eagerInit
fun eagerInit(): Unit
Eagerly initialize rather than waiting for the first shape to be drawn. Since initialization can be somewhat heavyweight, doing this as soon as it's likely for the user to start drawing can prevent initialization from introducing latency to the first shape.
finishShape
fun finishShape(event: MotionEvent, pointerId: Int): Boolean
Finish the corresponding in-progress shape with event data for pointerId, if present. The shape must have been started with an overload of startShape that accepts a MotionEvent. The resulting CompletedShapeT will be passed to InProgressShapesCompletedListener (registered with addCompletedShapesListener) shortly after all currently in-progress shapes are finished.
| Parameters | |
|---|---|
event: MotionEvent |
the last |
pointerId: Int |
the id of the relevant pointer in the |
| Returns | |
|---|---|
Boolean |
Whether the pointer corresponded to an in-progress shape. |
finishShape
fun finishShape(input: StrokeInput, shapeId: InProgressStrokeId): Unit
Finish providing inputs for the shape represented by the given shapeId, with the last input data coming from a StrokeInput. The shape must have been started with an overload of startShape that accepts a StrokeInput. The resulting CompletedShapeT will be passed to InProgressShapesCompletedListener (registered with addCompletedShapesListener) shortly after all currently in-progress shapes are finished.
| Parameters | |
|---|---|
input: StrokeInput |
The last |
shapeId: InProgressStrokeId |
The |
finishShape
fun finishShape(
event: MotionEvent,
pointerId: Int,
shapeId: InProgressStrokeId
): Unit
Complete the building of a shape, with the last input data coming from a particular pointer of a MotionEvent. The shape must have been started with an overload of startShape that accepts a MotionEvent.
The resulting CompletedShapeT will be passed to InProgressShapesCompletedListener (registered with addCompletedShapesListener) shortly after all currently in-progress shapes are finished.
Does nothing if a shape with the given shapeId is not in progress.
| Parameters | |
|---|---|
event: MotionEvent |
The last |
pointerId: Int |
The identifier of the pointer within |
shapeId: InProgressStrokeId |
The |
getCompletedShapes
fun getCompletedShapes(): Map<InProgressStrokeId, CompletedShapeT>
Returns all the finished shapes that are still being rendered by this view, with map iteration order in the z-order that the shapes are being rendered, from back to front. This is the same order that shapes were started with startShape. The IDs of these shapes should be passed to removeCompletedShapes when they are handed off to another view.
hasUnfinishedShapes
fun hasUnfinishedShapes(): Boolean
Returns true if there are any in-progress shapes.
removeCompletedShapes
@UiThread
fun removeCompletedShapes(strokeIds: Set<InProgressStrokeId>): Unit
Stop this view from rendering the shapes with the given IDs.
This should be called in the same UI thread run loop (HWUI frame) as when the shapes start being rendered elsewhere in the view hierarchy. This means they are saved in a location where they will be picked up in a view's next call to onDraw, and that view's invalidate method has been called. If these two operations are not done within the same UI thread run loop (usually side by side - see example below), then there will be brief rendering errors - either a visual gap where the shape is not drawn during a frame, or a double draw where the shape is drawn twice and translucent shapes appear more opaque than they should.
removeCompletedShapesListener
fun removeCompletedShapesListener(
listener: InProgressShapesCompletedListener<CompletedShapeT>
): Unit
Removes a listener that had previously been added with addCompletedShapesListener.
startShape
fun startShape(
input: StrokeInput,
shapeSpec: ShapeSpecT,
shapeToViewTransform: Matrix = IDENTITY_MATRIX
): InProgressStrokeId
Start building a shape with the provided input. This would typically be followed by many calls to addToShape, and the sequence would end with a call to either finishShape or cancelShape.
In most circumstances, the startShape overload that accepts a MotionEvent is more convenient. However, this overload using a StrokeInput is available for cases where the input data may not come directly from a MotionEvent, such as receiving events over a network connection. Using this function to start a shape can only be followed by the StrokeInput variants of addToShape and finishShape for the same shape.
If there is a way to request unbuffered dispatch from the source of the input data used here, equivalent to View.requestUnbufferedDispatch for unbuffered MotionEvent data, then be sure to request it for optimal performance.
| Parameters | |
|---|---|
input: StrokeInput |
The |
shapeSpec: ShapeSpecT |
Specification for the shape being started. |
shapeToViewTransform: Matrix = IDENTITY_MATRIX |
The |
| Returns | |
|---|---|
InProgressStrokeId |
The |
startShape
fun startShape(
event: MotionEvent,
pointerId: Int,
shapeSpec: ShapeSpecT,
motionEventToWorldTransform: Matrix = IDENTITY_MATRIX,
shapeToWorldTransform: Matrix = IDENTITY_MATRIX
): InProgressStrokeId
Start building a shape using a particular pointer within a MotionEvent. This would typically be followed by many calls to addToShape, and the sequence would end with a call to either finishShape or cancelShape.
In most circumstances, prefer to use this function over startShape that accepts a StrokeInput. Using this function to start a shape must only be followed by the MotionEvent variants of addToShape and finishShape for the same shape.
For optimum performance, it is strongly recommended to call View.requestUnbufferedDispatch using event and the View that generated event alongside calling this function. When requested this way, unbuffered dispatch mode will automatically end when the gesture is complete.
| Parameters | |
|---|---|
event: MotionEvent |
The first |
pointerId: Int |
The identifier of the pointer within |
shapeSpec: ShapeSpecT |
Specification for the shape being started. |
motionEventToWorldTransform: Matrix = IDENTITY_MATRIX |
The matrix that transforms |
shapeToWorldTransform: Matrix = IDENTITY_MATRIX |
Allows an object-specific (shape-specific) coordinate space to be defined in relation to the caller's "world" coordinate space. This defaults to the identity matrix, which is typical for many use cases at the time of shape construction. In typical use cases, shape coordinates and world coordinates may start to differ from one another after shape creation as a particular shape is manipulated within the world, e.g. it may be moved, scaled, or rotated relative to other content within an app's document. This matrix must be invertible. |
| Returns | |
|---|---|
InProgressStrokeId |
The |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if |
Protected functions
Public properties
customShapeWorkflow
var customShapeWorkflow: ShapeWorkflow<ShapeSpecT, InProgressShapeT, CompletedShapeT>?
A ShapeWorkflow to be used as an alternative to the standard Ink behavior provided by InProgressStrokesView. It must be set before the first call to startShape or eagerInit.
inProgressShapeCounter
@VisibleForTesting
var inProgressShapeCounter: CountingIdlingResource?
Allows a test to easily wait until all in-progress shapes are completed and handed off. There is no reason to set this in non-test code.
maskPath
var maskPath: Path?
Denote an area of this InProgressShapesView where no ink should be visible. A value of null indicates that shapes will be visible anywhere they are drawn. This is useful for UI elements that float on top of (in Z order) the drawing surface - without this, a user would be able to draw in-progress ("wet") shapes on top of those UI elements, but then when the shape is finished, it will appear as a dry shape underneath of the UI element. If this mask is set to the shape and position of the floating UI element, then the ink will never be rendered in that area, making it appear as if it's being drawn underneath the UI element.
This technique is most convincing when the UI element is opaque. Often there are parts of the UI element that are translucent, such as drop shadows, or anti-aliasing along the edges. The result will look a little different between wet and dry shapes for those cases, but it can be a worthwhile tradeoff compared to the alternative of drawing wet shapes on top of that UI element.
Note that this parameter does not affect the contents of the shapes at all, nor how they appear when drawn in a separate composable after InProgressShapesCompletedListener.onShapesCompleted is called - just how the shapes appear when they are still in progress in this view.
motionEventToViewTransform
var motionEventToViewTransform: Matrix
The transform matrix to convert MotionEvent coordinates, as passed to startShape, addToShape, and finishShape, into coordinates of this InProgressShapesView for rendering. Defaults to the identity matrix, for the recommended case where InProgressShapesView exactly overlays the android.view.View that has the touch listener from which MotionEvent instances are being forwarded.