Bradleycorn
05/19/2021, 4:16 PMLinearGradient
for the background on a BoxWithConstraints
where the Box size is fillMaxSize()
so it may be different sizes depending on where it’s used. So far I have it rendering as seen in the attached designer screen shot, which is not quite what I want. I’m trying to get it so that the “line” between red and blue always goes from the bottom left corner to the top right corner, no matter the shape of the Box. Any idea how I can achieve that? My current code is posted as a reply.Bradleycorn
05/19/2021, 4:16 PM@Composable
fun SaddleCloth(programNumber: String, trackType: TrackType, bettingInterest: Int) {
val saddleCloth = saddleCloths.forRunner(trackType, bettingInterest)
val color =Brush.linearGradient(
0.0f to Color(0xEE,0x34,0x25),
0.5f to Color(0xF0,0x4F,0x32),
0.5f to Color(0x00,0x59,0xA4),
1.0f to Color(0x30,0x63,0xAA)
)
BoxWithConstraints(
modifier = Modifier.fillMaxSize().background(color),
contentAlignment = Alignment.Center
) {
val fontSize = maxWidth * 0.4f
Text(
text = programNumber,
textAlign = TextAlign.Center,
fontSize = LocalDensity.current.run { fontSize.toSp() },
fontWeight = FontWeight.Bold,
color = saddleCloth.contentColor)
}
}
@Preview("Square")
@Composable
fun SaddleClothPreview() {
ThemedPreview {
Box(modifier = Modifier.size(48.dp)) {
SaddleCloth(programNumber = "10F", TrackType.THOROUGHBRED, 10 )
}
}
}
@Preview("Rectangle")
@Composable
fun SaddleClothRectPreview() {
ThemedPreview {
Box(modifier = Modifier.width(24.dp).height(35.dp)) {
SaddleCloth(programNumber = "10F", TrackType.HARNESS, 10 )
}
}
}
Zach Klippenstein (he/him) [MOD]
05/19/2021, 4:32 PMlinearGradient
function takes start and end offsets, those don’t work?Bradleycorn
05/19/2021, 4:34 PMCasey Brooks
05/19/2021, 4:36 PMCanvas
instead of a gradientCasey Brooks
05/19/2021, 4:40 PMCanvas
, and has examples of drawing a line between the two corners. Should be pretty similar to draw that with a fill https://dev.to/tkuenneth/drawing-and-painting-in-jetpack-compose-1-2oklZach Klippenstein (he/him) [MOD]
05/19/2021, 4:41 PMBradleycorn
05/19/2021, 4:41 PMBradleycorn
05/19/2021, 4:43 PMZach Klippenstein (he/him) [MOD]
05/19/2021, 4:44 PMBradleycorn
05/19/2021, 4:45 PMBradleycorn
05/19/2021, 4:49 PMSolidColor()
Brush for the solid colors, and the LinearGradient for these few outliers) … But perhaps that’s not going to work out.Casey Brooks
05/19/2021, 4:53 PMCanvas
https://dev.to/tkuenneth/more-on-canvas-70gBradleycorn
05/19/2021, 5:58 PMdrawBehind
FTW!!Bradleycorn
05/19/2021, 5:59 PMfun trianglePath(corner1: Offset, corner2: Offset, corner3: Offset): Path {
return Path().apply {
moveTo(corner1.x, corner1.y)
lineTo(corner2.x, corner2.y)
lineTo(corner3.x, corner3.y)
close()
}
}
@Composable
fun SaddleCloth(programNumber: String, trackType: TrackType, bettingInterest: Int) {
val saddleCloth = saddleCloths.forRunner(trackType, bettingInterest)
BoxWithConstraints(
modifier = Modifier
.fillMaxSize()
.drawBehind {
val topTriangle = trianglePath(
Offset(0f,0f),
Offset(size.width, 0f),
Offset(0f, size.height)
)
val bottomTriangle = trianglePath(
Offset(size.width, 0f),
Offset(size.width, size.height),
Offset(0f, size.height)
)
drawPath(topTriangle, Color(0xEE,0x34,0x25))
drawPath(bottomTriangle, Color(0x30,0x63,0xAA))
},
contentAlignment = Alignment.Center
) {
val fontSize = maxWidth * 0.4f
Text(
text = programNumber,
textAlign = TextAlign.Center,
fontSize = LocalDensity.current.run { fontSize.toSp() },
fontWeight = FontWeight.Bold,
color = saddleCloth.contentColor)
}
}
Bradleycorn
05/19/2021, 7:15 PM@Composable
fun SaddleCloth(programNumber: String, trackType: TrackType, bettingInterest: Int) {
val saddleCloth = saddleCloths.forRunner(trackType, bettingInterest)
BoxWithConstraints(
modifier = Modifier
.fillMaxSize()
.drawWithCache {
onDrawBehind {
when (saddleCloth.type) {
is SaddleClothType.Solid -> drawRect(saddleCloth.type.primaryColor)
is SaddleClothType.Triangles -> drawTriangles(size, saddleCloth.type.primaryColor, saddleCloth.type.bottomColor)
is SaddleClothType.VerticalStripes -> drawVerticalStripes(size, saddleCloth.type.primaryColor, saddleCloth.type.stripeColor)
is SaddleClothType.HorizontalStripes -> drawHorizontalStripes(size, saddleCloth.type.primaryColor, saddleCloth.type.stripeColor)
}
}
},
contentAlignment = Alignment.Center
) {
val fontSize = maxWidth * 0.4f
Text(
text = programNumber,
textAlign = TextAlign.Center,
fontSize = LocalDensity.current.run { fontSize.toSp() },
fontWeight = FontWeight.Bold,
color = saddleCloth.contentColor)
}
}
Bradleycorn
05/19/2021, 7:15 PMNader Jawad
05/24/2021, 9:31 PM