Skip to content

Commit b8aa1a3

Browse files
Merge pull request #104 from mtotschnig/StrokeCapCompose
[Compose] Allow configuration of StrokeCap
2 parents 2650b27 + 95c7295 commit b8aa1a3

File tree

6 files changed

+56
-34
lines changed

6 files changed

+56
-34
lines changed

library-compose/src/main/java/app/futured/donut/compose/Donut.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private fun DrawDonut(model: DonutModel, donutProgressValues: DonutProgressValue
103103
val wholeDonutAngle = 360f - gapWidthDegrees
104104
val masterSegmentAngle = wholeDonutAngle * masterProgress
105105
val startAngle = gapAngleDegrees + gapWidthDegrees / 2
106-
val strokeCap = StrokeCap.Round // TODO StrokeCap feature
106+
val strokeCap = model.strokeCap
107107

108108
val masterPathData = DonutPathDataEntry(
109109
color = backgroundLineColor,

library-compose/src/main/java/app/futured/donut/compose/data/DonutModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app.futured.donut.compose.data
22

33
import androidx.compose.ui.graphics.Color
4+
import androidx.compose.ui.graphics.StrokeCap
45
import app.futured.donut.compose.DonutProgress
56
import app.futured.donut.compose.internal.extensions.sumByFloat
67

@@ -16,6 +17,7 @@ import app.futured.donut.compose.internal.extensions.sumByFloat
1617
* @param gapAngleDegrees The angle in degrees, at which the gap will be displayed. Eg. when set to 90° then donut
1718
* will be rotated by 90° clockwise.
1819
* @param strokeWidth Stroke width of all lines in pixels.
20+
* @param strokeCap Stroke cap of all lines
1921
* @param backgroundLineColor The color of the donut background line.
2022
* @param sections The data used to define each section of the donut. This data should keep the same size
2123
* since adding or removing of the new section is not supported yet. If the size of this list is changed then
@@ -27,6 +29,7 @@ data class DonutModel(
2729
val gapWidthDegrees: Float = 90f,
2830
val gapAngleDegrees: Float = 90f,
2931
val strokeWidth: Float = 30f,
32+
val strokeCap: StrokeCap = StrokeCap.Round,
3033
val backgroundLineColor: Color = Color.LightGray,
3134
val sections: List<DonutSection>
3235
) {

sample/src/main/kotlin/app/futured/donutsample/ui/playground/compose/PlaygroundComposeActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.animation.core.tween
1919
import androidx.compose.runtime.collectAsState
2020
import androidx.compose.runtime.getValue
2121
import androidx.compose.ui.graphics.Color
22+
import androidx.compose.ui.graphics.StrokeCap
2223
import androidx.compose.ui.platform.ComposeView
2324
import app.futured.donut.compose.data.DonutConfig
2425
import app.futured.donut.compose.data.DonutModel
@@ -211,6 +212,14 @@ class PlaygroundComposeActivity : AppCompatActivity() {
211212
updateAnimationSpecs()
212213
}
213214
//endregion
215+
216+
findViewById<RadioGroup>(R.id.stroke_caps_radio_group).setOnCheckedChangeListener { _, checkedId ->
217+
mutateData { data -> data.copy(strokeCap = when (checkedId) {
218+
R.id.stroke_cap_round -> StrokeCap.Round
219+
R.id.stroke_cap_butt -> StrokeCap.Butt
220+
else -> error("Unexpected id: $checkedId")
221+
}) }
222+
}
214223
}
215224

216225
private fun updateAnimationSpecs() {

sample/src/main/res/layout/activity_playground.xml

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -329,39 +329,7 @@
329329

330330
</HorizontalScrollView>
331331

332-
<TextView
333-
android:layout_width="wrap_content"
334-
android:layout_height="wrap_content"
335-
android:layout_marginTop="8dp"
336-
android:text="@string/animation_stroke_caps"/>
337-
338-
<HorizontalScrollView
339-
android:layout_width="match_parent"
340-
android:layout_height="wrap_content">
341-
342-
<RadioGroup
343-
android:id="@+id/stroke_caps_radio_group"
344-
android:layout_width="wrap_content"
345-
android:layout_height="wrap_content"
346-
android:orientation="horizontal">
347-
348-
<androidx.appcompat.widget.AppCompatRadioButton
349-
android:id="@+id/stroke_cap_round"
350-
android:layout_width="wrap_content"
351-
android:layout_height="wrap_content"
352-
android:layout_marginStart="8dp"
353-
android:checked="true"
354-
android:text="@string/stroke_cap_round"/>
355-
356-
<androidx.appcompat.widget.AppCompatRadioButton
357-
android:id="@+id/stroke_cap_butt"
358-
android:layout_width="wrap_content"
359-
android:layout_height="wrap_content"
360-
android:text="@string/stroke_cap_butt"/>
361-
362-
</RadioGroup>
363-
364-
</HorizontalScrollView>
332+
<include layout="@layout/pick_stroke_cap" />
365333

366334
</LinearLayout>
367335

sample/src/main/res/layout/activity_playground_compose.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@
306306

307307
</HorizontalScrollView>
308308

309+
<include layout="@layout/pick_stroke_cap" />
310+
309311
</LinearLayout>
310312

311313
</ScrollView>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<merge xmlns:tools="http://schemas.android.com/tools"
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
tools:showIn="@layout/activity_playground">
5+
6+
7+
<TextView
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:layout_marginTop="8dp"
11+
android:text="@string/animation_stroke_caps" />
12+
13+
<HorizontalScrollView
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content">
16+
17+
<RadioGroup
18+
android:id="@+id/stroke_caps_radio_group"
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:orientation="horizontal">
22+
23+
<androidx.appcompat.widget.AppCompatRadioButton
24+
android:id="@+id/stroke_cap_round"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginStart="8dp"
28+
android:checked="true"
29+
android:text="@string/stroke_cap_round" />
30+
31+
<androidx.appcompat.widget.AppCompatRadioButton
32+
android:id="@+id/stroke_cap_butt"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:text="@string/stroke_cap_butt" />
36+
37+
</RadioGroup>
38+
39+
</HorizontalScrollView>
40+
</merge>

0 commit comments

Comments
 (0)