@@ -2,16 +2,14 @@ package com.pengxh.kt.lite.widget
2
2
3
3
import android.annotation.SuppressLint
4
4
import android.content.Context
5
- import android.graphics.Canvas
6
- import android.graphics.Color
7
- import android.graphics.Paint
8
- import android.graphics.RectF
9
5
import android.util.AttributeSet
10
6
import android.view.LayoutInflater
11
7
import android.view.MotionEvent
12
8
import android.widget.ImageButton
13
9
import android.widget.RelativeLayout
10
+ import androidx.constraintlayout.widget.ConstraintLayout
14
11
import com.pengxh.kt.lite.R
12
+ import com.pengxh.kt.lite.extensions.getScreenWidth
15
13
16
14
17
15
/* *
@@ -21,198 +19,82 @@ import com.pengxh.kt.lite.R
21
19
class SteeringWheelView constructor(context : Context , attrs : AttributeSet ) :
22
20
RelativeLayout (context, attrs) {
23
21
24
- // 画布中心x
25
- private var canvasCenterX = 0
26
-
27
- // 画布中心y
28
- private var canvasCenterY = 0
29
-
30
- // 控件直径
31
- private val diameter: Float
32
-
33
- // Paint
34
- private val backgroundPaint: Paint
35
- private val borderPaint: Paint
36
- private val directionPaint: Paint
37
-
38
- // 各控件使用状态
39
- private var leftTurn = false
40
- private var topTurn = false
41
- private var rightTurn = false
42
- private var bottomTurn = false
43
-
44
- // 外圆区域
45
- private lateinit var outerCircleRectF: RectF
46
-
47
- // 线条粗细
48
- private val borderStroke: Float
49
-
50
22
init {
51
23
val type = context.obtainStyledAttributes(attrs, R .styleable.SteeringWheelView )
52
- diameter = type.getDimension(
53
- R .styleable.SteeringWheelView_ctrl_diameter , 200f
54
- )
55
- val borderColor = type.getColor(
56
- R .styleable.SteeringWheelView_ctrl_borderColor , Color .CYAN
57
- )
58
- val backgroundColor = type.getColor(
59
- R .styleable.SteeringWheelView_ctrl_backgroundColor , Color .WHITE
60
- )
61
- borderStroke = type.getDimension(
62
- R .styleable.SteeringWheelView_ctrl_borderStroke , 5f
63
- )
24
+ var diameter = type.getDimension(R .styleable.SteeringWheelView_ctrl_diameter , 250f )
64
25
type.recycle()
26
+ if (diameter <= 150f ) {
27
+ diameter = 150f
28
+ }
65
29
66
- borderPaint = Paint ()
67
- borderPaint.isAntiAlias = true
68
- borderPaint.isDither = true
69
- borderPaint.style = Paint .Style .STROKE
70
- borderPaint.strokeWidth = borderStroke
71
- borderPaint.color = borderColor
72
-
73
- backgroundPaint = Paint ()
74
- backgroundPaint.isAntiAlias = true
75
- backgroundPaint.isDither = true
76
- backgroundPaint.style = Paint .Style .FILL
77
- backgroundPaint.color = backgroundColor
78
-
79
- directionPaint = Paint ()
80
- directionPaint.isAntiAlias = true
81
- directionPaint.isDither = true
82
- directionPaint.style = Paint .Style .FILL
83
- directionPaint.color = Color .parseColor(" #EEEEEE" )
30
+ if (diameter >= context.getScreenWidth()) {
31
+ diameter = context.getScreenWidth().toFloat()
32
+ }
84
33
85
34
val layoutParams = LayoutParams (diameter.toInt(), diameter.toInt())
86
-
87
35
val view = LayoutInflater .from(context).inflate(R .layout.widget_view_steering_wheel, this )
88
-
89
- val rootView = view.findViewById<RelativeLayout >(R .id.rootView)
36
+ val rootView = view.findViewById<ConstraintLayout >(R .id.rootView)
90
37
rootView.layoutParams = layoutParams
91
38
92
39
val leftButton = view.findViewById<ImageButton >(R .id.leftButton)
93
40
leftButton.setOnTouchListener { _, event ->
94
41
when (event.action) {
95
42
MotionEvent .ACTION_DOWN -> {
96
- leftTurn = true
97
43
listener?.onLeftTurn()
98
44
}
99
45
100
46
MotionEvent .ACTION_UP -> {
101
- leftTurn = false
102
47
listener?.onActionTurnUp(Direction .LEFT )
103
48
}
104
49
}
105
50
postInvalidate()
106
- true
51
+ false
107
52
}
108
53
109
54
view.findViewById<ImageButton >(R .id.topButton).setOnTouchListener { _, event ->
110
55
when (event.action) {
111
56
MotionEvent .ACTION_DOWN -> {
112
- topTurn = true
113
57
listener?.onTopTurn()
114
58
}
115
59
116
60
MotionEvent .ACTION_UP -> {
117
- topTurn = false
118
61
listener?.onActionTurnUp(Direction .TOP )
119
62
}
120
63
}
121
64
postInvalidate()
122
- true
65
+ false
123
66
}
124
67
view.findViewById<ImageButton >(R .id.rightButton).setOnTouchListener { _, event ->
125
68
when (event.action) {
126
69
MotionEvent .ACTION_DOWN -> {
127
- rightTurn = true
128
70
listener?.onRightTurn()
129
71
}
130
72
131
73
MotionEvent .ACTION_UP -> {
132
- rightTurn = false
133
74
listener?.onActionTurnUp(Direction .RIGHT )
134
75
}
135
76
}
136
77
postInvalidate()
137
- true
78
+ false
138
79
}
139
80
view.findViewById<ImageButton >(R .id.bottomButton).setOnTouchListener { _, event ->
140
81
when (event.action) {
141
82
MotionEvent .ACTION_DOWN -> {
142
- bottomTurn = true
143
83
listener?.onBottomTurn()
144
84
}
145
85
146
86
MotionEvent .ACTION_UP -> {
147
- bottomTurn = false
148
87
listener?.onActionTurnUp(Direction .BOTTOM )
149
88
}
150
89
}
151
90
postInvalidate()
152
- true
91
+ false
153
92
}
154
93
view.findViewById<ImageButton >(R .id.centerButton).setOnClickListener {
155
94
listener?.onCenterClicked()
156
95
}
157
96
}
158
97
159
- override fun onSizeChanged (w : Int , h : Int , oldw : Int , oldh : Int ) {
160
- super .onSizeChanged(w, h, oldw, oldh)
161
- // 圆心位置
162
- canvasCenterX = w shr 1
163
- canvasCenterY = h shr 1
164
-
165
- val outerCircleRadius = (diameter - 20 ).toInt() shr 1 // 半径
166
-
167
- // 大外圈区域
168
- outerCircleRectF = RectF (
169
- (canvasCenterX - outerCircleRadius).toFloat(),
170
- (canvasCenterY - outerCircleRadius).toFloat(),
171
- (canvasCenterX + outerCircleRadius).toFloat(),
172
- (canvasCenterY + outerCircleRadius).toFloat()
173
- )
174
- }
175
-
176
- override fun onDraw (canvas : Canvas ) {
177
- super .onDraw(canvas)
178
- val outerCircleRadius = diameter.toInt() shr 1 // 半径
179
- // 背景
180
- canvas.drawCircle(
181
- canvasCenterX.toFloat(),
182
- canvasCenterY.toFloat(),
183
- outerCircleRadius.toFloat(),
184
- backgroundPaint
185
- )
186
-
187
- // 外圆圆圈
188
- canvas.drawCircle(
189
- canvasCenterX.toFloat(),
190
- canvasCenterY.toFloat(),
191
- outerCircleRadius.toFloat(),
192
- borderPaint
193
- )
194
-
195
- if (leftTurn) {
196
- canvas.drawArc(
197
- outerCircleRectF, (90 * 2 - 45 ).toFloat(), 90f , true , directionPaint
198
- )
199
- }
200
-
201
- if (topTurn) {
202
- canvas.drawArc(
203
- outerCircleRectF, (90 * 3 - 45 ).toFloat(), 90f , true , directionPaint
204
- )
205
- }
206
-
207
- if (rightTurn) {
208
- canvas.drawArc(outerCircleRectF, - 45f , 90f , true , directionPaint)
209
- }
210
-
211
- if (bottomTurn) {
212
- canvas.drawArc(outerCircleRectF, 45f , 90f , true , directionPaint)
213
- }
214
- }
215
-
216
98
enum class Direction {
217
99
LEFT , TOP , RIGHT , BOTTOM
218
100
}
0 commit comments