fix
lmw
2025-03-04 449bdb5d2b5bf7b272ca5cda4c066f9a65040064
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.lzf.easyfloat.widget
 
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import com.lzf.easyfloat.R
import com.lzf.easyfloat.interfaces.OnTouchRangeListener
import com.lzf.easyfloat.utils.DisplayUtils
 
/**
 * @author: liuzhenfeng
 * @date: 2020/10/25  11:16
 * @Package: com.lzf.easyfloat.widget
 * @Description:
 */
class DefaultCloseView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseSwitchView(context, attrs, defStyleAttr) {
 
    private var normalColor = Color.parseColor("#99000000")
    private var inRangeColor = Color.parseColor("#99FF0000")
    private var shapeType = 0
 
    private lateinit var paint: Paint
    private var path = Path()
    private var width = 0f
    private var height = 0f
    private var rectF = RectF()
    private var region = Region()
    private val totalRegion = Region()
    private var inRange = false
    private var zoomSize = DisplayUtils.dp2px(context, 4f).toFloat()
    private var listener: OnTouchRangeListener? = null
 
    init {
        attrs?.apply { initAttrs(this) }
        initPaint()
        setWillNotDraw(false)
    }
 
    private fun initAttrs(attrs: AttributeSet) =
        context.theme.obtainStyledAttributes(attrs, R.styleable.DefaultCloseView, 0, 0).apply {
            normalColor = getColor(R.styleable.DefaultCloseView_normalColor, normalColor)
            inRangeColor = getColor(R.styleable.DefaultCloseView_inRangeColor, inRangeColor)
            shapeType = getInt(R.styleable.DefaultCloseView_shapeType, shapeType)
            zoomSize = getDimension(R.styleable.DefaultCloseView_zoomSize, zoomSize)
        }.recycle()
 
 
    private fun initPaint() {
        paint = Paint().apply {
            color = normalColor
            strokeWidth = 10f
            style = Paint.Style.FILL
            isAntiAlias = true
        }
    }
 
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        width = w.toFloat()
        height = h.toFloat()
    }
 
    override fun onDraw(canvas: Canvas?) {
        path.reset()
        if (inRange) {
            paint.color = inRangeColor
            when (shapeType) {
                // 半椭圆
                0 -> {
                    rectF.set(paddingLeft.toFloat(), 0f, width - paddingRight, height * 2)
                    path.addOval(rectF, Path.Direction.CW)
                }
                // 矩形
                1 -> {
                    rectF.set(paddingLeft.toFloat(), 0f, width - paddingRight, height)
                    path.addRect(rectF, Path.Direction.CW)
                }
                // 半圆
                2 -> path.addCircle(width / 2, height, height, Path.Direction.CW)
            }
        } else {
            paint.color = normalColor
            when (shapeType) {
                // 半椭圆
                0 -> {
                    rectF.set(
                        paddingLeft + zoomSize,
                        zoomSize,
                        width - paddingRight - zoomSize,
                        (height - zoomSize) * 2
                    )
                    path.addOval(rectF, Path.Direction.CW)
                    totalRegion.set(
                        paddingLeft + zoomSize.toInt(),
                        zoomSize.toInt(),
                        (width - paddingRight - zoomSize).toInt(),
                        height.toInt()
                    )
                }
                // 矩形
                1 -> {
                    rectF.set(
                        paddingLeft.toFloat(),
                        zoomSize,
                        width - paddingRight,
                        height
                    )
                    path.addRect(rectF, Path.Direction.CW)
                    totalRegion.set(
                        paddingLeft,
                        zoomSize.toInt(),
                        width.toInt() - paddingRight,
                        height.toInt()
                    )
                }
                // 半圆
                2 -> {
                    path.addCircle(width / 2, height, height - zoomSize, Path.Direction.CW)
                    totalRegion.set(0, zoomSize.toInt(), width.toInt(), height.toInt())
                }
            }
            region.setPath(path, totalRegion)
        }
        canvas?.drawPath(path, paint)
        super.onDraw(canvas)
    }
 
    override fun setTouchRangeListener(event: MotionEvent, listener: OnTouchRangeListener?) {
        this.listener = listener
        initTouchRange(event)
    }
 
    private fun initTouchRange(event: MotionEvent): Boolean {
        val location = IntArray(2)
        // 获取在整个屏幕内的绝对坐标
        getLocationOnScreen(location)
        val currentInRange = region.contains(
            event.rawX.toInt() - location[0], event.rawY.toInt() - location[1]
        )
        if (currentInRange != inRange) {
            inRange = currentInRange
            invalidate()
        }
        listener?.touchInRange(currentInRange, this)
        if (event.action == MotionEvent.ACTION_UP && currentInRange) {
            listener?.touchUpInRange()
        }
        return currentInRange
    }
 
}