Created
March 4, 2022 18:21
-
-
Save PSPanishetti/7097035147452606d6c69b0056895cac to your computer and use it in GitHub Desktop.
Spaghetti code for working widget that attached to accelerometer in android.
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.content.Context | |
import android.hardware.Sensor | |
import android.hardware.SensorEvent | |
import android.hardware.SensorEventListener | |
import android.hardware.SensorManager | |
import androidx.compose.animation.ExperimentalAnimationApi | |
import androidx.compose.animation.core.animateFloatAsState | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material.Card | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.ExperimentalComposeUiApi | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.blur | |
import androidx.compose.ui.geometry.Size | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.TransformOrigin | |
import androidx.compose.ui.graphics.graphicsLayer | |
import androidx.compose.ui.layout.onGloballyPositioned | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.res.painterResource | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
import com.p2.swasth.R | |
import com.p2.swasth.utils.logIt | |
import kotlin.math.abs | |
const val maxAngle = 4f | |
const val maxYAngle = 1f | |
const val maxXAxis = 15f | |
const val maxYAxis = 10f | |
@ExperimentalAnimationApi | |
@ExperimentalComposeUiApi | |
@Preview | |
@Composable | |
fun Parallax() { | |
val context = LocalContext.current | |
val xOffset = remember { mutableStateOf(0.0f) } | |
val yOffset = remember { mutableStateOf(0.0f) } | |
LaunchedEffect( | |
key1 = Unit, | |
block = { | |
val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | |
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | |
sensorManager.registerListener( | |
object : SensorEventListener { | |
override fun onSensorChanged(event: SensorEvent?) { | |
val differenceX = abs(event?.values?.get(0) ?: 0f - xOffset.value) | |
val differenceY = abs(event?.values?.get(1) ?: 0f - yOffset.value) | |
if (differenceX >= 0.5f) { | |
xOffset.value = (event?.values?.get(0) ?: 0f) * (-1) | |
} | |
if (differenceY >= 0.5f) { | |
yOffset.value = (event?.values?.get(1) ?: 0f) * (-1) | |
} | |
} | |
override fun onAccuracyChanged( | |
sensor: Sensor?, | |
accuracy: Int | |
) { | |
} | |
}, | |
sensor, | |
10000 | |
) | |
} | |
) | |
Column( | |
horizontalAlignment = Alignment.CenterHorizontally, | |
modifier = Modifier | |
.fillMaxSize() | |
.background(Color.White) | |
) { | |
Box( | |
contentAlignment = Alignment.Center, | |
modifier = Modifier | |
.weight(1f) | |
.fillMaxSize() | |
.padding(0.dp) | |
) { | |
Box( | |
modifier = Modifier | |
.graphicsLayer( | |
transformOrigin = TransformOrigin( | |
0.5f, | |
0.5f | |
), | |
rotationY = animateFloatAsState(xOffset.value*maxAngle).value, | |
rotationX= animateFloatAsState(yOffset.value*maxYAngle).value, | |
cameraDistance = 16.dp.value, | |
translationX = animateFloatAsState( | |
getMyTranslation( | |
xOffset.value, | |
maxXAxis - 3f | |
) | |
).value, | |
translationY = -animateFloatAsState( | |
getMyTranslation( | |
yOffset.value - 0.2f, | |
maxYAxis - 1f | |
) | |
).value | |
) | |
.fillMaxWidth(0.8f) | |
.fillMaxHeight(0.8f) | |
.align(Alignment.Center) | |
) { | |
Card( | |
elevation = 50.dp, | |
backgroundColor = MaterialTheme.colors.onSurface.copy(0.2f), | |
) { | |
Column(modifier = Modifier.fillMaxHeight(0.9f).fillMaxWidth(1f)) { | |
} | |
} | |
} | |
Box( | |
modifier = Modifier | |
.graphicsLayer( | |
transformOrigin = TransformOrigin( | |
0.5f, | |
0.5f | |
), | |
rotationY = animateFloatAsState(xOffset.value*maxAngle).value, | |
rotationX= animateFloatAsState(yOffset.value*maxYAngle | |
).value, | |
cameraDistance = 16.dp.value, | |
translationX = animateFloatAsState( | |
getMyTranslation( | |
xOffset.value, | |
maxXAxis+10f | |
) | |
).value, | |
translationY = -animateFloatAsState( | |
getMyTranslation( | |
yOffset.value, | |
maxYAxis+10f | |
) | |
).value | |
) | |
.fillMaxWidth(0.8f) | |
.fillMaxHeight(0.8f) | |
.align(Alignment.Center) | |
) { | |
Card(elevation = 50.dp) { | |
Image( | |
painterResource(id = R.drawable.nature_image), | |
contentDescription = "Parallax Background", | |
modifier = Modifier.blur(10.dp) | |
) | |
} | |
} | |
} | |
} | |
} | |
fun getMyTranslation( | |
angle: Float, | |
maxDistance: Float | |
): Float { | |
return (angle) * maxDistance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment