-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
253 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ android { | |
} | ||
|
||
dependencies { | ||
implementation(project(":base")) | ||
} |
55 changes: 55 additions & 0 deletions
55
.../compose-layout/src/main/java/com/sample/feature/compose_layout/LayoutSampleModifiyRow.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.sample.feature.compose_layout | ||
|
||
import android.util.Log | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun LayoutSampleModifyRow( | ||
modifier: Modifier = Modifier, | ||
content: @Composable () -> Unit = { | ||
Text(text = "first") | ||
Spacer(modifier = Modifier.width(20.dp)) | ||
Text(text = "second") | ||
Spacer(modifier = Modifier.width(20.dp)) | ||
Text(text = "third") | ||
} | ||
) { | ||
Layout( | ||
modifier = modifier | ||
.fillMaxSize() | ||
.background(Color.Gray), | ||
content = content | ||
) { measureable: List<Measurable>, constraints: Constraints -> | ||
val placeables = measureable.map { it.measure(constraints.copy(minWidth = 0, minHeight = 0)) } | ||
|
||
layout(width = constraints.maxWidth, height = constraints.maxHeight) { | ||
var x = 0 | ||
placeables.forEach { | ||
Log.d(TAG, "place x $x ") | ||
it.placeRelative(x, y = 0) | ||
x += it.width | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun LayoutSampleModifyRowPreview() { | ||
LayoutSampleModifyRow() | ||
} | ||
|
||
val TAG = "TAG" |
49 changes: 49 additions & 0 deletions
49
...ompose-layout/src/main/java/com/sample/feature/compose_layout/LayoutSampleModifyColumn.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.sample.feature.compose_layout | ||
|
||
import android.util.Log | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.Constraints | ||
|
||
@Composable | ||
fun LayoutSampleModifyColumn( | ||
modifier: Modifier = Modifier, | ||
content: @Composable () -> Unit = { | ||
Text(text = "first", modifier = Modifier.background(Color.Black)) | ||
Text(text = "second", modifier = Modifier.background(Color.Blue)) | ||
Text(text = "third", modifier = Modifier.background(Color.Cyan)) | ||
} | ||
) { | ||
Layout( | ||
modifier = modifier | ||
.fillMaxSize() | ||
.background(Color.Gray), | ||
content = content | ||
) { measureable: List<Measurable>, constraints: Constraints -> | ||
val minWidthHeightZeroConstraint = constraints.copy(minWidth = 0, minHeight = 0) | ||
val placeables = measureable.map { it.measure(minWidthHeightZeroConstraint) } | ||
|
||
layout(width = constraints.maxWidth, height = constraints.maxHeight) { | ||
var y = 0 | ||
placeables.forEach { placeable -> | ||
Log.d(TAG, "y $y") | ||
placeable.placeRelative(x = 0, y = y) | ||
y += placeable.height | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun LayoutSampleModifyColumnPreview() { | ||
LayoutSampleModifyColumn() | ||
} |
80 changes: 80 additions & 0 deletions
80
.../compose-layout/src/main/java/com/sample/feature/compose_layout/LayoutSampleModifyGrid.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.sample.feature.compose_layout | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.layout.Placeable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.LayoutDirection | ||
import androidx.compose.ui.unit.dp | ||
import kotlin.random.Random | ||
|
||
private fun getRandomColor(): Color { | ||
val randomColorValue = Random.nextLong(from = 0xFF000000, until = 0xFFFFFFFF) | ||
return Color(randomColorValue) | ||
} | ||
|
||
@Composable | ||
fun LayoutSampleModifyGrid( | ||
modifier: Modifier = Modifier, | ||
columns: Int = 2, | ||
itemPadding: PaddingValues = PaddingValues(10.dp), | ||
content: @Composable () -> Unit = { | ||
val sizes = listOf(150.dp, 200.dp, 300.dp, 150.dp, 100.dp, 100.dp) | ||
val createBox: @Composable (Dp) -> Unit = { size -> | ||
Box( | ||
modifier = Modifier | ||
.size(size) | ||
.background(getRandomColor()) | ||
) | ||
} | ||
sizes.forEach { size -> | ||
createBox(size) | ||
} | ||
} | ||
) { | ||
Layout( | ||
modifier = modifier, | ||
content = content | ||
) { measurables: List<Measurable>, constraints: Constraints -> | ||
val columnWidth = constraints.maxWidth / columns | ||
val itemConstraints = constraints.copy(maxWidth = columnWidth) | ||
val left = itemPadding.calculateLeftPadding(LayoutDirection.Ltr).roundToPx() | ||
val right = itemPadding.calculateRightPadding(LayoutDirection.Ltr).roundToPx() | ||
val top = itemPadding.calculateTopPadding().roundToPx() | ||
val bottom = itemPadding.calculateBottomPadding().roundToPx() | ||
|
||
val placeables: List<List<Placeable>> = measurables.map { measurable -> | ||
measurable.measure(itemConstraints) | ||
}.chunked(columns) | ||
|
||
val gridHeight = placeables.sumOf { gridPlaceables -> gridPlaceables.maxOf { it.height } } | ||
|
||
layout(constraints.maxWidth, gridHeight) { | ||
var y = top | ||
placeables.forEach { currentColumnPlaceables -> | ||
val currentColumnMaxHeight = currentColumnPlaceables.maxOf { it.height } | ||
var x = left | ||
currentColumnPlaceables.forEach { placeable -> | ||
placeable.place(x, y) | ||
x += placeable.width + right | ||
} | ||
y += currentColumnMaxHeight + bottom | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun LayoutSampleModifyGridPreview() { | ||
LayoutSampleModifyGrid() | ||
} |
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