PenSDKの利用について、公式のリファレンスがあるのですが、なかなかわかりにくいので情報を整理してみたいと思います。
でサンプルファイルも配布されているのだけど、サンプルファイルから移植を行う形で、制作をしてみます。
ライブラリにファイルを追加して、下記のようなにする
compile(name:'pen-v4.1.1_light', ext:'aar')
compile files('libs/sdk-v1.0.0.jar')
で、gradleファイルの中に
repositories {
flatDir {
dirs 'libs'
}
}
の内容も追記しておかないと動作をしなかった。また、パーミッションについても、
を許可しておくことで、ペンのSDKを使えるようになった。
Spen spenPackage = new Spen();
try {
spenPackage.initialize(this);
} catch (Exception e1) {
Toast.makeText(context, "Cannot initialize Spen.", Toast.LENGTH_SHORT).show();
}
これは特には何かに使うのではなく、利用を宣言する為に呼ぶ、Spenクラスだけど、ペン自体じゃないので注意。ペン自体のクラスはSpenPenっていうクラスで使う。ただ、ペンを作る時にペンは何を利用したペンか?っていうのを確定させたいので、
SpenPenManager mPenManager = new SpenPenManager(context);
SpenPenInfo penInfo = new SpenPenInfo();
List<SpenPenInfo> penInfoList = mPenManager.getPenInfoList();
for (SpenPenInfo info : penInfoList) {
if (info.name.equalsIgnoreCase("InkPen")) {
penInfo = info;
break;
}
}
ペンの情報を取得するためにSpenManagerクラスから、ペンのクラスを取得する。
ためしたところ取得できるペンが
FountainPen
InkPen
ObliquePen
Pencil
で、Pencilは同じ太さなのだけど、
FountainPen InkPenは筆圧の取得をしてくれるペン
ObliquePenは平たいペン(?)書く方向によって太さが異なるペン
が設定できた。
この設定の値を用いて、Spenpenを作ると
SpenPen mPen = null;
try {
mPen = mPenManager.createPen(penInfo);
} catch (Exception e) {
Toast.makeText(context, "SpenPenManager is not loaded.", Toast.LENGTH_SHORT).show();
}
mPen.setSize(20);
mPen.setColor(Color.RED);
でペンが完成した。
このペンは例えばViewに対してだと
protected class MyView extends View {
private final RectF bitmapRect = new RectF();
public MyView(Context context, int w, int h) {
super(context);
createBitmap(w, h);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mPen.draw(event, new RectF());
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// Draw the bitmap of a pen on the canvas.
canvas.drawBitmap(mBitmap, null, bitmapRect, null);
super.onDraw(canvas);
}
private void createBitmap(int w, int h) {
// Create a bitmap and set the bitmap to Pen to draw a pen.
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmapRect.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
mPen.setBitmap(mBitmap);
}
}