Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit ef01f03

Browse files
authored
add support for no Anchor onSwipe (#617)
* fix desig time lockout of on click * fix ConstraintLayout Compose chain * add support for weights and margins * resolve comments & clean up spaces * resolve comments & clean up spaces * resolve oscar comments * add support for onSwipe to work with and Anchor
1 parent f68c9aa commit ef01f03

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/Transition.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import androidx.constraintlayout.core.widgets.ConstraintWidgetContainer;
3434

3535
import java.util.ArrayList;
36+
import java.util.Collection;
3637
import java.util.HashMap;
3738

3839
public class Transition implements TypedValues {
@@ -78,7 +79,7 @@ public boolean hasOnSwipe() {
7879
}
7980

8081
static class OnSwipe {
81-
private String mAnchorId;
82+
String mAnchorId;
8283
private int mAnchorSide;
8384
private StopEngine mEngine;
8485
public static final int ANCHOR_SIDE_TOP = 0;
@@ -162,6 +163,10 @@ static class OnSwipe {
162163
};
163164
private long mStart;
164165

166+
float getScale() {
167+
return mDragScale;
168+
}
169+
165170
float[] getDirection() {
166171
return TOUCH_DIRECTION[mDragDirection];
167172
}
@@ -362,9 +367,27 @@ public boolean isNotDone(float progress) {
362367
* @return the change in progress
363368
*/
364369
public float dragToProgress(float currentProgress, int baseW, int baseH, float dx, float dy) {
365-
if (mOnSwipe == null || mOnSwipe.mAnchorId == null) {
366-
WidgetState w = mState.values().stream().findFirst().get();
367-
return -dy / w.mParentHeight;
370+
Collection<WidgetState> widgets = mState.values();
371+
WidgetState childWidget = null;
372+
for (WidgetState widget : widgets) {
373+
childWidget = widget;
374+
break;
375+
}
376+
if (mOnSwipe == null || childWidget == null) {
377+
if (childWidget != null) {
378+
return -dy / childWidget.mParentHeight;
379+
}
380+
return 1.0f;
381+
}
382+
if (mOnSwipe.mAnchorId == null) {
383+
384+
float[] dir = mOnSwipe.getDirection();
385+
float motionDpDtX = childWidget.mParentHeight;
386+
float motionDpDtY = childWidget.mParentHeight;
387+
388+
float drag = (dir[0] != 0) ? dx * Math.abs(dir[0]) / motionDpDtX
389+
: dy * Math.abs(dir[1]) / motionDpDtY;
390+
return drag * mOnSwipe.getScale();
368391
}
369392
WidgetState base = mState.get(mOnSwipe.mAnchorId);
370393
float[] dir = mOnSwipe.getDirection();
@@ -378,7 +401,7 @@ public float dragToProgress(float currentProgress, int baseW, int baseH, float d
378401
if (DEBUG) {
379402
Utils.log(" drag " + drag);
380403
}
381-
return drag;
404+
return drag * mOnSwipe.getScale();
382405
}
383406

384407
/**
@@ -412,7 +435,7 @@ public void setTouchUp(float currentProgress,
412435
}
413436

414437
float drag = (dir[0] != 0) ? velocityX / motionDpDt[0] : velocityY / motionDpDt[1];
415-
438+
drag *= mOnSwipe.getScale();
416439
if (DEBUG) {
417440
Utils.log(" >>> velocity " + drag);
418441
Utils.log(" >>> mDuration " + mDuration);
@@ -825,8 +848,8 @@ static class WidgetState {
825848
MotionWidget mMotionWidgetEnd;
826849
MotionWidget mMotionWidgetInterpolated;
827850
KeyCache mKeyCache = new KeyCache();
828-
private int mParentHeight = -1;
829-
private int mParentWidth = -1;
851+
int mParentHeight = -1;
852+
int mParentWidth = -1;
830853

831854
WidgetState() {
832855
mStart = new WidgetFrame();

projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import com.google.accompanist.coil.rememberCoilPainter
3737
class MainActivity : AppCompatActivity() {
3838
private var mFrameLayout: FrameLayout? = null
3939
private var composeNum = 0
40-
private val START_NUMBER = 6
40+
private val START_NUMBER = 48
4141
private var demos:ArrayList<CompFunc> = ArrayList()
4242
var map = HashMap<Int, String>();
4343
val linkServer = LinkServer()
@@ -153,6 +153,8 @@ class MainActivity : AppCompatActivity() {
153153
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeExperiment() } })
154154
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample1() } })
155155
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample2() } })
156+
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample3() } })
157+
156158
demos.add(object : CompFunc { @Composable override fun Run() { MultiSwipe() } })
157159
demos.add(object : CompFunc { @Composable override fun Run() { MotionArc() } })
158160
demos.add(object : CompFunc { @Composable override fun Run() { MotionEasing() } })

projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/OnSwipeExperiment.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,76 @@ fun OnSwipeSample2() {
260260
.background(Color.White),
261261
motionScene = MotionScene(content = scene),
262262
) {
263+
Text(
264+
text = "on Swipe example \n"+
265+
" onSwipe: {\n" +
266+
" anchor: 'box',\n" +
267+
" direction: 'end',\n" +
268+
" side: 'start',\n" +
269+
" mode: 'spring'\n" +
270+
" }"
271+
)
272+
Box(
273+
modifier = Modifier
274+
.background(Color.Green)
275+
.layoutId("box")
276+
)
277+
}
278+
}
279+
}
280+
281+
@Preview
282+
@Composable
283+
fun OnSwipeSample3() {
284+
285+
var scene =
286+
"""
287+
{
288+
ConstraintSets: {
289+
start: {
290+
box: {
291+
width: 50, height: 50,
292+
bottom: ['parent', 'bottom', 70],
293+
start: ['parent', 'start', 170],
294+
}
295+
},
296+
end: {
297+
298+
box: {
299+
width: 50, height: 50,
300+
top: ['parent', 'top', 170],
301+
end: ['parent', 'end', 170],
302+
}
303+
}
304+
},
305+
Transitions: {
306+
default: {
307+
from: 'start',
308+
to: 'end',
309+
onSwipe: {
310+
direction: 'end',
311+
mode: 'spring',
312+
scale: .3,
313+
}
314+
}
315+
}
316+
}
317+
""".trimIndent()
318+
319+
Column {
320+
MotionLayout(
321+
modifier = Modifier
322+
.fillMaxSize()
323+
.background(Color.White),
324+
motionScene = MotionScene(content = scene),
325+
) {
326+
Text(
327+
text = "on Swipe example \n"+
328+
" onSwipe: {\n" +
329+
" direction: 'end',\n" +
330+
" mode: 'spring'\n" +
331+
" }"
332+
)
263333
Box(
264334
modifier = Modifier
265335
.background(Color.Green)

0 commit comments

Comments
 (0)