Created
November 26, 2021 07:13
-
-
Save c5inco/fe178ab6e1eecaac426f45b6ce2bc696 to your computer and use it in GitHub Desktop.
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
import android.graphics.Matrix | |
import android.graphics.RectF | |
import androidx.compose.foundation.Canvas | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.material.Surface | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.* | |
import androidx.compose.ui.platform.LocalDensity | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
@Preview | |
@Composable | |
fun Squircle( | |
modifier: Modifier = Modifier | |
) { | |
Surface { | |
Box( | |
Modifier.padding(20.dp), | |
contentAlignment = Alignment.Center | |
) { | |
val density = LocalDensity.current | |
val targetSize = 200f | |
val baseSize = 60f | |
Canvas(modifier = modifier.size(targetSize.dp)) { | |
val path = Path() | |
path.moveTo(0f, 30f) | |
path.cubicTo(0f, 20.6812f, 0f, 16.0218f, 1.52241f, 12.3463f) | |
path.cubicTo(3.55229f, 7.44577f, 7.44577f, 3.55229f, 12.3463f, 1.52241f) | |
path.cubicTo(16.0218f, 0f, 20.6812f, 0f, 30f, 0f) | |
path.cubicTo(39.3188f, 0f, 43.9782f, 0f, 47.6537f, 1.52241f) | |
path.cubicTo(52.5542f, 3.55229f, 56.4477f, 7.44577f, 58.4776f, 12.3463f) | |
path.cubicTo(60f, 16.0218f, 60f, 20.6812f, 60f, 30f) | |
path.cubicTo(60f, 39.3188f, 60f, 43.9782f, 58.4776f, 47.6537f) | |
path.cubicTo(56.4477f, 52.5542f, 52.5542f, 56.4477f, 47.6537f, 58.4776f) | |
path.cubicTo(43.9782f, 60f, 39.3188f, 60f, 30f, 60f) | |
path.cubicTo(20.6812f, 60f, 16.0218f, 60f, 12.3463f, 58.4776f) | |
path.cubicTo(7.44577f, 56.4477f, 3.55229f, 52.5542f, 1.52241f, 47.6537f) | |
path.cubicTo(0f, 43.9782f, 0f, 39.3188f, 0f, 30f) | |
path.close() | |
val bounds = RectF() | |
val aPath = path.asAndroidPath() | |
aPath.computeBounds(bounds, true) | |
val scaleMatrix = Matrix() | |
val targetScale = targetSize / baseSize * density.density | |
scaleMatrix.setScale(targetScale, targetScale,0f, 0f) | |
aPath.transform(scaleMatrix) | |
drawPath( | |
path = aPath.asComposePath(), | |
brush = SolidColor(Color(0xff555555)) | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment