loloof64
11/27/2020, 10:35 AMloloof64
11/27/2020, 10:36 AM@Preview
@Composable
fun DragNDropComponent() {
Column(modifier = Modifier.size(100.dp).background(Color.Red)) {
var x by remember{ mutableStateOf(0f)}
var y by remember { mutableStateOf(0f)}
val offsetX = with(DensityAmbient.current) {
x.toDp()
}
val offsetY = with(DensityAmbient.current) {
y.toDp()
}
Column(modifier = Modifier
.offset(offsetX, offsetY)
.size(10.dp)
.background(Color.Blue)
.dragGestureFilter(
object : DragObserver {
override fun onDrag(dragDistance: Offset): Offset {
x += dragDistance.x
y += dragDistance.y
return dragDistance
}
override fun onCancel() {
// Here a need an animation instead
x = 0f
y = 0f
}
override fun onStart(downPosition: Offset) {
x = 0f
y = 0f
}
override fun onStop(velocity: Offset) {
// Here a need an animation instead
x = 0f
y = 0f
}
}
)
){}
}
}
loloof64
11/27/2020, 10:37 AMJoost Klitsie
11/27/2020, 11:19 AMJoost Klitsie
11/27/2020, 11:19 AMJoost Klitsie
11/27/2020, 11:20 AMJoost Klitsie
11/27/2020, 11:20 AMloloof64
11/27/2020, 12:17 PMloloof64
11/27/2020, 12:59 PM<https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationClockObservable|AnimationClockObservable>
I need to instantiate as a parameter of AnimatedFloat.loloof64
11/27/2020, 2:27 PM@Preview
@Composable
fun DragNDropComponent() {
Column(modifier = Modifier.size(100.dp).background(Color.Red)) {
var x by remember { mutableStateOf(0f) }
var y by remember { mutableStateOf(0f) }
var animationActive by remember { mutableStateOf(false) }
val animatedX = FloatPropKey()
val animatedY = FloatPropKey()
val positionAnimation = transitionDefinition<DndResetState> {
state(DndResetState.Start) {
this[animatedX] = 100f // setting to x does not help either
this[animatedY] = 200f // setting to y does not help either
}
state(DndResetState.End) {
this[animatedX] = 0f
this[animatedY] = 0f
}
transition {
animatedX using tween(durationMillis = 700, easing = FastOutSlowInEasing)
animatedY using tween(durationMillis = 700, easing = FastOutSlowInEasing)
}
}
val positionAnimationState = transition(
definition = positionAnimation,
initState = DndResetState.Start,
toState = DndResetState.End,
onStateChangeFinished = {
animationActive = false
}
)
val offsetX = with(DensityAmbient.current) {
(
if (animationActive) positionAnimationState[animatedX]
else x
).toDp()
}
val offsetY = with(DensityAmbient.current) {
(
if (animationActive) positionAnimationState[animatedY]
else y
).toDp()
}
Column(modifier = Modifier
.offset(offsetX, offsetY)
.size(10.dp)
.background(Color.Blue)
.dragGestureFilter(
object : DragObserver {
override fun onDrag(dragDistance: Offset): Offset {
x += dragDistance.x
y += dragDistance.y
return dragDistance
}
override fun onCancel() {
animationActive = true
x = 0f
y = 0f
}
override fun onStart(downPosition: Offset) {
x = 0f
y = 0f
}
override fun onStop(velocity: Offset) {
animationActive = true
x = 0f
y = 0f
}
}
)
) {}
}
}
loloof64
11/27/2020, 2:42 PM@Preview
@Composable
fun DragNDropComponent() {
Column(modifier = Modifier.size(300.dp).background(Color.Red)) {
var x by remember { mutableStateOf(0f) }
var y by remember { mutableStateOf(0f) }
var animationActive by remember { mutableStateOf(false) }
val animatedX = FloatPropKey()
val animatedY = FloatPropKey()
val positionAnimation = transitionDefinition<DndResetState> {
state(DndResetState.Start) {
this[animatedX] = x
this[animatedY] = y
}
state(DndResetState.End) {
this[animatedX] = 0f
this[animatedY] = 0f
}
transition {
animatedX using tween(durationMillis = 700, easing = FastOutSlowInEasing)
animatedY using tween(durationMillis = 700, easing = FastOutSlowInEasing)
}
}
val positionAnimationState = transition(
definition = positionAnimation,
initState = DndResetState.Start,
toState = DndResetState.End,
onStateChangeFinished = {
animationActive = false
x = 0f
y = 0f
}
)
val offsetX = with(DensityAmbient.current) {
(
if (animationActive) positionAnimationState[animatedX]
else x
).toDp()
}
val offsetY = with(DensityAmbient.current) {
(
if (animationActive) positionAnimationState[animatedY]
else y
).toDp()
}
Column(modifier = Modifier
.offset(offsetX, offsetY)
.size(10.dp)
.background(Color.Blue)
.dragGestureFilter(
object : DragObserver {
override fun onDrag(dragDistance: Offset): Offset {
x += dragDistance.x
y += dragDistance.y
return dragDistance
}
override fun onCancel() {
animationActive = true
}
override fun onStart(downPosition: Offset) {
x = 0f
y = 0f
}
override fun onStop(velocity: Offset) {
animationActive = true
}
}
)
) {}
}
}
loloof64
11/27/2020, 2:45 PMJoost Klitsie
11/27/2020, 3:02 PManimatedFloat
you don't have to worry about the clock or rememberingJoost Klitsie
11/27/2020, 3:18 PM@Preview
@Composable
fun DragAndDropComponent() {
Column(modifier = Modifier.size(300.dp).background(Color.Red)) {
val x = animatedFloat(0f)
val y = animatedFloat(0f)
Box(modifier = Modifier
.offset(
with(DensityAmbient.current) { x.value.toDp() },
with(DensityAmbient.current) { y.value.toDp() })
.size(10.dp)
.background(Color.Blue)
.dragGestureFilter(
object : DragObserver {
override fun onDrag(dragDistance: Offset): Offset {
x.animateTo(x.targetValue + dragDistance.x)
y.animateTo(y.targetValue + dragDistance.y)
return super.onDrag(dragDistance)
}
override fun onStop(velocity: Offset) {
super.onStop(velocity)
x.animateTo(0f)
y.animateTo(0f)
}
override fun onCancel() {
super.onCancel()
x.animateTo(0f)
y.animateTo(0f)
}
},
startDragImmediately = true
)
)
}
}
Joost Klitsie
11/27/2020, 3:20 PMJoost Klitsie
11/27/2020, 3:21 PM