I have a question. I’ve been punishing myself her...
# compose
c
I have a question. I’ve been punishing myself here trying to achieve what’s in the linked image, I kind of got it with drawing an arc bit I can get the arch to be aligned or anchored to the bottom of the image. Please, if you know how to improve my snippet or do this in a simpler way, code snippet of what I currently have is in the thread…
Copy code
val headerModifier = Modifier.fillMaxWidth()
                Box(
                    headerModifier,
                    contentAlignment = Alignment.BottomCenter,
                ) {
                    Image(
                        painter = painter,
                        contentDescription = null,
                        contentScale = ContentScale.Crop,
                        modifier = headerModifier
                    )
                    Box(
                        headerModifier
                    ) {
                        Canvas(headerModifier) {
                            println("size = $size")
                            val h = 120f
                            val w = size.width
                            val r = w * w / (6 * h) + h / 2
                            val x = w / 2 - r
                            println("r = $r, x = $x")
                            val topLeft = Offset(x, size.height - h)
                            val size = Size(2 * r, 2 * r)
                            drawArc(
                                background, startAngle = 0f, sweepAngle = -180f, useCenter = true,
                                topLeft = topLeft, size = size
                            )
                        }
                    }
                }
I also offer some other simple snippets as thank you: https://gist.github.com/cicerohellmann
I'm not trying to fit any specific image size, it should fit any width.
Also a "this approach is stupid, try this instead" also good, because this direction I'm on right now feels wrong.
n
just because I have to know, why don't you draw it using a tool (like Sketch), export as an SVG and convert it to a vector drawable ?
r
It's hard to tell from the image - how is it not lined up? Are you trying to get the white arc to go from corner to corner?
c
yes
I can share a picture
r
It's some sin/cos math to calculate the circle position based on the width and how high you want that arc. I may try now... because I'm nuts. 🙂
😂 2
c
I updated the snippet. I removed some padding bottom which made the drawable disapear
@nitrog42, I like the suffering.
👍 1
I don’t know if the drawable solution is the best approach
Also, if it’s of your curiosity: https://juliensalvi.medium.com/custom-shape-with-jetpack-compose-1cb48a991d42 Dude just posted a very interesting article on shapes
r
I've got an equation. Used pythagorean theorem, not sin/cos.
Copy code
Canvas(Modifier.fillMaxSize()) {
    println("size = $size")
    val h = 200f
    val w = size.width
    val r = w*w / (8*h) + h/2
    val x = w/2 - r
    println("r = $r, x = $x")
    val topLeft = Offset(x, size.height - h)
    val size = Size(2*r, 2*r)
    drawArc(Color.White, startAngle=0f, sweepAngle=-180f, useCenter=true,
            topLeft=topLeft, size=size)
}
You don't need that full sweep angle, but since mine is offscreen it doesn't matter.
c
I will try this
Omg
You really did it
r
Haha. 🙂
c
Can I use this?
r
Now you can animate
h
and have it bounce. Can't do that with the SVG. 🙂
Yes!
c
You can do all kinds of stuff with this
I could at least tip you a beer or a coffee
Srsly, you saved me a lot of time. Thanks.
Updated the snippet in the beginning of the conversation 😛
r
Cool, glad it helped. Slack hasn't built the beer dispenser button yet, so I'll take the karma. I got help from others here already.
🙏🏽 1
o
I would may be use Bezier curve instead. Start and end positions are corners, controls points are obviously symmetrical, and you can make it almost indistinguishable from an arc if you want (see for example https://spencermortensen.com/articles/bezier-circle/), but it gives more options if you want to animate it.
c
I will take a look. I've been playing around with vezier curves for years now but I've never sat down to stamp it in my brain. Thanks for the idea :)
r
Does Canvas have a way to draw quadratic or cubic Bezier curves? I don't see anything popping up in autocomplete in the
DrawScope
inside the
Canvas
closure.
1
o
Yes, of course it has Bezier 🙂 But you need to build a
Path
first (using
Path.cubicTo
) and then draw it.
❤️ 1