diff --git a/constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/Transition.java b/constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/Transition.java index 4c5ad89cb..c66bc1f3c 100644 --- a/constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/Transition.java +++ b/constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/Transition.java @@ -33,6 +33,7 @@ import androidx.constraintlayout.core.widgets.ConstraintWidgetContainer; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; public class Transition implements TypedValues { @@ -78,7 +79,7 @@ public boolean hasOnSwipe() { } static class OnSwipe { - private String mAnchorId; + String mAnchorId; private int mAnchorSide; private StopEngine mEngine; public static final int ANCHOR_SIDE_TOP = 0; @@ -162,6 +163,10 @@ static class OnSwipe { }; private long mStart; + float getScale() { + return mDragScale; + } + float[] getDirection() { return TOUCH_DIRECTION[mDragDirection]; } @@ -362,9 +367,27 @@ public boolean isNotDone(float progress) { * @return the change in progress */ public float dragToProgress(float currentProgress, int baseW, int baseH, float dx, float dy) { - if (mOnSwipe == null || mOnSwipe.mAnchorId == null) { - WidgetState w = mState.values().stream().findFirst().get(); - return -dy / w.mParentHeight; + Collection widgets = mState.values(); + WidgetState childWidget = null; + for (WidgetState widget : widgets) { + childWidget = widget; + break; + } + if (mOnSwipe == null || childWidget == null) { + if (childWidget != null) { + return -dy / childWidget.mParentHeight; + } + return 1.0f; + } + if (mOnSwipe.mAnchorId == null) { + + float[] dir = mOnSwipe.getDirection(); + float motionDpDtX = childWidget.mParentHeight; + float motionDpDtY = childWidget.mParentHeight; + + float drag = (dir[0] != 0) ? dx * Math.abs(dir[0]) / motionDpDtX + : dy * Math.abs(dir[1]) / motionDpDtY; + return drag * mOnSwipe.getScale(); } WidgetState base = mState.get(mOnSwipe.mAnchorId); float[] dir = mOnSwipe.getDirection(); @@ -378,7 +401,7 @@ public float dragToProgress(float currentProgress, int baseW, int baseH, float d if (DEBUG) { Utils.log(" drag " + drag); } - return drag; + return drag * mOnSwipe.getScale(); } /** @@ -412,7 +435,7 @@ public void setTouchUp(float currentProgress, } float drag = (dir[0] != 0) ? velocityX / motionDpDt[0] : velocityY / motionDpDt[1]; - + drag *= mOnSwipe.getScale(); if (DEBUG) { Utils.log(" >>> velocity " + drag); Utils.log(" >>> mDuration " + mDuration); @@ -825,8 +848,8 @@ static class WidgetState { MotionWidget mMotionWidgetEnd; MotionWidget mMotionWidgetInterpolated; KeyCache mKeyCache = new KeyCache(); - private int mParentHeight = -1; - private int mParentWidth = -1; + int mParentHeight = -1; + int mParentWidth = -1; WidgetState() { mStart = new WidgetFrame(); diff --git a/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/MainActivity.kt b/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/MainActivity.kt index 6e18d1fe0..19cc73697 100644 --- a/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/MainActivity.kt +++ b/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/MainActivity.kt @@ -37,7 +37,7 @@ import com.google.accompanist.coil.rememberCoilPainter class MainActivity : AppCompatActivity() { private var mFrameLayout: FrameLayout? = null private var composeNum = 0 - private val START_NUMBER = 6 + private val START_NUMBER = 48 private var demos:ArrayList = ArrayList() var map = HashMap(); val linkServer = LinkServer() @@ -153,6 +153,8 @@ class MainActivity : AppCompatActivity() { demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeExperiment() } }) demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample1() } }) demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample2() } }) + demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample3() } }) + demos.add(object : CompFunc { @Composable override fun Run() { MultiSwipe() } }) demos.add(object : CompFunc { @Composable override fun Run() { MotionArc() } }) demos.add(object : CompFunc { @Composable override fun Run() { MotionEasing() } }) diff --git a/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/OnSwipeExperiment.kt b/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/OnSwipeExperiment.kt index 189d977b7..4030cd227 100644 --- a/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/OnSwipeExperiment.kt +++ b/projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/OnSwipeExperiment.kt @@ -260,6 +260,76 @@ fun OnSwipeSample2() { .background(Color.White), motionScene = MotionScene(content = scene), ) { + Text( + text = "on Swipe example \n"+ + " onSwipe: {\n" + + " anchor: 'box',\n" + + " direction: 'end',\n" + + " side: 'start',\n" + + " mode: 'spring'\n" + + " }" + ) + Box( + modifier = Modifier + .background(Color.Green) + .layoutId("box") + ) + } + } +} + +@Preview +@Composable +fun OnSwipeSample3() { + + var scene = + """ + { + ConstraintSets: { + start: { + box: { + width: 50, height: 50, + bottom: ['parent', 'bottom', 70], + start: ['parent', 'start', 170], + } + }, + end: { + + box: { + width: 50, height: 50, + top: ['parent', 'top', 170], + end: ['parent', 'end', 170], + } + } + }, + Transitions: { + default: { + from: 'start', + to: 'end', + onSwipe: { + direction: 'end', + mode: 'spring', + scale: .3, + } + } + } + } + """.trimIndent() + + Column { + MotionLayout( + modifier = Modifier + .fillMaxSize() + .background(Color.White), + motionScene = MotionScene(content = scene), + ) { + Text( + text = "on Swipe example \n"+ + " onSwipe: {\n" + + " direction: 'end',\n" + + " mode: 'spring'\n" + + " }" + ) Box( modifier = Modifier .background(Color.Green)