-
-
Save tasjapr/ed66e6ba1d70cc1c6ff65ae38500a7b4 to your computer and use it in GitHub Desktop.
Draw a colored shadow in Android Jetpack Compose
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
/* | |
Copyright 2020 Cedric Kring. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.drawBehind | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.Paint | |
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.graphics.toArgb | |
fun Modifier.drawColoredShadow( | |
color: Color, | |
alpha: Float = 0.2f, | |
borderRadius: Dp = 0.dp, | |
shadowRadius: Dp = 20.dp, | |
offsetY: Dp = 0.dp, | |
offsetX: Dp = 0.dp | |
) = this.drawBehind { | |
val transparentColor = color.copy(alpha = 0.0f).toArgb() | |
val shadowColor = color.copy(alpha = alpha).toArgb() | |
this.drawIntoCanvas { | |
val paint = Paint() | |
val frameworkPaint = paint.asFrameworkPaint() | |
frameworkPaint.color = transparentColor | |
frameworkPaint.setShadowLayer( | |
shadowRadius.toPx(), | |
offsetX.toPx(), | |
offsetY.toPx(), | |
shadowColor | |
) | |
it.drawRoundRect( | |
0f, | |
0f, | |
this.size.width, | |
this.size.height, | |
borderRadius.toPx(), | |
borderRadius.toPx(), | |
paint | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it doesn't work either