11package com.trydroid.coboard.views
22
33import android.content.Context
4- import android.graphics.Canvas
5- import android.graphics.Color
6- import android.graphics.Paint
7- import android.graphics.Path
4+ import android.graphics.*
85import android.util.AttributeSet
96import android.view.MotionEvent
107import android.view.View
118
12- typealias OnDrawListener = (List <SimpleDrawView . Point >? ) -> Unit
9+ typealias OnDrawListener = (List <Point >? ) -> Unit
1310
1411/* *
1512 * Customize form original source
1613 * - https://github.com/johncarl81/androiddraw/blob/master/src/main/java/org/example/androiddraw/SimpleDrawView.java
1714 * - https://github.com/ByoxCode/DrawView/blob/master/drawview/src/main/java/com/byox/drawview/views/DrawView.java
1815 * */
1916class SimpleDrawView (context : Context , attributeSet : AttributeSet ) : View(context, attributeSet), View.OnTouchListener {
20- private val mPointList : MutableList <MutableList <Point >> = mutableListOf ()
17+ private val mLineHistoryList : MutableList <MutableList <Point >> = mutableListOf ()
2118 private val mPaint: Paint by lazy {
2219 Paint (Paint .ANTI_ALIAS_FLAG ).apply {
2320 strokeWidth = STROKE_WIDTH
@@ -36,24 +33,21 @@ class SimpleDrawView(context: Context, attributeSet: AttributeSet) : View(contex
3633
3734 override fun onDraw (canvas : Canvas ) {
3835 val path = Path ()
39- mPointList.forEach { pointHistory ->
40- pointHistory.forEachIndexed { index, point ->
36+ mLineHistoryList.forEach { line ->
37+ line.forEachIndexed { index, point ->
38+ val x = point.x.toFloat()
39+ val y = point.y.toFloat()
4140 if (index == 0 ) {
42- path.moveTo(point. x, point. y)
41+ path.moveTo(x, y)
4342 }
4443 else {
45- path.lineTo(point. x, point. y)
44+ path.lineTo(x, y)
4645 }
4746 }
4847 canvas.drawPath(path, mPaint)
4948 }
5049 }
5150
52- fun clear () {
53- mPointList.clear()
54- invalidate()
55- }
56-
5751 override fun onTouch (view : View , event : MotionEvent ): Boolean {
5852 when (event.action) {
5953 MotionEvent .ACTION_DOWN -> {
@@ -70,27 +64,31 @@ class SimpleDrawView(context: Context, attributeSet: AttributeSet) : View(contex
7064 return true
7165 }
7266
73- private fun onTouchStart () {
74- mPointList.add(mutableListOf ())
67+ fun clear () {
68+ mLineHistoryList.clear()
69+ invalidate()
7570 }
7671
77- private fun onTouchMove (event : MotionEvent ) {
78- mPointList.last()
79- .add(Point (x = event.x,
80- y = event.y))
72+ fun drawLine (lineList : List <Point >) {
73+ mLineHistoryList.add(lineList.toMutableList())
8174 invalidate()
8275 }
8376
84- private fun onTouchEnd () {
85- drawListener?.invoke(mPointList.lastOrNull ())
77+ private fun onTouchStart () {
78+ mLineHistoryList.add( mutableListOf ())
8679 }
8780
88- inner class Point (val x : Float = 0f ,
89- val y : Float = 0f ) {
90-
91- override fun toString (): String {
92- return " $x ,$y "
81+ private fun onTouchMove (event : MotionEvent ) {
82+ val point = Point ().apply {
83+ x = event.x.toInt()
84+ y = event.y.toInt()
9385 }
86+ mLineHistoryList.last().add(point)
87+ invalidate()
88+ }
89+
90+ private fun onTouchEnd () {
91+ drawListener?.invoke(mLineHistoryList.lastOrNull())
9492 }
9593
9694 companion object {
0 commit comments