Pen input on Surface Duo
Android pen events are handled by implementing the onTouchEvent
method in your View class. The key to making your app touch interactive is to expand your implementation of View to override the onTouchEvent to listen for touch events. The MotionEvent as an argument reports input details from the touch screen to let users interact with the screen using a pen.
Your code can detect the user is using a pen by looking at the value returned from the MotionEvent.getToolType
method. A value of TOOL_TYPE_STYLUS
indicates a pen and TOOL_TYPE_ERASER
indicates a pen held inverted with the eraser down.
Represents the pressure the user is using the pen with. It is usually between 0 to 1 (but it is not limited by the system); 0 indicates light pressure and 1 indicates strong pressure.
The value is obtained by calling the getPressure
method or calling getAxisValue
with AXIS_PRESSURE
parameter.
The orientation value starts at 0 when the pen is held pointing towards the top of the screen with the eraser part pointing at the user.
When rotating the pen, the values will be negative if the pen points to the left (tip of pen left to the eraser) and positive when pointing to the right.
The value is obtained by calling the getOrientation
method or calling getAxisValue with AXIS_ORIENTATION
parameter.
The following code snippet detects the orientation of the pen and draws an arc on the screen to match:
float orientation = ((event.getOrientation() * 57.2958f) + 90 ) % 360 ;
canvas.drawArc(oval,orientation,5.0f,true,paint);
The getButtonState
method returns a bit mask of the pressed buttons such as BUTTON_STYLUS_PRIMARY and BUTTON_STYLUS_SECONDARY which represent the buttons on the pen.