在以 View 為基礎的版面配置中,除了 MotionEventPredictor 之外,您還需要在 InProgressStrokesView 內處理使用者的觸控輸入。
如要獲得最佳繪圖效能,請使用 InProgressStrokesView 類別的 startStroke()、addToStroke() 和 finishStroke() 方法,並傳遞 MotionEvent 物件做為輸入內容:
設定 UI 元件
如果是以 View 為基礎的版面配置,請將
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()方法中,取得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()筆劃幾乎完成。 筆劃完全處理完畢後,應用程式就能存取筆劃,前提是沒有其他筆劃正在處理中。這可確保在筆劃交給用戶端之前,所有繪圖作業都已完成。如要擷取完成的筆劃,有兩種做法:
- 在活動或 ViewModel 中實作
InProgressStrokesFinishedListener介面,並呼叫addFinishedStrokesListener,透過InProgressStrokesView註冊事件監聽器。 - 呼叫
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) } }擷取完成的筆劃後,您可以使用
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 中實作