Program Resource

Resource libraries for programmers and developers

Device like Samsung Galaxy Note, some Android device supports stylus input. Those stylus uses Electro-Magnetic Resonance technology, and able to distinguish stylus and finger, and also provides pressure info, and some pen supports eraser mode.

If you have stylus supported device, you can try Stylus Tester for Android to see what kind of data it gives (as 05/07/2013, app is not Android 4.0 ready).

It isn’t hard for app to support stylus; you just need to add some flag checker in onTouchEvent function.

Below is simple sample code.

@SuppressLint("NewApi")
@Override
public boolean onTouchEvent(MotionEvent e) {

	boolean sw_androidoverfour;
	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
		sw_androidoverfour = false;
	else
		sw_androidoverfour = true;

	if (e.getAction() == MotionEvent.ACTION_DOWN) {
		if (sw_androidoverfour){ //for android 4.0+
			if (e.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS){ //stylus
				//e.getPressure() for pressure, range 0 to 1, float
			else if (e.getToolType(0) == MotionEvent.TOOL_TYPE_ERASER){ //stylus eraser
			}else{ //finger
			}
		}else{
			if (e.getMetaState() == 512){ //stylus
			}else if (e.getMetaState() == 1024){ //stylus eraser
			}else{ //finger
			}
		}
		return true;
	} else if (e.getAction() == MotionEvent.ACTION_UP) {
		if (sw_androidoverfour){ //for android 4.0+
			if (e.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS){ //stylus
			}else if (e.getToolType(0) == MotionEvent.TOOL_TYPE_ERASER){ //stylus eraser
			}else{ //finger
			}
		}else{
			if (e.getMetaState() == 512){ //stylus
			}else if (e.getMetaState() == 1024){ //stylus eraser
			}else{ //finger
			}
		}
		return true;
	} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
		if (/*use history for higher density input*/){
			int N = e.getHistorySize();
			for (int i=0; i<=N; i++) {
				int ex,ey;
				if (i == N){
					ex = (int)e.getX();
					ey = (int)e.getY();
				}else{
					ex = (int)e.getHistoricalX(i);
					ey = (int)e.getHistoricalY(i);
				}
			}
			//use ex and ey for position
			if (sw_androidoverfour){ //for android 4.0+
				if (e.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS){ //stylus
				}else if (e.getToolType(0) == MotionEvent.TOOL_TYPE_ERASER){ //stylus eraser
				}else{ //finger
				}
			}else{
				if (e.getMetaState() == 512){ //stylus
				}else if (e.getMetaState() == 1024){ //stylus eraser
				}else{ //finger
				}
			}
		}else{
			if (sw_androidoverfour){ //for android 4.0+
				if (e.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS){ //stylus
				}else if (e.getToolType(0) == MotionEvent.TOOL_TYPE_ERASER){ //stylus eraser
				}else{ //finger
				}
			}else{
				if (e.getMetaState() == 512){ //stylus
				}else if (e.getMetaState() == 1024){ //stylus eraser
				}else{ //finger
				}
			}
		}
		return true;
	}
}

For android after 4.0, e.getToolType is used to check stylus, and e.getMetaState for older android version. Build.VERSION.SDK_INT is used to check OS version.

e.getToolType will cause unknown function when building target with older android platform, so SuppressLint is added before function to suppress error.

If e.getToolType is TOOL_TYPE_STYLUS then stylus, TOOL_TYPE_ERASER for erasor, and else, finger. e.getMetaState is 512 for stylus, 1024 for eraser, and else, finger. When stylus or eraser, e.getPressure returns pressure.

Pressure is floating value between 0 to 1, but this varies depending on device or stylus used. As I checked, Galaxy Note with attached stylus pen returns pressure between 0 to 0.6, and other compatible stylus pen returned 0 to 1.0. When developing drawing app, it would be better to give calibration option by letting user to test write, like Photoshop or Painter.

In above sample code, e.getHistorySize code is included. This is, if more detailed stylus coordinate path info is available than ACTION_MOVE interval, you can get them by calling getHistoricalX or getHistoricalY.

Print Friendly, PDF & Email

This post is also available in: Japanese

Handle stylus, eraser, and pressure with supported device (Android 4.0 support)」 に2件のコメント

  1. Siddharth says:

    What is cdata?

Leave a Reply

Your email address will not be published. Required fields are marked *


*

CAPTCHA