Pen and ink SDK (alpha)

Important

This article describes functionality and guidance that is in public preview and may be substantially modified before it's generally available. Microsoft makes no warranties, express or implied, with respect to the information provided here.

The InkView control can be added to a view and will accept touch input, including additional attributes available when the customer is using a pen.

Configure your project and module

In your project build.gradle, under allprojects > repositories make sure you have:

mavenCentral()

In your module build.gradle, under dependencies, add the ink SDK:

implementation "com.microsoft.device:ink:1.0.0-alpha2"

Also in your module build.gradle, under android > defaultConfig, ensure the minSdkVersion is set to API 29 (or newer):

minSdkVersion 29

Add to layout

In the root element of your layout XML add an InkView namespace:

xmlns:InkView="http://schemas.android.com/apk/res-auto"

Add the control com.microsoft.device.ink.InkView in your layout file:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
    <com.microsoft.device.ink.InkView
        android:layout_width="400dp"
        android:layout_height="100dp" />
</FrameLayout>

The FrameLayout is there to provide a different background color as the InkView has a transparent background.

Run the code

At this point you have an ink enabled page. You can run the code and use the pen or a finger on the InkView area.

InkView API

Property Description
color Set the pen color (e.g., Color.RED).
enablePressure Detect different pressure values (if available).
dynamicPaintHandler Set a custom handler that implements DynamicPaintHandler.
maxStrokeWidth Set the maximum stroke width (when pressure is 1).
minStrokeWidth Set the minimum stroke width (when pressure is 0).
Method Description
clearInk() Clear all the pen ink.
saveBitmap() Generate a bitmap from the pen ink.

DynamicPaintHandler interface

Implement this interface when you want to control the stroke paint every time the ink view is painting a new segment. Using this interface will have performance implications as the ink view will call your code every time it gets a new input.

import com.microsoft.device.ink.InkView.DynamicPaintHandler
Method Description
generatePaintFromPenInfo(penInfo: InputManager.PenInfo): Paint This method will be called by the InkView every time it gets a new segment of ink to be painted. InkView will use the Paint object returned to paint the next segment of ink.

InputManager.PenInfo class

Property Type Description
pointerType PointerType Type of the pointer: MOUSE, FINGER, PEN_TIP, PEN_ERASER, UNKNOWN.
x Float x coordinate of the pen on the surface.
y Float y coordinate of the pen on the surface.
pressure Float The pressure applied by the pen on the screen. See the Android documentation.
orientation Float The direction in which the stylus is pointing. See the Android documentation.
tilt Float The angle of the pen in radians. See the Android documentation.
primaryButtonState Boolean True if the primary button is pressed, False if not.
secondaryButtonState Boolean True if the secondary button is pressed, False if not.

Sample

This sample project is available on GitHub:

Pen ink sample

Source and feedback

Visit the source on GitHub to raise issues or provide feedback.