回答編集履歴
1
コード追加
test
CHANGED
@@ -3,3 +3,279 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
もしかしたら勘違いされているのかもしれませんが, シークバーの移動によって onProgressChanged が実行されても, コード上で並んでいる ```//円の描画``` 以降が実行されるわけではありません.
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
#追加
|
10
|
+
|
11
|
+
円を描画する専用 View を作成して, 半径を指定したら再描画するようにするテもあります.
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
CircleView.kt
|
16
|
+
|
17
|
+
```kotlin
|
18
|
+
|
19
|
+
package com.teratail.q248833
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
import android.content.Context
|
24
|
+
|
25
|
+
import android.graphics.Canvas
|
26
|
+
|
27
|
+
import android.graphics.Color
|
28
|
+
|
29
|
+
import android.graphics.Paint
|
30
|
+
|
31
|
+
import android.graphics.Paint.ANTI_ALIAS_FLAG
|
32
|
+
|
33
|
+
import android.graphics.Paint.DITHER_FLAG
|
34
|
+
|
35
|
+
import android.util.AttributeSet
|
36
|
+
|
37
|
+
import android.util.Log
|
38
|
+
|
39
|
+
import androidx.appcompat.widget.AppCompatImageView
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
public class CircleView : AppCompatImageView {
|
44
|
+
|
45
|
+
lateinit var variableCirclePaint: Paint
|
46
|
+
|
47
|
+
lateinit var fixedCirclePaint: Paint
|
48
|
+
|
49
|
+
var radius = 100
|
50
|
+
|
51
|
+
get() = field
|
52
|
+
|
53
|
+
set(value) {
|
54
|
+
|
55
|
+
if(field != value) {
|
56
|
+
|
57
|
+
field = value
|
58
|
+
|
59
|
+
invalidate()
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
constructor(context: Context?) : super(context) { init() }
|
68
|
+
|
69
|
+
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { init() }
|
70
|
+
|
71
|
+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init() }
|
72
|
+
|
73
|
+
fun init() {
|
74
|
+
|
75
|
+
variableCirclePaint = Paint(ANTI_ALIAS_FLAG or DITHER_FLAG)
|
76
|
+
|
77
|
+
variableCirclePaint.setColor(Color.parseColor("#F5FF5F"))
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
fixedCirclePaint = Paint(ANTI_ALIAS_FLAG or DITHER_FLAG)
|
82
|
+
|
83
|
+
fixedCirclePaint.setColor(Color.parseColor("#c6142d"))
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
override fun onDraw(canvas: Canvas?) {
|
90
|
+
|
91
|
+
super.onDraw(canvas)
|
92
|
+
|
93
|
+
if(canvas != null) {
|
94
|
+
|
95
|
+
val center_x = canvas.width / 2f
|
96
|
+
|
97
|
+
val center_y = canvas.height / 2f
|
98
|
+
|
99
|
+
//変化しない円
|
100
|
+
|
101
|
+
canvas.drawCircle(center_x, center_y, 120.0f, fixedCirclePaint)
|
102
|
+
|
103
|
+
//半径が変化する円
|
104
|
+
|
105
|
+
canvas.drawCircle(center_x, center_y, radius.toFloat(), variableCirclePaint)
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
MainActivity.kt
|
116
|
+
|
117
|
+
```kotlin
|
118
|
+
|
119
|
+
package com.teratail.q248833
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
import android.os.Bundle
|
124
|
+
|
125
|
+
import android.widget.SeekBar
|
126
|
+
|
127
|
+
import androidx.appcompat.app.AppCompatActivity
|
128
|
+
|
129
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
130
|
+
|
131
|
+
import java.util.*
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
class MainActivity : AppCompatActivity() {
|
136
|
+
|
137
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
138
|
+
|
139
|
+
super.onCreate(savedInstanceState)
|
140
|
+
|
141
|
+
setContentView(R.layout.activity_main)
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
//円の半径初期値
|
146
|
+
|
147
|
+
circle.radius = 100 //[px]
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
//seekbar
|
152
|
+
|
153
|
+
seekbar.min = 0 //[%]
|
154
|
+
|
155
|
+
seekbar.max = 100 //[%]
|
156
|
+
|
157
|
+
seekbar.setOnSeekBarChangeListener(
|
158
|
+
|
159
|
+
object : SeekBar.OnSeekBarChangeListener {
|
160
|
+
|
161
|
+
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
|
162
|
+
|
163
|
+
//ここでseekbarの数字を代入したい
|
164
|
+
|
165
|
+
circle.radius = progress
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
val str = String.format(Locale.US, "%d %%", progress)
|
170
|
+
|
171
|
+
percent.text = str //percentage which user setted
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
override fun onStartTrackingTouch(seekBar: SeekBar) {}
|
176
|
+
|
177
|
+
override fun onStopTrackingTouch(seekBar: SeekBar) {}
|
178
|
+
|
179
|
+
})
|
180
|
+
|
181
|
+
seekbar.progress = circle.radius
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
activity_main.xml
|
190
|
+
|
191
|
+
```xml
|
192
|
+
|
193
|
+
<?xml version="1.0" encoding="utf-8"?>
|
194
|
+
|
195
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
196
|
+
|
197
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
198
|
+
|
199
|
+
xmlns:tools="http://schemas.android.com/tools"
|
200
|
+
|
201
|
+
android:layout_width="match_parent"
|
202
|
+
|
203
|
+
android:layout_height="match_parent"
|
204
|
+
|
205
|
+
tools:context=".MainActivity">
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
<com.teratail.q248833.CircleView
|
210
|
+
|
211
|
+
android:id="@+id/circle"
|
212
|
+
|
213
|
+
android:layout_width="300px"
|
214
|
+
|
215
|
+
android:layout_height="300px"
|
216
|
+
|
217
|
+
app:layout_constraintBottom_toTopOf="@id/percent"
|
218
|
+
|
219
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
220
|
+
|
221
|
+
app:layout_constraintRight_toRightOf="parent"
|
222
|
+
|
223
|
+
app:layout_constraintTop_toTopOf="parent" />
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
<TextView
|
228
|
+
|
229
|
+
android:id="@+id/percent"
|
230
|
+
|
231
|
+
android:padding="10dp"
|
232
|
+
|
233
|
+
android:layout_width="match_parent"
|
234
|
+
|
235
|
+
android:layout_height="wrap_content"
|
236
|
+
|
237
|
+
android:layout_margin="10dp"
|
238
|
+
|
239
|
+
android:gravity="center"
|
240
|
+
|
241
|
+
android:textColor="#000"
|
242
|
+
|
243
|
+
android:textSize="20sp"
|
244
|
+
|
245
|
+
app:layout_constraintBottom_toTopOf="@id/seekbar"
|
246
|
+
|
247
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
248
|
+
|
249
|
+
app:layout_constraintRight_toRightOf="parent"
|
250
|
+
|
251
|
+
app:layout_constraintTop_toBottomOf="@id/circle"/>
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
<SeekBar
|
256
|
+
|
257
|
+
android:id="@+id/seekbar"
|
258
|
+
|
259
|
+
android:layout_width="match_parent"
|
260
|
+
|
261
|
+
android:layout_height="60dp"
|
262
|
+
|
263
|
+
android:layout_margin="10dp"
|
264
|
+
|
265
|
+
android:background="#ccc"
|
266
|
+
|
267
|
+
android:gravity="center"
|
268
|
+
|
269
|
+
android:padding="20dp"
|
270
|
+
|
271
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
272
|
+
|
273
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
274
|
+
|
275
|
+
app:layout_constraintRight_toRightOf="parent"
|
276
|
+
|
277
|
+
app:layout_constraintTop_toBottomOf="@id/percent"/>
|
278
|
+
|
279
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
280
|
+
|
281
|
+
```
|