描画のパフォーマンスを最適化するには、InProgressStrokesView
クラスの startStroke()
、addToStroke()
、finishStroke()
メソッドを使用し、MotionEvent
オブジェクトを入力として渡します。
UI コンポーネントを設定する
InProgressStrokesView
をビュー階層に統合します。<FrameLayout> <ScrollView android:id="@+id/my_content" android:width="match_parent" android:height="match_parent" > <!-- Your content here. --> </ScrollView> <androidx.ink.authoring.InProgressStrokesView android:id="@+id/in_progress_strokes_view" android:width="match_parent" android:height="match_parent" /> </FrameLayout>
InProgressStrokesView をインスタンス化
アクティビティまたはフラグメントの [
onCreate()
][ink-draw-include6] メソッド内で、InProgressStrokesView
への参照を取得し、ユーザー入力を管理するためのタッチリスナーを設定します。class MyActivity : View.OnTouchListener {
private lateinit var contentView: ScrollView
private lateinit var inProgressStrokesView: InProgressStrokesView
private lateinit var predictor: MotionEventPredictor
// ... other variables
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
predictor = MotionEventPredictor.newInstance(contentView)
contentView = findViewById(R.id.my_content)
contentView.setOnTouchListener(touchListener)
inProgressStrokesView = findViewById(R.id.in_progress_strokes_view)
}
// ... (touchListener implementation)
}タップイベントを処理する
UI コンポーネントを設定したので、タップイベントに基づいて描画を開始できます。
MotionEvent
アクションInProgressStrokesView
メソッド説明
ストロークのレンダリングを開始
ストロークのレンダリングを続行する
ストロークのレンダリングを確定
パーム リジェクションを実装してストロークをキャンセルする
class MyActivity : View.OnTouchListener { private lateinit var contentView: ScrollView private lateinit var inProgressStrokesView: InProgressStrokesView private var pointerId = -1 private var strokeId: InProgressStrokeId? = null private lateinit var predictor: MotionEventPredictor override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) contentView = findViewById(R.id.my_content) predictor = MotionEventPredictor.create(contentView) contentView.setOnTouchListener(touchListener) inProgressStrokesView = findViewById(R.id.in_progress_strokes_view) } private val touchListener = { view: View, event: MotionEvent -> predictor.record(event) when (event.actionMasked) { MotionEvent.ACTION_DOWN -> { // First pointer - treat it as inking. view.requestUnbufferedDispatch(event) val pointerIndex = event.actionIndex pointerIdToStrokeId[event.getPointerId(pointerIndex)] = inProgressStrokesView.startStroke(event, pointerId) return true } MotionEvent.ACTION_POINTER_DOWN -> { val stroke = strokeId ?: return false inProgressStrokesView.cancelStroke(stroke, event) strokeId = null pointerId = -1 return false } MotionEvent.ACTION_MOVE -> { val predictedEvent = predictor.predict() try { for (pointerIndex in 0 until pointerCount) { val strokeId = pointerIdToStrokeId[event.getPointerId(pointerIndex)] ?: continue inProgressStrokesView.addToStroke(event, pointerId, strokeId, predictedEvent) } finally { predictedEvent?.recycle() } } } MotionEvent.ACTION_UP -> { val pointerIndex = event.actionIndex val strokeId = pointerIdToStrokeId[event.getPointerId(pointerIndex)] ?: return false inProgressStrokesView.finishStroke(event, pointerId, strokeId) return true } MotionEvent.ACTION_CANCEL -> { val pointerIndex = event.actionIndex val strokeId = pointerIdToStrokeId[event.getPointerId(pointerIndex)] ?: return false inProgressStrokesView.cancelStroke(strokeId, event) return true } } return false } }
終了したストロークを処理する
finishStroke()
を呼び出すと、ストロークが完了としてマークされます。ただし、確定プロセスは即時ではありません。ストロークはfinishStroke()
が呼び出された直後に完全に処理され、アプリからアクセスできるようになります(特に、他のストロークが進行していない場合)。これにより、ストロークが完了としてクライアントに渡される前に、すべての描画オペレーションが完了します。完了したストロークを取得するには、次の 2 つの方法があります。
- アクティビティまたは ViewModel 内に
InProgressStrokesFinishedListener
インターフェースを実装し、InProgressStrokesView
にリスナーを登録します。addFinishedStrokesListener
InProgressStrokesView
のgetFinishedStrokes()
メソッドを使用して、完成したすべてのストロークを直接取得します。
class MyActivity : ComponentActivity(), InProgressStrokesFinishedListener {
...
private val finishedStrokesState = mutableStateOf(emptySet<Stroke>())
override fun onCreate(savedInstanceState: Bundle?) {
...
inProgressStrokesView.addFinishedStrokesListener(this)
}
// ... (handle touch events)
@UiThread
override fun onStrokesFinished(strokes: Map<InProgressStrokeId, Stroke>) {
finishedStrokesState.value += strokes.values
inProgressStrokesView.removeFinishedStrokes(strokes.keys)
}
}完成したストロークを取得したら、
CanvasStrokeRenderer
上に構築された上位レベルの抽象化としてViewStrokeRenderer
を使用できます。これにより、ビュー階層内のレンダリング プロセスをさらに簡素化できます。class DrawingView(context: Context) : View(context) {
private val viewStrokeRenderer = ViewStrokeRenderer(myCanvasStrokeRenderer, this)
override fun onDraw(canvas: Canvas) {
viewStrokeRenderer.drawWithStrokes(canvas) { scope ->
canvas.scale(myZoomLevel)
canvas.rotate(myRotation)
canvas.translate(myPanX, myPanY)
scope.drawStroke(myStroke)
// Draw other objects including more strokes, apply more transformations, ...
}
}
}- アクティビティまたは ViewModel 内に