18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.example.smart.ui.icondraw
 
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Point
import android.graphics.PointF
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.basecore.util.ScreenUtil
import com.example.basecore.util.dp2px
 
/**
 * @author: dai hang
 * @data: 2023/12/20 14:34
 * @description:
 */
class DrawView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr) {
 
    companion object {
        const val shape_none = "none"
        const val shape_round = "round"
        const val shape_rect = "rect"
    }
 
    private var list = ArrayList<RectF>()
    private var drawingRect = RectF()
 
    private var drawShape = shape_rect
 
 
    private var strokeWidth = 3
    private val paint: Paint
 
    private var touchPoint: PointF
    private val movePoint: PointF
 
    init {
        paint = Paint()
        touchPoint = PointF()
        movePoint = PointF()
 
        strokeWidth = context.dp2px(3)
        paint.strokeWidth = strokeWidth.toFloat()
        paint.color = Color.RED
        paint.isAntiAlias = true
        paint.style = Paint.Style.STROKE
    }
 
    fun updateShape(shape: String) {
        drawShape = shape
        invalidate()
    }
 
    fun isEdit(): Boolean {
        return list.isNotEmpty()
    }
 
    fun clear() {
        drawShape = shape_none
        drawingRect.setEmpty()
        list.clear()
        invalidate()
    }
 
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (drawShape == shape_none) {
            return super.onTouchEvent(event)
        }
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                touchPoint.x = event.x
                touchPoint.y = event.y
            }
 
            MotionEvent.ACTION_MOVE -> {
                movePoint.x = event.x
                movePoint.y = event.y
 
                val startX = touchPoint.x.coerceAtMost(movePoint.x)
                val startY = touchPoint.y.coerceAtMost(movePoint.y)
                val endX = touchPoint.x.coerceAtLeast(movePoint.x)
                val endY = touchPoint.y.coerceAtLeast(movePoint.y)
 
                drawingRect.set(startX, startY, endX, endY)
                invalidate()
            }
 
            MotionEvent.ACTION_UP -> {
                list.add(RectF(drawingRect))
                drawingRect.setEmpty()
                invalidate()
            }
        }
        return true
    }
 
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        super.onDraw(canvas)
        for (rect in list) {
            canvas?.drawRect(rect, paint)
        }
        when (drawShape) {
            shape_rect -> {
                if (!drawingRect.isEmpty) {
                    canvas?.drawRect(drawingRect, paint)
                }
            }
 
            shape_round -> {
 
            }
 
            else -> {
 
            }
        }
    }
 
//    override fun onDraw(canvas: Canvas?) {
//
//    }
}