回答編集履歴
1
コード追加
answer
CHANGED
@@ -1,3 +1,141 @@
|
|
1
1
|
必要なことは, シークバーを移動させたら円を再描画することです.
|
2
2
|
|
3
|
-
もしかしたら勘違いされているのかもしれませんが, シークバーの移動によって onProgressChanged が実行されても, コード上で並んでいる ```//円の描画``` 以降が実行されるわけではありません.
|
3
|
+
もしかしたら勘違いされているのかもしれませんが, シークバーの移動によって onProgressChanged が実行されても, コード上で並んでいる ```//円の描画``` 以降が実行されるわけではありません.
|
4
|
+
|
5
|
+
#追加
|
6
|
+
円を描画する専用 View を作成して, 半径を指定したら再描画するようにするテもあります.
|
7
|
+
|
8
|
+
CircleView.kt
|
9
|
+
```kotlin
|
10
|
+
package com.teratail.q248833
|
11
|
+
|
12
|
+
import android.content.Context
|
13
|
+
import android.graphics.Canvas
|
14
|
+
import android.graphics.Color
|
15
|
+
import android.graphics.Paint
|
16
|
+
import android.graphics.Paint.ANTI_ALIAS_FLAG
|
17
|
+
import android.graphics.Paint.DITHER_FLAG
|
18
|
+
import android.util.AttributeSet
|
19
|
+
import android.util.Log
|
20
|
+
import androidx.appcompat.widget.AppCompatImageView
|
21
|
+
|
22
|
+
public class CircleView : AppCompatImageView {
|
23
|
+
lateinit var variableCirclePaint: Paint
|
24
|
+
lateinit var fixedCirclePaint: Paint
|
25
|
+
var radius = 100
|
26
|
+
get() = field
|
27
|
+
set(value) {
|
28
|
+
if(field != value) {
|
29
|
+
field = value
|
30
|
+
invalidate()
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
constructor(context: Context?) : super(context) { init() }
|
35
|
+
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { init() }
|
36
|
+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init() }
|
37
|
+
fun init() {
|
38
|
+
variableCirclePaint = Paint(ANTI_ALIAS_FLAG or DITHER_FLAG)
|
39
|
+
variableCirclePaint.setColor(Color.parseColor("#F5FF5F"))
|
40
|
+
|
41
|
+
fixedCirclePaint = Paint(ANTI_ALIAS_FLAG or DITHER_FLAG)
|
42
|
+
fixedCirclePaint.setColor(Color.parseColor("#c6142d"))
|
43
|
+
}
|
44
|
+
|
45
|
+
override fun onDraw(canvas: Canvas?) {
|
46
|
+
super.onDraw(canvas)
|
47
|
+
if(canvas != null) {
|
48
|
+
val center_x = canvas.width / 2f
|
49
|
+
val center_y = canvas.height / 2f
|
50
|
+
//変化しない円
|
51
|
+
canvas.drawCircle(center_x, center_y, 120.0f, fixedCirclePaint)
|
52
|
+
//半径が変化する円
|
53
|
+
canvas.drawCircle(center_x, center_y, radius.toFloat(), variableCirclePaint)
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
```
|
58
|
+
MainActivity.kt
|
59
|
+
```kotlin
|
60
|
+
package com.teratail.q248833
|
61
|
+
|
62
|
+
import android.os.Bundle
|
63
|
+
import android.widget.SeekBar
|
64
|
+
import androidx.appcompat.app.AppCompatActivity
|
65
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
66
|
+
import java.util.*
|
67
|
+
|
68
|
+
class MainActivity : AppCompatActivity() {
|
69
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
70
|
+
super.onCreate(savedInstanceState)
|
71
|
+
setContentView(R.layout.activity_main)
|
72
|
+
|
73
|
+
//円の半径初期値
|
74
|
+
circle.radius = 100 //[px]
|
75
|
+
|
76
|
+
//seekbar
|
77
|
+
seekbar.min = 0 //[%]
|
78
|
+
seekbar.max = 100 //[%]
|
79
|
+
seekbar.setOnSeekBarChangeListener(
|
80
|
+
object : SeekBar.OnSeekBarChangeListener {
|
81
|
+
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
|
82
|
+
//ここでseekbarの数字を代入したい
|
83
|
+
circle.radius = progress
|
84
|
+
|
85
|
+
val str = String.format(Locale.US, "%d %%", progress)
|
86
|
+
percent.text = str //percentage which user setted
|
87
|
+
}
|
88
|
+
override fun onStartTrackingTouch(seekBar: SeekBar) {}
|
89
|
+
override fun onStopTrackingTouch(seekBar: SeekBar) {}
|
90
|
+
})
|
91
|
+
seekbar.progress = circle.radius
|
92
|
+
}
|
93
|
+
}
|
94
|
+
```
|
95
|
+
activity_main.xml
|
96
|
+
```xml
|
97
|
+
<?xml version="1.0" encoding="utf-8"?>
|
98
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
99
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
100
|
+
xmlns:tools="http://schemas.android.com/tools"
|
101
|
+
android:layout_width="match_parent"
|
102
|
+
android:layout_height="match_parent"
|
103
|
+
tools:context=".MainActivity">
|
104
|
+
|
105
|
+
<com.teratail.q248833.CircleView
|
106
|
+
android:id="@+id/circle"
|
107
|
+
android:layout_width="300px"
|
108
|
+
android:layout_height="300px"
|
109
|
+
app:layout_constraintBottom_toTopOf="@id/percent"
|
110
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
111
|
+
app:layout_constraintRight_toRightOf="parent"
|
112
|
+
app:layout_constraintTop_toTopOf="parent" />
|
113
|
+
|
114
|
+
<TextView
|
115
|
+
android:id="@+id/percent"
|
116
|
+
android:padding="10dp"
|
117
|
+
android:layout_width="match_parent"
|
118
|
+
android:layout_height="wrap_content"
|
119
|
+
android:layout_margin="10dp"
|
120
|
+
android:gravity="center"
|
121
|
+
android:textColor="#000"
|
122
|
+
android:textSize="20sp"
|
123
|
+
app:layout_constraintBottom_toTopOf="@id/seekbar"
|
124
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
125
|
+
app:layout_constraintRight_toRightOf="parent"
|
126
|
+
app:layout_constraintTop_toBottomOf="@id/circle"/>
|
127
|
+
|
128
|
+
<SeekBar
|
129
|
+
android:id="@+id/seekbar"
|
130
|
+
android:layout_width="match_parent"
|
131
|
+
android:layout_height="60dp"
|
132
|
+
android:layout_margin="10dp"
|
133
|
+
android:background="#ccc"
|
134
|
+
android:gravity="center"
|
135
|
+
android:padding="20dp"
|
136
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
137
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
138
|
+
app:layout_constraintRight_toRightOf="parent"
|
139
|
+
app:layout_constraintTop_toBottomOf="@id/percent"/>
|
140
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
141
|
+
```
|