-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
♻️ [ShareableCardContent] Display a shadow instead of a border around the profile picture. #906
base: main
Are you sure you want to change the base?
Changes from all commits
a6da413
c0b750e
54230f9
34f00d1
a3e7f02
8bd53f5
a386dc2
88c9986
588db37
d0f8deb
0b1da73
519a2bb
6006238
081f99e
5b2824d
6867e80
8b16b8e
e7bce0f
7246053
ddd697d
5690ecd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package io.github.droidkaigi.confsched.profilecard.component | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.Canvas | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.offset | ||
|
@@ -18,8 +18,13 @@ import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.drawWithContent | ||
import androidx.compose.ui.draw.rotate | ||
import androidx.compose.ui.geometry.CornerRadius | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Brush | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.drawscope.rotate | ||
import androidx.compose.ui.graphics.layer.GraphicsLayer | ||
import androidx.compose.ui.graphics.layer.drawLayer | ||
import androidx.compose.ui.platform.LocalDensity | ||
|
@@ -109,42 +114,74 @@ private fun ShareableCardContent( | |
.background(LocalProfileCardTheme.current.primaryColor), | ||
) { | ||
Box(modifier = Modifier.padding(vertical = with(density) { verticalPaddingPx.toDp() })) { | ||
backImage?.let { | ||
Image( | ||
bitmap = it, | ||
contentDescription = null, | ||
modifier = Modifier | ||
.offset( | ||
x = with(density) { offsetXBackPx.toDp() }, | ||
y = with(density) { offsetYBackPx.toDp() }, | ||
) | ||
.rotate(10f) | ||
.size( | ||
width = with(density) { cardWidthPx.toDp() }, | ||
height = with(density) { cardHeightPx.toDp() }, | ||
), | ||
backImage?.let { backBitmap -> | ||
ShadowedImage( | ||
imageBitmap = backBitmap, | ||
offsetX = offsetXBackPx, | ||
offsetY = offsetYBackPx, | ||
rotation = 10f, | ||
cardWidthPx = cardWidthPx, | ||
cardHeightPx = cardHeightPx, | ||
) | ||
} | ||
frontImage?.let { | ||
Image( | ||
bitmap = it, | ||
contentDescription = null, | ||
modifier = Modifier | ||
.offset( | ||
x = with(density) { offsetXFrontPx.toDp() }, | ||
y = with(density) { offsetYFrontPx.toDp() }, | ||
) | ||
.rotate(-12.2f) | ||
.size( | ||
width = with(density) { cardWidthPx.toDp() }, | ||
height = with(density) { cardHeightPx.toDp() }, | ||
), | ||
frontImage?.let { frontBitmap -> | ||
ShadowedImage( | ||
imageBitmap = frontBitmap, | ||
offsetX = offsetXFrontPx, | ||
offsetY = offsetYFrontPx, | ||
rotation = -12.2f, | ||
cardWidthPx = cardWidthPx, | ||
cardHeightPx = cardHeightPx, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ShadowedImage( | ||
imageBitmap: ImageBitmap, | ||
offsetX: Float, | ||
offsetY: Float, | ||
rotation: Float, | ||
cardWidthPx: Int, | ||
cardHeightPx: Int, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val sweepGradient = Brush.sweepGradient( | ||
colors = listOf( | ||
Color.Black.copy(alpha = 0.1f), | ||
Color.Transparent, | ||
), | ||
) | ||
val density = LocalDensity.current | ||
|
||
Canvas( | ||
modifier = modifier | ||
.offset( | ||
x = with(density) { offsetX.toDp() }, | ||
y = with(density) { offsetY.toDp() }, | ||
) | ||
.rotate(rotation) | ||
.size( | ||
width = with(density) { cardWidthPx.toDp() }, | ||
height = with(density) { cardHeightPx.toDp() }, | ||
), | ||
) { | ||
rotate(degrees = 180f) { | ||
for (i in 1..15) { | ||
drawRoundRect( | ||
Comment on lines
+172
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was impossible to make a shadow like Figma with just one brush, no matter how hard I tried. |
||
brush = sweepGradient, | ||
size = Size(cardWidthPx.toFloat(), cardHeightPx.toFloat()), | ||
topLeft = Offset(-i.toFloat(), -i.toFloat()), | ||
cornerRadius = CornerRadius(with(density) { 16.toDp().toPx() }), | ||
) | ||
} | ||
} | ||
drawImage(imageBitmap) | ||
Comment on lines
+171
to
+181
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you use Canvas, you won't even need to use Image. |
||
} | ||
} | ||
|
||
@Composable | ||
@Preview | ||
fun ShareableCardPreview() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only brush that could create a shadowy effect like Figma was the sweepGradient.
RadialGradient is good for drawing perfect circles, but it is not suitable for drawing shadows on rectangular cards like this one.
No matter how you try, it's impossible to achieve the same kind of rendering as Figma. 🙏