|
| 1 | +package com.sackcentury.shinebuttonlib |
| 2 | + |
| 3 | +import androidx.compose.animation.core.* |
| 4 | +import androidx.compose.foundation.Canvas |
| 5 | +import androidx.compose.foundation.clickable |
| 6 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 7 | +import androidx.compose.foundation.layout.Box |
| 8 | +import androidx.compose.foundation.layout.size |
| 9 | +import androidx.compose.runtime.* |
| 10 | +import androidx.compose.ui.Alignment |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.geometry.Offset |
| 13 | +import androidx.compose.ui.geometry.Size |
| 14 | +import androidx.compose.ui.graphics.Color |
| 15 | +import androidx.compose.ui.graphics.ImageBitmap |
| 16 | +import androidx.compose.ui.graphics.Paint |
| 17 | +import androidx.compose.ui.graphics.PaintingStyle |
| 18 | +import androidx.compose.ui.graphics.drawscope.Stroke |
| 19 | +import androidx.compose.ui.graphics.drawscope.drawIntoCanvas |
| 20 | +import androidx.compose.ui.graphics.nativeCanvas |
| 21 | +import androidx.compose.ui.graphics.painter.Painter |
| 22 | +import androidx.compose.ui.res.imageResource |
| 23 | +import androidx.compose.ui.unit.Dp |
| 24 | +import androidx.compose.ui.unit.dp |
| 25 | +import kotlin.math.PI |
| 26 | +import kotlin.math.cos |
| 27 | +import kotlin.math.sin |
| 28 | + |
| 29 | +import androidx.compose.ui.graphics.drawscope.drawIntoCanvas |
| 30 | +import androidx.compose.ui.graphics.nativeCanvas |
| 31 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 32 | +import androidx.compose.ui.graphics.vector.rememberVectorPainter |
| 33 | +import androidx.compose.ui.graphics.graphicsLayer |
| 34 | +import androidx.compose.ui.graphics.drawscope.translate |
| 35 | + |
| 36 | +/** |
| 37 | + * A native Jetpack Compose implementation of the ShineButton. |
| 38 | + * |
| 39 | + * @param isChecked The current checked state. |
| 40 | + * @param onCheckedChange Callback when the state changes. |
| 41 | + * @param modifier Modifier for the button. |
| 42 | + * @param shape The Vector image to use for the button icon (e.g., Icons.Default.Favorite). |
| 43 | + * @param btnColor Base color of the button (unchecked). |
| 44 | + * @param btnFillColor Color when the button is checked. |
| 45 | + * @param shineColor Primary color for shine particles. |
| 46 | + * @param shineSize Size of the button. |
| 47 | + * @param shineCount Number of shine particles. |
| 48 | + */ |
| 49 | +@Composable |
| 50 | +fun ShineButtonCompose( |
| 51 | + isChecked: Boolean, |
| 52 | + onCheckedChange: (Boolean) -> Unit, |
| 53 | + shape: ImageVector, |
| 54 | + modifier: Modifier = Modifier, |
| 55 | + btnColor: Color = Color.Gray, |
| 56 | + btnFillColor: Color = Color.Red, |
| 57 | + shineColor: Color = Color.Red, |
| 58 | + shineSize: Dp = 50.dp, |
| 59 | + shineCount: Int = 8, |
| 60 | + animDuration: Int = 1000, |
| 61 | + allowRandomColor: Boolean = false |
| 62 | +) { |
| 63 | + val interactionSource = remember { MutableInteractionSource() } |
| 64 | + val painter = rememberVectorPainter(image = shape) |
| 65 | + |
| 66 | + // Animation states |
| 67 | + val transition = updateTransition(targetState = isChecked, label = "ShineTransition") |
| 68 | + |
| 69 | + val shineProgress by transition.animateFloat( |
| 70 | + transitionSpec = { |
| 71 | + if (targetState) { |
| 72 | + tween(durationMillis = animDuration, easing = LinearOutSlowInEasing) |
| 73 | + } else { |
| 74 | + tween(durationMillis = 0) |
| 75 | + } |
| 76 | + }, |
| 77 | + label = "ShineProgress" |
| 78 | + ) { checked -> |
| 79 | + if (checked) 1f else 0f |
| 80 | + } |
| 81 | + |
| 82 | + val scale by transition.animateFloat( |
| 83 | + transitionSpec = { |
| 84 | + if (targetState) { |
| 85 | + keyframes { |
| 86 | + durationMillis = 500 |
| 87 | + 0.4f at 0 |
| 88 | + 1.1f at 250 |
| 89 | + 1.0f at 500 |
| 90 | + } |
| 91 | + } else { |
| 92 | + tween(durationMillis = 200) |
| 93 | + } |
| 94 | + }, |
| 95 | + label = "Scale" |
| 96 | + ) { checked -> |
| 97 | + 1.0f |
| 98 | + } |
| 99 | + |
| 100 | + val particleColors = remember(allowRandomColor) { |
| 101 | + if (allowRandomColor) { |
| 102 | + listOf( |
| 103 | + Color(0xFFFFFF99), Color(0xFFFFCCCC), Color(0xFF996699), |
| 104 | + Color(0xFFFF6666), Color(0xFFFFFF66), Color(0xFFF44336), |
| 105 | + Color(0xFF666666), Color(0xFFCCCC00) |
| 106 | + ) |
| 107 | + } else { |
| 108 | + listOf(shineColor) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + Box( |
| 113 | + modifier = modifier |
| 114 | + .size(shineSize) |
| 115 | + .clickable( |
| 116 | + interactionSource = interactionSource, |
| 117 | + indication = null |
| 118 | + ) { |
| 119 | + onCheckedChange(!isChecked) |
| 120 | + }, |
| 121 | + contentAlignment = Alignment.Center |
| 122 | + ) { |
| 123 | + // Shine Particles Layer |
| 124 | + if (shineProgress > 0f && shineProgress < 1f) { |
| 125 | + Canvas(modifier = Modifier.size(shineSize * 2.5f)) { |
| 126 | + val center = Offset(size.width / 2, size.height / 2) |
| 127 | + val maxRadius = (shineSize.toPx() * 1.2f) |
| 128 | + val currentRadius = maxRadius * shineProgress |
| 129 | + |
| 130 | + for (i in 0 until shineCount) { |
| 131 | + val angle = (360f / shineCount) * i |
| 132 | + val angleRad = angle * (PI / 180f).toFloat() |
| 133 | + |
| 134 | + val x = center.x + cos(angleRad) * currentRadius |
| 135 | + val y = center.y + sin(angleRad) * currentRadius |
| 136 | + |
| 137 | + val color = if (allowRandomColor) particleColors[i % particleColors.size] else shineColor |
| 138 | + |
| 139 | + // Main particle |
| 140 | + drawCircle( |
| 141 | + color = color, |
| 142 | + radius = 3.dp.toPx() * (1f - shineProgress), |
| 143 | + center = Offset(x, y) |
| 144 | + ) |
| 145 | + |
| 146 | + // Secondary small particle |
| 147 | + val offsetAngleRad = (angle + 20f) * (PI / 180f).toFloat() |
| 148 | + val x2 = center.x + cos(offsetAngleRad) * (currentRadius * 0.85f) |
| 149 | + val y2 = center.y + sin(offsetAngleRad) * (currentRadius * 0.85f) |
| 150 | + |
| 151 | + drawCircle( |
| 152 | + color = color.copy(alpha = 0.6f), |
| 153 | + radius = 1.5.dp.toPx() * (1f - shineProgress), |
| 154 | + center = Offset(x2, y2) |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Center Icon Layer |
| 161 | + val iconColor = if (isChecked) btnFillColor else btnColor |
| 162 | + Box( |
| 163 | + modifier = Modifier |
| 164 | + .size(shineSize) |
| 165 | + .graphicsLayer( |
| 166 | + scaleX = scale, |
| 167 | + scaleY = scale |
| 168 | + ) |
| 169 | + ) { |
| 170 | + Canvas(modifier = Modifier.matchParentSize()) { |
| 171 | + with(painter) { |
| 172 | + draw( |
| 173 | + size = size, |
| 174 | + colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(iconColor) |
| 175 | + ) |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments