Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jetpack Compose to realize the effect of drawing pentagram

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article Xiaobian introduces in detail for you "how to use Jetpack Compose to achieve the pentagram effect", the content is detailed, the steps are clear, the details are handled properly, I hope this "how to use Jetpack Compose to achieve the pentagram effect" article can help you solve your doubts, the following follows the editor's ideas slowly in depth, together to learn new knowledge.

Principle

We realize the principle of drawing a five-pointed star as shown in the following figure. First, we will make up two circles, divide the angles of the inner circle and the outer circle into five parts, then connect the coordinates of the tangent points of the inner circle and the outer circle in turn, and then draw them with path.

Realize

The implementation in the code involves custom rendering, which is not difficult. Points to pay attention to:

The anchor point of the angle in composse is Math.PI, while the original anchor point is the angle (360).

The default origin is in the upper left corner, and we need to actively move to the center point of the combination when we draw.

The drawing of path can be filled with closed path graphics with Fill, and linear closed path graphics can be drawn with Stroke.

Code fun Modifier.customDraw (color: Color, starCount: Int = 5, checked: Boolean = false,) = this.then (CustomDrawModifier (color, starCount, checked = checked) class CustomDrawModifier (privateval color: Color, privateval starCount: Int = 5 private var checked: Boolean = false ): DrawModifier {override fun ContentDrawScope.draw () {log ("$size") val radiusOuter = if (size.width > size.height) size.height / 2 else size.width / 2 / / the outer diameter of the pentagram val radiusInner = radiusOuter / 2 / / the radius of the inner circle of the pentagram val startAngle = (- Math.PI / 2). ToFloat () / / the outer diameter angle val of the starting point PerAngle = (2 * Math.PI / starCount). ToFloat () / / the direct angle difference between two five-pointed stars val outAngles = (0 until starCount). Map {val angle = it * perAngle + startAngle Offset (radiusOuter * cos (angle)) RadiusOuter * sin (angle)} / / the vertices of all outer fillets val innerAngles = (0 until starCount). Map {val angle = it * perAngle + perAngle / 2 + startAngle Offset (radiusInner * cos (angle)) RadiusInner * sin (angle)} / / vertices of all inner fillets val path = Path () / / draw the point connection line (0 until starCount) of all inner and outer circles of the five-pointed star. ForEachIndexed {index _-> val outerX = outAngles [index] .x val outerY = outAngles [index] .y val innerX = innerAngles [index] .x val innerY = innerAngles [index] .y / / drawCircle (Color.Red, radius = 3f, center = outAngles [index]) / / drawCircle (Color.Yellow, radius = 3f) Center = innerAngles [index]) if (index = = 0) {path.moveTo (outerX, outerY) path.lineTo (innerX, innerY) path.lineTo (outAngles [(index + 1)% starCount] .x, outAngles [(index + 1)% starCount] .y)} else {path.lineTo (innerX) InnerY) / / move to the end of the inner fillet path.lineTo (outAngles [(index + 1)% starCount] .x OutAngles [(index + 1)% starCount] .y) / / connects to the endpoint of the next outer fillet} if (index = = starCount-1) {path.close ()}} translate (size.width / 2, size.height / 2) {drawPath (path, color) Style = if (checked) Fill else Stroke (width = 5f)} final effect

Read here, this "how to use Jetpack Compose to achieve the effect of drawing five-pointed star" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report