| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.android.test.handwritingime; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.graphics.Canvas; |
| 21 |
import android.graphics.Color; |
| 22 |
import android.graphics.Insets; |
| 23 |
import android.graphics.Paint; |
| 24 |
import android.graphics.Path; |
| 25 |
import android.view.MotionEvent; |
| 26 |
import android.view.View; |
| 27 |
import android.view.ViewGroup; |
| 28 |
import android.view.WindowInsets; |
| 29 |
import android.view.WindowManager; |
| 30 |
import android.view.WindowMetrics; |
| 31 |
|
| 32 |
class InkView extends View { |
| 33 |
private static final long FINISH_TIMEOUT = 2500; |
| 34 |
private final HandwritingIme.HandwritingFinisher mHwCanceller; |
| 35 |
private final HandwritingIme.StylusConsumer mConsumer; |
| 36 |
private final int mTopInset; |
| 37 |
private Paint mPaint; |
| 38 |
private Path mPath; |
| 39 |
private float mX, mY; |
| 40 |
private static final float STYLUS_MOVE_TOLERANCE = 1; |
| 41 |
private Runnable mFinishRunnable; |
| 42 |
|
| 43 |
InkView(Context context, HandwritingIme.HandwritingFinisher hwController, |
| 44 |
HandwritingIme.StylusConsumer consumer) { |
| 45 |
super(context); |
| 46 |
mHwCanceller = hwController; |
| 47 |
mConsumer = consumer; |
| 48 |
|
| 49 |
mPaint = new Paint(); |
| 50 |
mPaint.setAntiAlias(true); |
| 51 |
mPaint.setDither(true); |
| 52 |
mPaint.setColor(Color.GREEN); |
| 53 |
mPaint.setStyle(Paint.Style.STROKE); |
| 54 |
mPaint.setStrokeJoin(Paint.Join.ROUND); |
| 55 |
mPaint.setStrokeCap(Paint.Cap.ROUND); |
| 56 |
mPaint.setStrokeWidth(14); |
| 57 |
|
| 58 |
mPath = new Path(); |
| 59 |
|
| 60 |
WindowManager wm = context.getSystemService(WindowManager.class); |
| 61 |
WindowMetrics metrics = wm.getCurrentWindowMetrics(); |
| 62 |
Insets insets = metrics.getWindowInsets() |
| 63 |
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()); |
| 64 |
setLayoutParams(new ViewGroup.LayoutParams( |
| 65 |
metrics.getBounds().width() - insets.left - insets.right, |
| 66 |
metrics.getBounds().height() - insets.top - insets.bottom)); |
| 67 |
mTopInset = insets.top; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
protected void onDraw(Canvas canvas) { |
| 72 |
super.onDraw(canvas); |
| 73 |
|
| 74 |
canvas.drawPath(mPath, mPaint); |
| 75 |
canvas.drawARGB(20, 255, 50, 50); |
| 76 |
} |
| 77 |
|
| 78 |
private void stylusStart(float x, float y) { |
| 79 |
y = y - mTopInset; |
| 80 |
mPath.moveTo(x, y); |
| 81 |
mX = x; |
| 82 |
mY = y; |
| 83 |
} |
| 84 |
|
| 85 |
private void stylusMove(float x, float y) { |
| 86 |
y = y - mTopInset; |
| 87 |
float dx = Math.abs(x - mX); |
| 88 |
float dy = Math.abs(y - mY); |
| 89 |
if (mPath.isEmpty()) { |
| 90 |
stylusStart(x, y); |
| 91 |
} |
| 92 |
if (dx >= STYLUS_MOVE_TOLERANCE || dy >= STYLUS_MOVE_TOLERANCE) { |
| 93 |
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); |
| 94 |
mX = x; |
| 95 |
mY = y; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
private void stylusFinish() { |
| 100 |
mPath.lineTo(mX, mY); |
| 101 |
|
| 102 |
mPath.reset(); |
| 103 |
mX = 0; |
| 104 |
mY = 0; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
@Override |
| 110 |
public boolean onTouchEvent(MotionEvent event) { |
| 111 |
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) { |
| 112 |
mConsumer.onStylusEvent(event); |
| 113 |
android.util.Log.w(HandwritingIme.TAG, "INK touch onStylusEvent " + event); |
| 114 |
float x = event.getX(); |
| 115 |
float y = event.getY(); |
| 116 |
switch (event.getAction()) { |
| 117 |
case MotionEvent.ACTION_DOWN: |
| 118 |
cancelTimer(); |
| 119 |
stylusStart(x, y); |
| 120 |
invalidate(); |
| 121 |
break; |
| 122 |
case MotionEvent.ACTION_MOVE: |
| 123 |
stylusMove(x, y); |
| 124 |
invalidate(); |
| 125 |
break; |
| 126 |
|
| 127 |
case MotionEvent.ACTION_UP: |
| 128 |
scheduleTimer(); |
| 129 |
break; |
| 130 |
|
| 131 |
} |
| 132 |
return true; |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
private void cancelTimer() { |
| 138 |
if (mFinishRunnable != null) { |
| 139 |
if (getHandler() != null) { |
| 140 |
getHandler().removeCallbacks(mFinishRunnable); |
| 141 |
} |
| 142 |
mFinishRunnable = null; |
| 143 |
} |
| 144 |
if (getHandler() != null) { |
| 145 |
getHandler().removeCallbacksAndMessages(null); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
private void scheduleTimer() { |
| 150 |
cancelTimer(); |
| 151 |
if (getHandler() != null) { |
| 152 |
postDelayed(getFinishRunnable(), FINISH_TIMEOUT); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private Runnable getFinishRunnable() { |
| 157 |
mFinishRunnable = () -> { |
| 158 |
android.util.Log.e(HandwritingIme.TAG, "Hw view timer finishHandwriting "); |
| 159 |
mHwCanceller.finish(); |
| 160 |
stylusFinish(); |
| 161 |
mPath.reset(); |
| 162 |
invalidate(); |
| 163 |
}; |
| 164 |
|
| 165 |
return mFinishRunnable; |
| 166 |
} |
| 167 |
|
| 168 |
} |