在基于视图 (view-based) 的布局中,除了需要处理 MotionEventPredictor 之外,还需要在 InProgressStrokesView 中处理用户的触摸输入。
为了实现最佳的绘制性能,请使用 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在您的 Activity 或 Fragment 的
onCreate()方法中,获取InProgressStrokesView的引用并设置触摸监听器以管理用户输入。在您的 Activity 或 Fragment 的 [
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()之后,笔画即将完成。当没有其他正在进行的笔画时,该笔画将得到完全处理并可供您的应用程序访问。这可确保所有绘制操作在将笔画移交给客户端之前完成。要检索已完成的笔画,有两种方法
- 在您的 Activity 或 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, ... } } }- 在您的 Activity 或 ViewModel 中实现