Android Program to Draw Multiple Circles With Varying Size
Jetpack Compose makes it easier to work with custom graphics. Many apps need to be able to precisely control exactly what'due south drawn on the screen. This might be as pocket-size as putting a box or a circle on the screen in but the correct place, or it might be an elaborate organisation of graphic elements in many different styles. With Compose's declarative approach, all the graphic configuration happens in 1 place, instead of being carve up betwixt a method call and a Paint
helper object. Etch takes intendance of creating and updating the needed objects in an efficient way.
Declarative graphics with Etch
Etch extends its declarative approach to how it handles graphics. Compose's arroyo offers a number of advantages:
- Compose minimizes the state in its graphic elements, helping yous avoid state's programming pitfalls.
- When y'all depict something, all the options are right where you'd await them, in the composable function.
- Compose's graphics APIs take care of creating and freeing objects in an efficient way.
Canvas
The cadre composable for custom graphics is Canvass
. You place the Canvas
in your layout the same way yous would with whatever other Etch UI element. Within the Canvass
, y'all can draw elements with precise control over their way and location.
For case, this code creates a Canvas
composable that fills all the available space in its parent element:
Sheet(modifier = Modifier.fillMaxSize()) { }
The Sheet
automatically exposes a DrawScope
, a scoped drawing surround that maintains its ain land. This lets you set the parameters for a group of graphical elements. The DrawScope
provides several useful fields, like size
, a Size
object specifying the current and maximum dimensions of the DrawScope
.
So, for example, suppose you want to describe a diagonal line from the superlative-right corner of the sail to the bottom-left corner. You would practise this past calculation a drawLine
composable:
Canvas(modifier = Modifier.fillMaxSize()) { val canvasWidth = size.width val canvasHeight = size.height drawLine( offset = Showtime(ten = canvasWidth, y = 0f), end = First(x = 0f, y = canvasHeight), color = Color.Blue ) }
Figure ane. Using drawLine
to draw a line across the canvas. The code sets the line's color merely uses the default width.
Yous can use other parameters to customize the cartoon. For case, by default, the line is drawn with a hairline width, which displays as one pixel wide regardless of the drawing's calibration. You tin can override the default by setting a strokeWidth
value:
Canvas(modifier = Modifier.fillMaxSize()) { val canvasWidth = size.width val canvasHeight = size.superlative drawLine( start = Beginning(10 = canvasWidth, y = 0f), cease = Offset(ten = 0f, y = canvasHeight), colour = Color.Blue, strokeWidth = 5F ) }
Effigy ii. Modifying the line from effigy 1 past overriding the default width.
There are many other simple drawing functions, such every bit drawRect
and drawCircle
. For case, this code draws a filled circle in the middle of the sheet, with a diameter equal to half of the canvas's shorter dimension:
Canvas(modifier = Modifier.fillMaxSize()) { val canvasWidth = size.width val canvasHeight = size.acme drawCircle( colour = Color.Blue, center = Offset(x = canvasWidth / ii, y = canvasHeight / two), radius = size.minDimension / 4 ) }
Figure 3. Using drawCircle
to put a circle in the center of the canvas. Past default, drawCircle
draws a filled circle, and then we don't need to specify that setting explicitly.
The cartoon functions have useful default parameters. For instance, by default drawRectangle()
fills its entire parent scope, and drawCircle()
has a radius equal to half of its parent'south shorter dimension. Every bit e'er with Kotlin, y'all tin can brand your code much simpler and clearer by taking reward of the default parameter values and only setting the parameters that you demand to change. You tin take advantage of this by providing explicit parameters to the DrawScope
drawing methods, since your drawn elements volition base their default settings on the settings of the parent scope.
DrawScope
As noted, each Etch Canvas
exposes a DrawScope
, a scoped drawing surroundings, where you actually issue your drawing commands.
For example, the following lawmaking draws a rectangle in the upper-left corner of the sail:
Canvas(modifier = Modifier.fillMaxSize()) { val canvasQuadrantSize = size / 2F drawRect( colour = Colour.Green, size = canvasQuadrantSize ) }
You tin use the function DrawScope.inset()
to adjust the default parameters of the current scope, changing the drawing boundaries and translating the drawings appropriately. Operations similar inset()
apply to all drawing operations within the corresponding lambda:
val canvasQuadrantSize = size / 2F inset(50F, 30F) { drawRect( color = Color.Green, size = canvasQuadrantSize ) }
DrawScope
offers other simple transformations, like rotate()
. For case, this code draws a rectangle that fills the middle 9th of the canvas:
val canvasSize = size val canvasWidth = size.width val canvasHeight = size.height drawRect( color = Color.Gray, topLeft = Beginning(x = canvasWidth / 3F, y = canvasHeight / 3F), size = canvasSize / 3F )
Figure four. Using drawRect
to draw a filled rectangle in the center of the screen.
You can rotate the rectangle past applying a rotation to its DrawScope
:
rotate(degrees = 45F) { drawRect( colour = Color.Gray, topLeft = Start(x = canvasWidth / 3F, y = canvasHeight / 3F), size = canvasSize / 3F ) }
Figure 5. We utilize rotate()
to use a rotation to the current drawing scope, which rotates the rectangle by 45 degrees.
If you want to apply multiple transformations to your drawings, the best approach is not to create nested DrawScope
environments. Instead, you should use the withTransform()
function, which creates and applies a unmarried transformation that combines all your desired changes. Using withTransform()
is more than efficient than making nested calls to individual transformations, because all the transformations are performed together in a single functioning, instead of Compose needing to calculate and save each of the nested transformations.
For case, this lawmaking applies both a translation and a rotation to the rectangle:
withTransform({ translate(left = canvasWidth / 5F) rotate(degrees = 45F) }) { drawRect( color = Color.Gray, topLeft = Offset(x = canvasWidth / 3F, y = canvasHeight / 3F), size = canvasSize / 3F ) }
Effigy six. Here nosotros utilise withTransform
to employ both a rotation and a translation, rotating the rectangle and shifting it to the left.
Source: https://developer.android.com/jetpack/compose/graphics
0 Response to "Android Program to Draw Multiple Circles With Varying Size"
Post a Comment