Skip to content

Commit c57274f

Browse files
优化方向控制盘控件
1 parent e92f76e commit c57274f

File tree

8 files changed

+90
-198
lines changed

8 files changed

+90
-198
lines changed

app/src/main/res/layout/fragment_widget_steering_wheel_view.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,5 @@
1313
android:layout_height="match_parent"
1414
android:background="@color/mainBackground"
1515
android:gravity="center"
16-
app:ctrl_backgroundColor="@color/white"
17-
app:ctrl_borderColor="#64BFEB"
18-
app:ctrl_borderStroke="@dimen/dp_5"
19-
app:ctrl_diameter="@dimen/dp_200" />
16+
app:ctrl_diameter="250dp" />
2017
</LinearLayout>

lite/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ android {
3535
dependencies {
3636
implementation 'androidx.annotation:annotation:1.7.1'
3737
implementation 'androidx.core:core-ktx:1.9.0'
38-
implementation 'androidx.appcompat:appcompat:1.6.1'
38+
def base_version = "1.6.1"
39+
implementation "androidx.appcompat:appcompat:${base_version}"
40+
implementation "com.google.android.material:material:${base_version}"
3941
implementation 'androidx.cardview:cardview:1.0.0'
4042
implementation "androidx.recyclerview:recyclerview:1.3.0"
4143
implementation 'com.squareup.retrofit2:adapter-rxjava:2.8.1'

lite/src/main/java/com/pengxh/kt/lite/widget/SteeringWheelView.kt

Lines changed: 14 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ package com.pengxh.kt.lite.widget
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5-
import android.graphics.Canvas
6-
import android.graphics.Color
7-
import android.graphics.Paint
8-
import android.graphics.RectF
95
import android.util.AttributeSet
106
import android.view.LayoutInflater
117
import android.view.MotionEvent
128
import android.widget.ImageButton
139
import android.widget.RelativeLayout
10+
import androidx.constraintlayout.widget.ConstraintLayout
1411
import com.pengxh.kt.lite.R
12+
import com.pengxh.kt.lite.extensions.getScreenWidth
1513

1614

1715
/**
@@ -21,198 +19,82 @@ import com.pengxh.kt.lite.R
2119
class SteeringWheelView constructor(context: Context, attrs: AttributeSet) :
2220
RelativeLayout(context, attrs) {
2321

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-
5022
init {
5123
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)
6425
type.recycle()
26+
if (diameter <= 150f) {
27+
diameter = 150f
28+
}
6529

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+
}
8433

8534
val layoutParams = LayoutParams(diameter.toInt(), diameter.toInt())
86-
8735
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)
9037
rootView.layoutParams = layoutParams
9138

9239
val leftButton = view.findViewById<ImageButton>(R.id.leftButton)
9340
leftButton.setOnTouchListener { _, event ->
9441
when (event.action) {
9542
MotionEvent.ACTION_DOWN -> {
96-
leftTurn = true
9743
listener?.onLeftTurn()
9844
}
9945

10046
MotionEvent.ACTION_UP -> {
101-
leftTurn = false
10247
listener?.onActionTurnUp(Direction.LEFT)
10348
}
10449
}
10550
postInvalidate()
106-
true
51+
false
10752
}
10853

10954
view.findViewById<ImageButton>(R.id.topButton).setOnTouchListener { _, event ->
11055
when (event.action) {
11156
MotionEvent.ACTION_DOWN -> {
112-
topTurn = true
11357
listener?.onTopTurn()
11458
}
11559

11660
MotionEvent.ACTION_UP -> {
117-
topTurn = false
11861
listener?.onActionTurnUp(Direction.TOP)
11962
}
12063
}
12164
postInvalidate()
122-
true
65+
false
12366
}
12467
view.findViewById<ImageButton>(R.id.rightButton).setOnTouchListener { _, event ->
12568
when (event.action) {
12669
MotionEvent.ACTION_DOWN -> {
127-
rightTurn = true
12870
listener?.onRightTurn()
12971
}
13072

13173
MotionEvent.ACTION_UP -> {
132-
rightTurn = false
13374
listener?.onActionTurnUp(Direction.RIGHT)
13475
}
13576
}
13677
postInvalidate()
137-
true
78+
false
13879
}
13980
view.findViewById<ImageButton>(R.id.bottomButton).setOnTouchListener { _, event ->
14081
when (event.action) {
14182
MotionEvent.ACTION_DOWN -> {
142-
bottomTurn = true
14383
listener?.onBottomTurn()
14484
}
14585

14686
MotionEvent.ACTION_UP -> {
147-
bottomTurn = false
14887
listener?.onActionTurnUp(Direction.BOTTOM)
14988
}
15089
}
15190
postInvalidate()
152-
true
91+
false
15392
}
15493
view.findViewById<ImageButton>(R.id.centerButton).setOnClickListener {
15594
listener?.onCenterClicked()
15695
}
15796
}
15897

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-
21698
enum class Direction {
21799
LEFT, TOP, RIGHT, BOTTOM
218100
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="64dp"
3+
android:height="64dp"
4+
android:viewportWidth="1024"
5+
android:viewportHeight="1024">
6+
<path
7+
android:fillColor="#4D65BFEA"
8+
android:pathData="M619.8,44.8l359.4,359.5a152.5,152.5 0,0 1,0 215.6L619.8,979.3a152.5,152.5 0,0 1,-215.7 0L44.7,619.8a152.5,152.5 0,0 1,0 -215.6L404.2,44.8a152.5,152.5 0,0 1,215.7 0z" />
9+
</vector>

lite/src/main/res/drawable/direction_point_selector.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:state_pressed="true">
4+
<vector android:width="64px" android:height="64px" android:viewportWidth="1024" android:viewportHeight="1024">
5+
<path android:fillColor="#1A65BFEA" android:pathData="M83.1,669l346.2,-490.3c37.1,-51.7 129.8,-51.7 166.9,0l347.3,490.3c37.1,51.7 10.5,137.1 -63.7,137.1l-734.1,-0c-74.2,-0 -99.8,-85.4 -62.7,-137.1z" />
6+
</vector>
7+
</item>
8+
9+
<item android:state_pressed="false">
10+
<vector android:width="64px" android:height="64px" android:viewportWidth="1024" android:viewportHeight="1024">
11+
<path android:fillColor="@color/white" android:pathData="M83.1,669l346.2,-490.3c37.1,-51.7 129.8,-51.7 166.9,0l347.3,490.3c37.1,51.7 10.5,137.1 -63.7,137.1l-734.1,-0c-74.2,-0 -99.8,-85.4 -62.7,-137.1z" />
12+
</vector>
13+
</item>
14+
</selector>

0 commit comments

Comments
 (0)