Colton Idle
05/04/2023, 12:08 AMCompositingStrategy.Always
? Can't seem to use it even though I'm on 1.4romainguy
05/04/2023, 12:12 AMAlways
romainguy
05/04/2023, 12:12 AMColton Idle
05/04/2023, 12:54 AMColton Idle
05/04/2023, 12:54 AMColton Idle
05/04/2023, 1:05 AMColton Idle
05/04/2023, 1:58 AMColton Idle
05/04/2023, 1:59 AMimport androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment.Companion.BottomEnd
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.center
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun MyBoxAndBadge() {
Box(
Modifier
.size(48.dp)
.graphicsLayer {
compositingStrategy = CompositingStrategy.Offscreen
}
) {
Box(
Modifier
.fillMaxSize()
.background(Color.Green)
)
Dot(
clipWidth = 2.dp,
modifier = Modifier
.size(8.dp)
.align(BottomEnd)
.offset(4.dp, 4.dp)
)
}
}
@Composable
private fun Dot(clipWidth: Dp, modifier: Modifier) {
val pxValue = LocalDensity.current.run { clipWidth.toPx() }
val stroke = remember(pxValue) { Stroke(width = pxValue) }
Box(
modifier
.drawWithContent {
drawContent()
drawCircle(
Color.Black,
size.minDimension / 2,
size.center,
style = stroke,
blendMode = BlendMode.Clear
)
}
.background(Color.Red, CircleShape)
)
}
@Preview
@Composable
fun PreviewIt() {
Box(
Modifier
.background(Color.White)
.padding(16.dp)
) {
MyBoxAndBadge()
}
}
romainguy
05/04/2023, 2:04 AMOffscreen
strategy does what it says on the tin. It renders into a texture/bitmap, which causes clipping since we can’t draw outside of that texture 🙂romainguy
05/04/2023, 2:05 AMColton Idle
05/04/2023, 2:05 AMColton Idle
05/04/2023, 2:05 AMromainguy
05/04/2023, 2:05 AMoffset()
since it doesn’t affect layoutromainguy
05/04/2023, 2:05 AMColton Idle
05/04/2023, 2:05 AMromainguy
05/04/2023, 2:05 AMColton Idle
05/04/2023, 2:06 AMColton Idle
05/04/2023, 2:06 AMromainguy
05/04/2023, 2:06 AMColton Idle
05/04/2023, 2:06 AMromainguy
05/04/2023, 2:06 AMColton Idle
05/04/2023, 2:06 AMColton Idle
05/04/2023, 2:07 AMromainguy
05/04/2023, 2:07 AMromainguy
05/04/2023, 2:07 AMromainguy
05/04/2023, 2:07 AMColton Idle
05/04/2023, 2:09 AMColton Idle
05/04/2023, 2:09 AMColton Idle
05/04/2023, 2:10 AM