Created
November 13, 2023 21:28
-
-
Save dladukedev/a642e5b1eda76a807cb1119c47134b8f to your computer and use it in GitHub Desktop.
Alignment vs Absolute Alignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dladukedev.alignment | |
import androidx.compose.foundation.Canvas | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.CompositionLocalProvider | |
import androidx.compose.ui.AbsoluteAlignment | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.rotate | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.Path | |
import androidx.compose.ui.graphics.StrokeCap | |
import androidx.compose.ui.graphics.drawscope.Stroke | |
import androidx.compose.ui.platform.LocalLayoutDirection | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.LayoutDirection | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun Corner(modifier: Modifier = Modifier) { | |
Box(modifier = modifier.size(24.dp)) { | |
Canvas(Modifier.fillMaxSize()) { | |
val cornerPath = Path().apply { | |
lineTo(0f, size.height) | |
moveTo(0f, 0f) | |
lineTo(size.width, 0f) | |
} | |
drawPath(cornerPath, color = Color.White, style = Stroke(width = 4f, cap = StrokeCap.Round)) | |
} | |
} | |
} | |
@Composable | |
fun AlignmentExample() { | |
Box(modifier = Modifier.size(200.dp).padding(20.dp)) { | |
Corner(modifier = Modifier.align(Alignment.TopStart).rotate(0f)) | |
Corner(modifier = Modifier.align(Alignment.TopEnd).rotate(90f)) | |
Corner(modifier = Modifier.align(Alignment.BottomEnd).rotate(180f)) | |
Corner(modifier = Modifier.align(Alignment.BottomStart).rotate(270f)) | |
} | |
} | |
@Composable | |
fun AbsoluteAlignmentExample() { | |
Box(modifier = Modifier.size(200.dp).padding(20.dp)) { | |
Corner(modifier = Modifier.align(AbsoluteAlignment.TopLeft).rotate(0f)) | |
Corner(modifier = Modifier.align(AbsoluteAlignment.TopRight).rotate(90f)) | |
Corner(modifier = Modifier.align(AbsoluteAlignment.BottomRight).rotate(180f)) | |
Corner(modifier = Modifier.align(AbsoluteAlignment.BottomLeft).rotate(270f)) | |
} | |
} | |
@Preview | |
@Composable | |
fun PreviewAlignmentExample() { | |
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { | |
AlignmentExample() | |
} | |
} | |
@Preview | |
@Composable | |
fun PreviewAbsoluteAlignmentExample() { | |
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { | |
AbsoluteAlignmentExample() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment