Skip to content

Commit

Permalink
Merge pull request #58 from niallermoran/master
Browse files Browse the repository at this point in the history
 added strokeWidth to GaugeArcStyle to allow control of arc thickness by @niallermoran
  • Loading branch information
yamin8000 authored Sep 22, 2024
2 parents 23c4364 + 422848b commit 2631310
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
13 changes: 9 additions & 4 deletions Gauge/src/main/java/com/github/yamin8000/gauge/main/Gauge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import kotlin.math.sin
* customization options.
*
* @param value current value of the [Gauge], this value directly affects
* Gauge's arc and Gauge's needle style
* Gauge's arc and Gauge's needle style
* @param modifier refer to [Modifier]
* @param valueUnit unit of the Gauge's value like %, km/h or etc
* @param decimalFormat decimal formatter for value text
Expand All @@ -73,10 +73,10 @@ import kotlin.math.sin
* @param arcColors
* @param ticksColors
* @param ticksColorProvider a lambda for fine tune of individual tick's
* color
* color
* @param arcColorsProvider a lambda for fine tune of arc colors
* @throws IllegalArgumentException when some parameters are inconsistent
* with the design
* with the design
*/
@Composable
fun Gauge(
Expand Down Expand Up @@ -345,8 +345,13 @@ private fun DrawScope.drawArcs(
arcColorsProvider: (GaugeArcColors, Float, ClosedFloatingPointRange<Float>) -> GaugeArcColors
) {
val arcColors = arcColorsProvider(colors, value, valueRange)

val strokeWidth = if (style.strokeWidth != null) {
if (style.strokeWidth < size.toPx() / 15f) style.strokeWidth else size.toPx() / 15f
} else size.toPx() / 15f

val arcStroke = Stroke(
width = size.toPx() / 15f,
width = strokeWidth,
miter = 0f,
cap = style.cap
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import androidx.compose.ui.graphics.StrokeCap
* @param hasProgressiveAlpha whether to progressively increase arc's alpha according to gauge value
* @param bigTicksHasLabels whether to show numbers/labels for big ticks
* @param cap Gauge arc's [StrokeCap] type
* @param strokeWidth Arc's stroke width. If null then it will be calculated automatically
*/
data class GaugeArcStyle(
val hasArcs: Boolean = true,
val hasProgressiveAlpha: Boolean = true,
val bigTicksHasLabels: Boolean = true,
val cap: StrokeCap = StrokeCap.Round,
val strokeWidth: Float? = null
)
12 changes: 11 additions & 1 deletion app/src/main/java/com/github/yamin8000/gauge/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class MainActivity : ComponentActivity() {
val screenWidth = configuration.screenWidthDp.dp
var value by remember { mutableFloatStateOf(15f) }
var totalSize by remember { mutableFloatStateOf(350f) }
var strokeWidth by remember { mutableFloatStateOf(35f) }
val valueRange = 10f..20f
Gauge(
value = value,
Expand All @@ -77,7 +78,7 @@ class MainActivity : ComponentActivity() {
),
style = GaugeStyle(
hasBorder = true,
arcStyle = GaugeArcStyle(hasProgressiveAlpha = false)
arcStyle = GaugeArcStyle(hasProgressiveAlpha = false, strokeWidth = strokeWidth)
),
arcColorsProvider = { colors, gaugeValue, range ->
when (gaugeValue) {
Expand Down Expand Up @@ -117,6 +118,15 @@ class MainActivity : ComponentActivity() {
totalSize = it
}
)

Text("Arc Stroke Width: $strokeWidth")
Slider(
value = strokeWidth,
valueRange = 10f..60f,
onValueChange = {
strokeWidth = it
}
)
}
}
)
Expand Down

0 comments on commit 2631310

Please sign in to comment.